Testing
Rivet provides a straightforward testing framework to build reliable and maintainable applications. This guide covers how to write effective tests for your actor-based services.
Setup
To set up testing with Rivet:
Basic Testing Setup
Rivet includes a test helper called setupTest
that configures a test environment with in-memory drivers for your actors. This allows for fast, isolated tests without external dependencies.
Testing Actor State
The test framework uses in-memory drivers that persist state within each test, allowing you to verify that your actor correctly maintains state between operations.
Testing Events
For actors that emit events, you can verify events are correctly triggered by subscribing to them:
Testing Schedules
Rivet's schedule functionality can be tested using Vitest's time manipulation utilities:
The setupTest
function automatically calls vi.useFakeTimers()
, allowing you to control time in your tests with functions like vi.advanceTimersByTimeAsync()
. This makes it possible to test scheduled operations without waiting for real time to pass.
Best Practices
- Isolate tests: Each test should run independently, avoiding shared state.
- Test edge cases: Verify how your actor handles invalid inputs, concurrent operations, and error conditions.
- Mock time: Use Vitest's timer mocks for testing scheduled operations.
- Use realistic data: Test with data that resembles production scenarios.
Rivet's testing framework automatically handles server setup and teardown, so you can focus on writing effective tests for your business logic.