To unit test routes with Express and JavaScript, we use supertest.
For instance, we write
describe("GET /users", () => {
it("respond with json", (done) => {
request(app)
.get("/users")
.set("Accept", "application/json")
.expect(200)
.end((err, res) => {
if (err) return done(err);
done();
});
});
});
to call request with app to use the Express app to make requests.
Then we call get to make a get request to the /users route.
We call set to set the request header.
We call expect to check that the status code returned is 200.
And we call end with a callback that calls done to finish the test.
The test passes if done is called with no argument.