For API tests, start the server on a random free port in before, call it over HTTP, and close it in after. Approach Notes Built-in fetch (Node 18+) zero dependencies, as below supertest request(app).get("/users").expect(200) — no port needed for Express apps chai-http chai.request(app) with Chai assertions Test what a client sees Status codes, headers (content-type, location), JSON body shape. Error paths: 400/404/422 with useful messages. Isolate data per test (reset the in-memory store, or wrap DB work in transactions).
Key Takeaways
Listen on port 0 to get a free port and avoid clashes in CI.
Start servers in before and close them in after.
Assert on status, headers and body — the contract clients depend on.
supertest can test Express apps without opening a port at all.