Back to Rivet

Testing

website/src/content/docs/actors/testing.mdx

2.3.31.8 KB
Original Source

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.

<CodeSnippet file="examples/docs/actors-testing/basic-setup.ts" />

Testing Actor State

State persists within each test, allowing you to verify that your actor correctly maintains state between operations.

<CodeSnippet file="examples/docs/actors-testing/testing-state.ts" />

Testing Events

For actors that emit events, you can verify events are correctly triggered by subscribing to them:

<CodeSnippet file="examples/docs/actors-testing/testing-events.ts" />

Testing Schedules

Rivet's schedule functionality can be tested by scheduling work and waiting for it to run:

<CodeSnippet file="examples/docs/actors-testing/testing-schedules.ts" />

Use a short real-time delay to wait for scheduled work to run. setupTest does not install fake timers, so if you want to use vi.useFakeTimers() you must enable it yourself and confirm it works with your selected runtime.

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: Use short real-time delays to wait for scheduled work to run.
  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