Lesson
Test WebSockets in Vitest with MSW
Write an integration test for a WebSocket component in Vitest and React Testing Library (RTL) using Mock Service Worker.
- Access
- Included
- Transcript
- Needs source
Let's write an integration test for our <Chat/> component in Vitest and React Testing Library.
The WebSocket interception in MSW requires a global WebSocket class. You are covered if you're using Node.js v22 or higher since that global class exists there. But for older versions, we will create a custom Vitest environment to polyfill it using undici.