@effect/vitest, which integrates Effect’s runtime with Vitest. Write tests using familiar syntax while leveraging Effect’s powerful features like dependency injection, time control, and structured concurrency.
Getting Started with @effect/vitest
Install the testing package:it.effect test helper:
Basic Testing Patterns
Using it.effect
Theit.effect function runs Effect code in your tests. It automatically handles the Effect runtime and provides built-in assertions:
Parameterized Tests
Useit.effect.each for parameterized tests:
Property-Based Testing
Useit.effect.prop with Schema-based arbitraries for property-based testing:
Controlling Time with TestClock
TestClock allows you to control virtual time in tests, making it easy to test time-dependent code without waiting:
Testing with Real Time
When you need to test with real runtime services (actual time, random, etc.), useit.live:
Testing Services with Layers
Effect’s layer system makes it easy to test services with mock dependencies.Creating Test Layers
Define test-specific layers that provide mock implementations:Shared Layers Across Tests
Use thelayer() helper to create one shared layer for all tests in a block:
Testing Composed Services
Test higher-level services by composing their test layers:Testing Patterns
- Test Isolation
- Mock Dependencies
Assertion Helpers
Theassert object from @effect/vitest provides common assertions:
Best Practices
Test Real Behavior
Test services through their public interface, not implementation details. Use
layer() for integration-style tests.Use TestClock
Always use
TestClock for time-dependent tests. Never use real delays in tests.Separate Layers
Create separate
layerTest and layer (production) implementations. Keep test layers simple and focused.Test Refs
Use test ref services to expose internal state for assertions without breaking encapsulation.
Next Steps
Observability
Add logging and tracing to understand test behavior
Error Handling
Learn how to test error cases and recovery
