- Introduction
- Quick start
- Philosophy
- Comparison
- Default behaviors
- Limitations
- Debugging runbook
- FAQ
- Mocking HTTP
- Mocking SSE
- Mocking GraphQL
- Mocking WebSocket
- Integrations
- API
- CLI
- Best practices
- Recipes
Custom events
Mocking custom server events.
You can specify the custom event type by providing the event key on the client message:
import { sse } from 'msw'
export const handlers = [
sse('/stream', ({ client }) => {
client.send({
event: 'greeting',
data: 'hello world',
})
}),
]This will result in the following message received by the client:
event:greeting
data:hello world
Custom event ID
You can attach a custom ID field to the sent custom event by providing the id property alongside the sent event:
client.send({
id: '1',
event: 'greeting',
data: 'hello world',
})id:1
event:greeting
data:hello world
TypeScript
You can annotate the data type of your custom events by providing a type argument object to the sse function and including the event names as keys on that object:
sse<{ greeting: 'hello world' }>('/stream', ({ client }) => {
client.send({ greeting: 'hello world' }) // ✅
client.send({ greeting: 'goodbye cosmos' }) // ❌
})Note that the
messagekey is reserved for the default message data type. You can combine both the defaultmessagedata type and custom events within the same type.