.run() directly with mocks for fast unit tests, or spin up the full runtime when you need middleware and lifecycle behavior.
Testing Philosophy
Runnerβs testing approach is built on three principles:- Explicit over implicit - Dependencies are injected as plain objects, making mocking trivial
- No framework ceremonies - Call tasks directly in tests; no special test harnesses required
- Test what matters - Unit test business logic with mocks, integration test with real dependencies
Two Testing Strategies
| Approach | Speed | What runs | Best for |
|---|---|---|---|
| Unit test | Fast | Just your function | Logic, edge cases |
| Integration test | Slower | Full pipeline | End-to-end flows, wiring |
When to Use Each
Unit tests (calling.run() directly):
- Testing business logic in isolation
- Edge cases and error handling
- Fast feedback during development
- Bypasses middleware, events, and lifecycle
run() with overrides):
- Testing the full flow including middleware
- Verifying event emissions and hook execution
- Testing with real infrastructure (in-memory DBs, test servers)
- Ensuring components wire together correctly
Test Isolation
Everyrun() call creates a completely isolated container:
- Tests can run in parallel without interference
- Each test gets fresh resources
- No cleanup between tests needed (beyond
dispose())
Running Tests
Basic Test Structure
Always Dispose
Resources hold connections, timers, and listeners. Always calldispose() to prevent:
- Memory leaks
- Port conflicts
- Hanging test processes
- Flaky tests
Debug Mode in Tests
By default, logs are suppressed whenNODE_ENV=test. Enable them for debugging:
| Level | Whatβs logged |
|---|---|
"normal" | Lifecycle events, errors, event emissions |
"verbose" | All of above + task inputs/outputs, resource configs |
Test Patterns
Testing with Mock Dependencies
Type-Safe Test Inputs
Testing Tips
Prefer Task References Over String IDs
Test Registration Errors Early
UsedryRun to validate wiring without starting resources:
Isolate Slow Resources
Create test-specific overrides for expensive resources:Next Steps
- Test Resources - Learn about
createTestResourcehelper and test runtime utilities - Mocking - Deep dive into mocking dependencies and using overrides
Testing with createTestResource (deprecated)Runner provides
createTestResource() helper, but itβs deprecated in favor of calling run() directly. The direct approach is more flexible and has better type safety. See Test Resources for details.