8000 Promote bootstrap layer sharing in migration guide by swoogles · Pull Request #7187 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Promote bootstrap layer sharing in migration guide #7187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 58 additions & 17 deletions docs/guides/migrate/migration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -2246,36 +2246,77 @@ So without any special effort, whenever we use `ZSpec` we should change it to `S
}
```

### Composable Test Apps

In ZIO 2.x, the `DefaultRunnableSpec` deprecated, so in order to write tests, we should extend the `ZIOSpecDefault` abstract class. Due to this change, executable tests can be composed, similar to `ZIOAppDefault`:
### Sharing Layers between Specs
In ZIO 1.x, in order to share costly layers between specs, you needed a "Master" spec that would invoke test code from individual Spec classes.
In ZIO 2.x, sharing layers is much simpler.
Use `ZIOSpec[YourSharedType]` and plug your layer into the `bootstrap` field.

```scala mdoc:compile-only
import zio.ZIOApp
import zio.test._

object ExampleSpec1 extends ZIOSpecDefault {
def spec = suite("suite 1")(
test("passing test 1")(assertTrue(true)),
test("failing test 2")(assertTrue(false))
)
class SharedService()

object Layers {
val sharedLayer =
ZLayer.succeed(new SharedService())
}

object ExampleSpec2 extends ZIOSpecDefault {
def spec = suite("suite 2")(
test("passing test")(assertTrue(true))
)
object UseSharedLayerA extends ZIOSpec[SharedService]{
override def spec =
test("use the shared layer in test A") {
assertCompletes
}

override def bootstrap= Layers.sharedLayer
}

object AllSpecs extends ZIOApp.Proxy(ExampleSpec1 <> ExampleSpec2)
object UseSharedLayerB extends ZIOSpec[SharedService]{
override def spec =
test("use the shared layer in test B") {
assertCompletes
}

override def bootstrap = Layers.sharedLayer
}
```
Then use the standard
```bash
sbt test
```
in order to run the tests.

To run `AllSpecs` with sbt:
Note - If you assign the results of a function call to `bootstrap`, like this:

```bash
sbt Test/runMain AllSpecs
```scala mdoc:compile-only
import zio.test._
class SharedService()

object LayerBuilder {
def createLayer() = ZLayer.succeed(new SharedService())
}

object NotUsingSharedLayerA extends ZIOSpec[SharedService]{
override def spec =
test("use the shared layer in test A") {
assertCompletes
}

override def bootstrap = LayerBuilder.createLayer()
}

object NotUsingSharedLayerB extends ZIOSpec[SharedService]{
override def spec =
test("use the shared layer in test B") {
assertCompletes
}

override def bootstrap = LayerBuilder.createLayer()
}
```

These Specs will *not* share the same instance, as they are different references.


### Smart Constructors

By introducing smart constructors, we do not longer have the `testM` function to create effectful test suits. Instead, we should use the `test` function:
Expand Down
0