| name | effect-testing-mocking |
| description | Testing patterns with layers, mocks, and deterministic time. Use when preparing testable services and small smoke tests. |
| allowed-tools | Read, Grep, Glob, Edit, Write |
Testing & Mocking
When to use
- You need deterministic tests without external IO
- You want to inject mock services via layers
Mock Layer
const RepoTest = Layer.succeed(Repo, Repo.of({ find: () => Effect.succeed(mock) }))
Provide Test Impl
const result = yield* Effect.provide(program, RepoTest)
Test Clock
Guidance
- Keep tests focused and fast; avoid network by mocking services
- Provide layers explicitly to match program requirements
- Keep tests close to real interfaces; do not reshape app code for tests
- Use
.Default layers for quick wiring; add custom mock layers per test
- Favor small smoke tests that run quickly and fail fast
Pitfalls
- Leaking real IO into tests → flakiness
- Over-mocking internals → brittle tests; mock at service boundary
Cross-links
- Layers & Services for DI patterns
- Time/Logging for deterministic time and observable output
Local Source Reference
CRITICAL: Search local Effect source before implementing
The full Effect source code is available at docs/effect-source/. Always search the actual implementation before writing Effect code.
Key Source Files
- Layer:
docs/effect-source/effect/src/Layer.ts
- TestClock:
docs/effect-source/effect/src/TestClock.ts
- TestContext:
docs/effect-source/effect/src/TestContext.ts
Example Searches
grep -F "Layer.succeed" docs/effect-source/effect/src/Layer.ts
grep -F "TestClock" docs/effect-source/effect/src/TestClock.ts
grep -F "TestContext" docs/effect-source/effect/src/TestContext.ts
grep -F "Layer.succeed" docs/effect-source/effect/test/Layer.test.ts
Workflow
- Identify the testing API you need (e.g., Layer.succeed, TestClock)
- Search
docs/effect-source/effect/src/Layer.ts for the implementation
- Study the types and testing patterns
- Look at test files for usage examples
- Write your code based on real implementations
Real source code > documentation > assumptions
Real-world snippet: Build a comprehensive Test layer
export const TestLayer = (input?: TestLiveInput) =>
Effect.gen(function* () {
const tempDir = tempy.temporaryDirectory({ prefix: 'test' })
const cwd = (yield* setupFixtureFolder({ fixture: input?.fixture, tempDir })) ?? tempDir
const NodeOsTest = Layer.succeed(NodeOs, new NodeOs({ homedir: cwd, arch: 'arm64', platform: 'darwin' }))
const NodeProcessTest = Layer.succeed(NodeProcess, new NodeProcess({ cwd, platform: 'darwin', arch: 'arm64' }))
const ToolkitsRepoTest = Layer.succeed(ComposioToolkitsRepository, new ComposioToolkitsRepository({
getToolkits: () => Effect.succeed([]),
: .([])
}))
layers = .(
.(* .),
.(),
,
.(, .(., )),
,
.,
.,
.,
.,
.,
.
)
layers
}).(
.(.),
.,
.,
.(.(input?. ?? .( ([]))))
)
References