52 lines
1.9 KiB
Markdown
52 lines
1.9 KiB
Markdown
# Testing
|
|
|
|
> Source: `src/content/docs/actors/testing.mdx`
|
|
> Canonical URL: https://rivet.dev/docs/actors/testing
|
|
> Description: 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:
|
|
|
|
```bash
|
|
# Install Vitest
|
|
npm install -D vitest
|
|
|
|
# Run tests
|
|
npm test
|
|
```
|
|
|
|
## Basic Testing Setup
|
|
|
|
Rivet includes a test helper called `setupTest` that starts your registry in test mode and returns a client connected to it. This allows for fast, isolated tests without external dependencies.
|
|
|
|
## Testing Actor State
|
|
|
|
State persists 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 by scheduling work and waiting for it to run:
|
|
|
|
Use a short schedule and `expect.poll` the action's observable result. Vitest's fake date and fake JavaScript timers do not advance RivetKit's scheduler, which runs outside the test's JavaScript timer queue.
|
|
|
|
## Best Practices
|
|
|
|
1. **Isolate tests**: Each test should run independently, avoiding shared state.
|
|
2. **Test edge cases**: Verify how your actor handles invalid inputs, concurrent operations, and error conditions.
|
|
3. **Test scheduled operations**: Poll observable state or output with a bounded timeout instead of sleeping for an exact duration.
|
|
4. **Use realistic data**: Test with data that resembles production scenarios.
|
|
|
|
`setupTest` starts the registry and disposes the returned client when the test finishes, so you can focus on writing effective tests for your business logic.
|
|
|
|
## API Reference
|
|
|
|
- [`setupTest`](/typedoc/functions/rivetkit.test_mod.setupTest.html) - Test setup helper function
|
|
|
|
_Source doc path: /docs/actors/testing_
|