typelevel / munit-cats-effect

Integration library for MUnit & cats-effect
Apache License 2.0
149 stars 34 forks source link

ResourceSuiteLocalFixture is allocated after suite's beforeAll() #234

Open kamilkloch opened 1 year ago

kamilkloch commented 1 year ago

The following code

import cats.effect.IO
import cats.effect.kernel.Resource
import munit.CatsEffectSuite

class TestWsResource extends CatsEffectSuite {

  val d = ResourceSuiteLocalFixture(
    "Double resource",
    Resource.make(IO.println("Allocating resource") >> IO(4.0))(d => IO.println(s"Releasing resource: $d"))
  )

  override val munitFixtures = List(d)

  test("resource") {
    IO.println(s"Using resource: ${d()}")
  }

  override def beforeAll(): Unit = {
    // println(s"{${d()}")
    println(s"{d()}")
  }

  override def afterAll(): Unit = {
    println(s"After all ${d()}")
  }

}

produces

{{d()}
Allocating resource
Using resource: 4.0
After all 4.0
Releasing resource: 4.0

Changing beforeAll() to

  override def beforeAll(): Unit = {
    println(s"{${d()}")
  }

results in

Allocating resource
Releasing resource: 4.0

Process finished with exit code 255

munit.catseffect.ResourceFixture$FixtureNotInstantiatedException: The fixture `Double resource` was not instantiated. Override `munitFixtures` and include a reference to this fixture.

It looks that suite local resource is allocated after beforeAll and released after afterAll . Is this the desired behavior?

armanbilge commented 1 year ago

Thanks for opening the issue. Which version is this? Can you replicate with the 2.x milestone? https://github.com/typelevel/munit-cats-effect/releases/tag/v2.0.0-M1

kamilkloch commented 1 year ago

Forgot to add: I am using v2.0.0-M1.

armanbilge commented 1 year ago

Thanks for clarifying. Sorry, one more question: can you reproduce this without IO and munit-cats-effect? I have a hunch this is a question for munit itself.

See the docs here: https://github.com/scalameta/munit/blob/main/docs/fixtures.md#reusable-suite-local-fixtures

kamilkloch commented 1 year ago

@armanbilge It looks like the munit matter indeed: https://github.com/scalameta/munit/issues/573

mzuehlke commented 2 months ago

Can this issue be solved ? As the seen behaviour is the intended one ? ~But the linked munit issues (https://github.com/scalameta/munit/issues/573) is still open, too...~ it got closed in the meantime