Closing client connection

You can close an intercepted WebSocket client connection by calling client.close() at any point in your event handler:

api.addEventListener("connaction', ({ client }) => {
	client.close()
})

By default, client.close() results in a graceful connection closure (1000 code).

API reference

client.close(code, reason)

The `client.close()` API reference.

Custom code and reason

Much like WebSocket.prototype.close, you can provide a custom code and reason as the arguments to the client.close() method:

client.close(1003, 'Custom reason')

Since you write your mocks from the server’s perspective, MSW enables custom, user non-configurable closure codes in your mocks, such as 1003 code. Use that to reproduce different closure scenarios originating from the server.