QuickDevGuide

Home › Mocha JS Testing

Testing HTTP APIs — Mocha JS Testing

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