#8247·effect

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

Author: chmnchiangCreated Sep 15, 2026Updated Sep 15, 2026
Labelsenhancement

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:

typescript
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.