import { expect, it } from "@effect/vitest"
import { Context, Effect, Layer } from "effect"
class Counter extends Context.Tag("@app/Counter")<
Counter,
{ readonly get: () => Effect.Effect<number>; readonly increment: () => Effect.Effect<void> }
>() {
static readonly layer = Layer.sync(Counter, () => {
let count = 0
return {
get: () => Effect.succeed(count),
increment: () => Effect.sync(() => void count++),
}
})
}
// Each test provides the layer, so each gets its own fresh counter
it.effect("starts at zero", () =>
Effect.gen(function* () {
const counter = yield* Counter
expect(yield* counter.get()).toBe(0)
}).pipe(Effect.provide(Counter.layer)),
)
it.effect("increments without leaking", () =>
Effect.gen(function* () {
const counter = yield* Counter
yield* counter.increment()
expect(yield* counter.get()).toBe(1)
}).pipe(Effect.provide(Counter.layer)),
)
import { expect, it } from "@effect/vitest"
import { Context, Effect, Layer } from "effect"
class Counter extends Context.Tag("@app/Counter")<
Counter,
{ readonly get: () => Effect.Effect<number>; readonly increment: () => Effect.Effect<void> }
>() {
static readonly layer = Layer.sync(Counter, () => {
let count = 0
return {
get: () => Effect.succeed(count),
increment: () => Effect.sync(() => void count++),
}
})
}
// Each test provides the layer, so each gets its own fresh counter
it.effect("starts at zero", () =>
Effect.gen(function* () {
const counter = yield* Counter
expect(yield* counter.get()).toBe(0)
}).pipe(Effect.provide(Counter.layer)),
)
it.effect("increments without leaking", () =>
Effect.gen(function* () {
const counter = yield* Counter
yield* counter.increment()
expect(yield* counter.get()).toBe(1)
}).pipe(Effect.provide(Counter.layer)),
)