Skip to content
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

Sharing multiple layers across test files #9688

Open
petern-sc opened this issue Mar 12, 2025 · 0 comments
Open

Sharing multiple layers across test files #9688

petern-sc opened this issue Mar 12, 2025 · 0 comments
Labels
question Further information is requested

Comments

@petern-sc
Copy link

petern-sc commented Mar 12, 2025

Going off this example https://zio.dev/reference/test/sharing-layers-between-multiple-files, I'm able to get a single resource shared but this doesn't seem possible if I want to share multiple resources.

As an example, I have 2 test containers, Mysql and Kafka.
Some integration tests require only Mysql and some only Kafka, which works with this pattern

trait SharedDatabaseSpec extends ZIOSpec[TestEnvironment with MySQLContainer] {
  override val bootstrap: ZLayer[Any, Nothing, TestEnvironment with MySQLContainer] = testEnvironment ++ ZMySQLContainer.live
}

trait SharedKafkaSpec extends ZIOSpec[TestEnvironment with KafkaContainer] {
  override val bootstrap: ZLayer[Any, Nothing, TestEnvironment with KafkaContainer] = testEnvironment ++ KafkaContainer.live
}

But with some tests that require both Kafka and Database containers, this pattern no longer works.

MyExampleSpec extends SharedDatabaseSpec with SharedKafkaSpec { ... }

illegal inheritance;
 self-type RecoveryIntegrationSpec.type does not conform to zio.test.ZIOSpec[zio.test.TestEnvironment with com.dimafeng.testcontainers.MySQLContainer]'s selftype zio.test.ZIOSpec[zio.test.TestEnvironment with com.dimafeng.testcontainers.MySQLContainer]
object RecoveryIntegrationSpec extends SharedDatabaseSpec with SharedKafkaSpec {

I assume this is because both are overriding bootstrap and there isn't a way to compose at the traits level. One option I considered is to just have another trait which includes both types but I couldn't figure out if there's a way to memoize or re-use the same containers from the previous traits.

trait SharedContainerSpec extends ZIOSpec[TestEnvironment with KafkaContainer with MySQLContainer] {
  override val bootstrap: ZLayer[Any, Nothing, TestEnvironment with KafkaContainer] = testEnvironment ++ KafkaContainer.live ++ ZMySQLContainer.live
}

Is there any other way to achieve what I'm looking to do or is that not possible at the moment?

Also somewhat related to the layer sharing, is there a reason provideSome supports providing a list of layers, but provideSomeLayerShared only accepts a single zlayer?

@hearnadam hearnadam added the question Further information is requested label Mar 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants