Skip to content

effect/vitest: Make a way to configure it.layer so that some service instances are recreated each test #8247

Description

@chmnchiang

What is the problem this feature would solve?

Currently, it.layer(...) let us inject the same layer in each test. The service instances are shared, but while having the same dependencies for each test is common, usually we also want the instances to start fresh and be recreated for each test.

What is the feature you are proposing to solve the problem?

An example API will be:

import { it, TestLayer } from "@effect/vitest"
import { Effect } from "effect"

// Proposed API.
// RepositoryLive depends on the database and cache services.
const TestServices = TestLayer.merge(
  TestLayer.shared(DatabaseTest),
  TestLayer.each(CacheTest),
  TestLayer.each(RepositoryLive)
)

it.layer(TestServices)("repository", (it) => {
  it.effect("creates a record", () =>
    Effect.gen(function* () {
      const repository = yield* Repository
      // Shared database, fresh cache and repository.
    })
  )

  it.effect("lists records", () =>
    Effect.gen(function* () {
      const repository = yield* Repository
      // Same database, new cache and repository.
    })
  )
})

Or some way to mark the service when creating the layer

What alternatives have you considered?

  • Provide layers inside each test using Effect.provide: Very repeatitive
  • Reset shared services in beforeEach: beforeEach is not effect-aware and has no access to the layer unfortunately.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions