Testing a gateway — WebSockets · NestJS

Write the tests: end-to-end specs that start the application, connect socket.io clients to it, and check what every client receives, so that a broadcast reaching the wrong client, a refused adoption that goes through, or a departure nobody hears fails a test.

What you will learn

Read the theory for Testing a gateway

All WebSockets lessons

All NestJS courses

loading types…

What you'll learn

  • Start a Nest application with its gateways in a test, listening on a port, and close it after each test
  • Connect socket.io clients from a test, listening before connecting so nothing sent during the handshake is missed
  • Assert on acknowledgements with emitWithAck(), and on what every client received, including the ones that should have received nothing
  • Know that a gateway test must wait for events in flight before asserting, and why asserting nothing arrived is a real assertion