Hi there, quick question about Listeners: I see th...
# kotest
w
Hi there, quick question about Listeners: I see that
Listener
class is deprecated, and
listeners()
method on
AbstractProjectConfigs
suggests to use `Extension`s instead. However,
TestListener
interface (and all the
BeforeEachListener
and friends) is not deprecated. Is it an oversight, or I should keep implementing a
TestListener
and registering it as an extension?
If I should use an extension, do I understand correctly that
Copy code
class MyListener : TestListener {
  override suspend fun prepareSpec() = foo()
  override suspend fun finalizeSpec() = bar()
}
should be translated to
Copy code
class MyExtension : SpecExtension {
  override suspend fun intercept(spec, execute) {
    foo()
    execute(spec)
    bar()
  }
}
s
Keep using TestListener and just register them in the extensions method
Listeners are extensions too
w
Thanks 🙂 Btw would the extension I wrote above be equivalent to the test listener, or I would break something?
s
Same
w
Many thanks, btw I finally updated to 5.x and it works perfect now 🙂 Thanks for all the effort you put into the library 🙏
s
Glad you're enjoying it :)
w
fyi actually the above migration broke something 😄 I think perhaps prepare/finalize is called before the spec is instantiated, and SpecExtension is called when it’s already created. No big deal, just wanted to put it out there in case it’s useful for someone in the future
s
There's two spec extensions. Try the other one sorry.
w
The other one would be
SpecRefExtension
?
s
I think so, can't remember off the top of my head. One is called before the spec is created (what you want) and one is called after (equiv to BeforeSpecListener / AfterSpecListener)
Unless you specifically want the around advice capability, you can stick with the listeners. They are not deprecated. The word listener is just used here to mean it's side effecting only.
w
Yep, I was just curious — ended up keeping a listener 🙂