diff --git a/test/shared/src/main/scala/zio/test/TestAspect.scala b/test/shared/src/main/scala/zio/test/TestAspect.scala index 2640edd20a70..aae6cecc1139 100644 --- a/test/shared/src/main/scala/zio/test/TestAspect.scala +++ b/test/shared/src/main/scala/zio/test/TestAspect.scala @@ -100,10 +100,24 @@ object TestAspect extends TimeoutVariants { test <* effect.catchAllCause(cause => ZIO.fail(TestFailure.Runtime(cause))) } + /** + * Constructs an aspect that evaluates every test inside the context of a `Managed`. + */ + def around[R0, E0](before: ZIO[R0, E0, Any], after: ZIO[R0, Nothing, Any]) = + new TestAspect.PerTest[Nothing, R0, E0, Any, Nothing, Any] { + def perTest[R >: Nothing <: R0, E >: E0 <: Any, S >: Nothing <: Any]( + test: ZIO[R, TestFailure[E], TestSuccess[S]] + ): ZIO[R, TestFailure[E], TestSuccess[S]] = + ZManaged + .make(before)(_ => after) + .foldCauseM(c => ZManaged.fail(TestFailure.Runtime(c)), ZManaged.succeed) + .use(_ => test) + } + /** * Constructs an aspect that evaluates every test inside the context of the managed function. */ - def around[R0, E0, S0]( + def aroundTest[R0, E0, S0]( managed: ZManaged[R0, TestFailure[E0], TestSuccess[S0] => ZIO[R0, TestFailure[E0], TestSuccess[S0]]] ) = new TestAspect.PerTest[Nothing, R0, E0, Any, S0, S0] { diff --git a/test/shared/src/test/scala/zio/test/TestAspectSpec.scala b/test/shared/src/test/scala/zio/test/TestAspectSpec.scala index 102727489df2..84494c0ed1e3 100644 --- a/test/shared/src/test/scala/zio/test/TestAspectSpec.scala +++ b/test/shared/src/test/scala/zio/test/TestAspectSpec.scala @@ -10,12 +10,26 @@ import zio.test.TestUtils.{ execute, ignored, label, succeeded } object TestAspectSpec extends DefaultRuntime { val run: List[Async[(Boolean, String)]] = List( + label(aroundEvaluatesTestsInsideContextOfManaged, "around evaluates tests inside context of Managed"), label(jsAppliesTestAspectOnlyOnJS, "js applies test aspect only on ScalaJS"), label(jsOnlyRunsTestsOnlyOnScalaJS, "jsOnly runs tests only on ScalaJS"), label(jvmAppliesTestAspectOnlyOnJVM, "jvm applies test aspect only on ScalaJS"), label(jvmOnlyRunsTestsOnlyOnTheJVM, "jvmOnly runs tests only on the JVM") ) + def aroundEvaluatesTestsInsideContextOfManaged: Future[Boolean] = + unsafeRunToFuture { + for { + ref <- Ref.make(0) + spec = testM("test") { + assertM(ref.get, equalTo(1)) + } @@ around(ref.set(1), ref.set(-1)) + _ <- execute(spec) + result <- succeeded(spec) + after <- ref.get + } yield result && (after == -1) + } + def jsAppliesTestAspectOnlyOnJS: Future[Boolean] = unsafeRunToFuture { for {