Testing Node APIs
Prove your Express routes work — automatically, on every change — with Jest and Supertest
- write and run a test suite with Jest (
describe,test,expect); - drive your Express app with Supertest — no port, no
listen; - assert on status codes, JSON bodies, and headers of real routes;
- mock a slow or external dependency (like a database) so tests stay fast and deterministic;
- reset test data between tests so each one runs in isolation.
1. Why automate API tests?
You already know how to build routes with Express. But how do you know they still work after you refactor, add a feature, or upgrade a dependency? Clicking through the app by hand is slow, easy to forget, and impossible to repeat exactly. An automated test encodes the check once and runs it forever.
Analogy. Manual testing is like tasting a soup once and hoping it stays the same all week. An automated test is a smoke detector wired into the kitchen: it watches continuously and screams the moment something burns, whether you are in the room or not.
API tests sit at a useful level. They are broader than a unit test (which checks a single function in isolation) but cheaper and faster than a full end-to-end test (which drives a real browser against a deployed server). An API test loads your Express app in memory, fires an HTTP request at it, and inspects the response. Fast enough to run hundreds of times a day; realistic enough to catch routing, status-code, and body-shape bugs.