@cloudflare/vitest-pool-workers, which runs tests inside the actual workerd runtime for accurate results.
Why vitest-pool-workers?
Traditional testing frameworks run in Node.js, which has different APIs and behaviors than the Workers runtime.vitest-pool-workers runs your tests in the same workerd runtime as production, ensuring:
- Accurate testing - Tests run in the actual Workers environment
- Real bindings - Test with actual KV, R2, D1, Durable Objects implementations
- Fast execution - Tests run directly in workerd without network overhead
- Type safety - Full TypeScript support with runtime types
Installation
Quick Start
Configuration
Basic Configuration
vitest.config.ts
With Miniflare Options
vitest.config.ts
Multiple Workers
vitest.config.ts
Testing Patterns
Unit Tests
Test individual functions in isolation:src/utils.test.ts
Integration Tests
Test your Worker’s fetch handler:src/index.test.ts
Testing with Bindings
src/kv.test.ts
Testing Durable Objects
src/counter.test.ts
Testing D1 Databases
src/database.test.ts
test/setup.ts
vitest.config.ts
Testing Queues
src/queue.test.ts
Testing Scheduled Events
src/cron.test.ts
Mocking Fetch Requests
src/api.test.ts
Testing ExecutionContext
src/context.test.ts
Advanced Configuration
Isolated Storage
Each test file gets isolated storage by default. EnablesingleWorker for shared storage:
vitest.config.ts
Environment-Specific Testing
vitest.config.ts
Dynamic Configuration
vitest.config.ts
Global Setup
vitest.config.ts
test/global-setup.ts
Best Practices
1. Use Isolated Tests
Each test should be independent:2. Test Edge Cases
3. Use Type Safety
4. Test Async Operations
Troubleshooting
Tests Not Running
Ensure your pool is configured:vitest.config.ts
Binding Not Available
Check yourwrangler.json configuration and ensure bindings are defined.
Type Errors
Generate types:Timeout Errors
Increase test timeout:vitest.config.ts
See Also
Local Development
Develop Workers locally with wrangler dev
Debugging
Debug Workers with DevTools
Vitest Documentation
Learn more about Vitest testing framework