JavaScript testing #14. Mocking WebSockets using the mock-socket library

React Testing

This entry is part 14 of 18 in the JavaScript testing tutorial

Every automated test that we write should be deterministic. One of the things we need to do to achieve that is to make our frontend tests independent of the backend. Besides mocking REST API requests, we also need to learn how to mock a WebSocket connection. In this article, we create a simple backend application and use React to connect to it and render data. We also write unit tests that ensure that our frontend application works correctly.

Creating a simple application with WebSockets

Let’s start by defining a simple application that uses WebSockets:

  • the frontend application establishes a connection with the WebSocket server and sends a message,
  • the server receives the and starts sending logs,
  • the server sends the message if there are no more logs,
  • the frontend application closes the WebSocket connection as soon as it receives the message.

First, we need to define the format of the messages our WebSocket server sends.

ServerMessage.ts

Besides the above, we also need to specify the messages that our frontend application can send.

ClientMessage.ts

Feel free to add more types of message types if your applicatio needs it.

Creating the WebSocket server

Once we have the messages defined, we can create the WebSocket server we will mock later.

server.ts

If you want to know more about WebSockets, check out Introduction to WebSockets. Creating a Node.js server and using WebSocket API in the browser

Above, we send all the messages one by one as soon as the client sends the message.

Creating the frontend application

In our frontend application, we aim to receive all of the logs and render them.

LogsList.tsx

To connect to the server, we need to add its URL to the environment variables.

.env

In this simple example, we use Create React App. With CRA, all custom environment variables have to start with .

For the above variable to work well with TypeScript, let’s declare it in the file.

react-app-env.d.ts

Under the hood of our component, we want to connect to the WebSocket server and receive the logs.

useLogsList.tsx

Doing the above causes our React application to establish a WebSocket connection and render all the logs.

Testing our frontend application with mock-socket

To test our frontend application, we must first install the mock-socket library.

To be able to mock the WebSocket connection, we need to create an instance of the .

The mock-socket library also has the socket.io support.

The class mimics the WebSocket API we’ve used in Node.js. Thanks to that, it is straightforward to use.

Once we don’t need our server anymore, we can call .

Let’s write a simple test combining all of the above knowledge.

LogsList.test.tsx

PASS src/LogsList/LogsList.test.tsx
The LogsList component
if the WebSocket sends messages and announces the end of logs
✓ should render all of the logs

Testing if the WebSocket connection was closed

We want our frontend application to close the WebSocket connection as soon as it receives the message.

To test the above, we can take advantage of the function that returns the array of all connected clients.

LogsList.test.tsx

PASS src/LogsList/LogsList.test.tsx
The LogsList component
if the WebSocket sends messages and announces the end of logs
✓ should render all of the logs
✓ should close the WebSocket connection

Above, we use the function because our code is asynchronous, and the application doesn’t close the connection immediately.

There is another case in which we want our application to close the WebSocket connection. We want it to happen when the application doesn’t receive the message, but the component is unmounted.

LogsList.test.tsx

PASS src/LogsList/LogsList.test.tsx
The LogsList component
if the WebSocket sends messages and does not announce the end of logs
✓ should render all of the logs
and when the component is unmounted
✓ should close the WebSocket connection

Testing the messages sent by the client

We might want to test if the frontend application sends the correct messages to our WebSocket server. To do the above, we can declare an array of the client messages and update it when the message arrives.

Summary

In this article, we’ve created a simple Node.js server and a frontend application that connects to it with a WebSocket connection. We’ve tested if the React application correctly interprets the messages sent by the server. Besides that, we’ve also tested if the client sends the expected messages and closes the connection when appropriate. All of the above gave us a solid grasp on how to use the mock-socket library to test frontend applications that use a WebSocket connection.

Series Navigation<< JavaScript testing #13. Mocking a REST API with the Mock Service WorkerJavaScript testing #15. Interpreting the code coverage metric >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments