Introduction
I had 94% test coverage on a React app. Components, hooks, the edge cases nobody thinks about. That same sprint I pushed an API change that broke authentication for anyone who hadn't logged in recently. Found out three hours later from a support ticket.
The test suite was green the whole time.
If you know Jest for React, you already know 80% of what you need to test an Express API. The other 20% is a different test environment and one library you haven't used yet. This article covers that 20%.
The Setup
Start with a plain Node project. No Create React App, no framework opinions. Just Express.
mkdir api-testing-demo cd api-testing-demo npm init -y npm install express npm install --save-dev jest supertest @types/jest
You know Jest. Supertest is the new piece. It lets you fire HTTP requests directly at your Express app without starting a server on a real port. Instead of rendering components, you're hitting endpoints.…