Testing
Test your Motia Backend System - APIs, Queue handlers
You built an API. You added some queue handlers. Everything seems to work. But does it?
Without tests, you're guessing. With tests, you know.
Motia has motia/test built in. It helps you:
- Test API triggers (hit endpoints, check responses)
- Test Queue triggers (verify messages get enqueued)
- Mock contexts for unit tests
Install
The test utilities are included in the motia package. No extra install needed.
Test API Triggers
Here's an API Step that creates a todo and enqueues a message:
Now let's test it:
What's happening here:
createMotiaTester()- Spins up a test version of your apptester.post()- Hits your API like a real client wouldtester.watch()- Captures messages on a topictester.waitEvents()- Waits for all async stuff to finish- Then check if everything worked
Test Queue Triggers
Queue Steps listen for messages and do stuff in the background. Here's how to test them:
The Test:
Use tester.enqueue() to manually fire messages and test Queue triggers without hitting APIs.
Unit Test Handlers
Don't want to spin up the whole app? Test handler functions directly:
Run Your Tests
All tests:
Watch mode (re-runs when you save files):
Single test file:
Tester API
createMotiaTester()
Starts a test version of your app.
What you can do with it:
| Method | What it does |
|---|---|
post(path, options) | Hit a POST endpoint |
get(path, options) | Hit a GET endpoint |
enqueue(message) | Enqueue a message to a topic |
watch(topic) | Catch messages on a topic |
waitEvents() | Wait for messages to finish |
sleep(ms) | Pause for X milliseconds |
close() | Shut down the tester |
createMockContext()
Mock a context for testing handlers directly.
You get:
logger- Mock logger (Jest spy)enqueue- Mock enqueue (Jest spy)traceId- Request trace IDstate- Mock state manager
Tips
- Start simple - Test basic stuff first, then edge cases
- Test errors - Make sure your error handling actually works
- Watch messages - Don't assume messages were enqueued, check them
- Always wait - Call
waitEvents()or messages might not finish - Clean up - Always
close()the tester when done - Keep it isolated - Each test should work on its own
- Name tests well - Say what you're checking, not how