Make requests against express handlers without a server.
This module supports testing express handlers and middleware without having to start an HTTP server. Instead a runtime is provided which constructs request/response objects, calls the handler or middleware with them an captures the response.
The following interfaces are currently supported:
- superagent
- supertest
Once installed, the module is required and is ready to be wrapped around the express app or middleware you wish to test:
const expressception = require("expressception");
Let's take the following simple express app to demonstrate how it works:
const express = require("express");
const app = express().post("/foo/bar", (req, res) => {
res.status(201).send();
});
The superagent interface exposes a fully compatible
const superAgent = expressception(app).superagent();
agent.post("/foo/bar").end((err, res) => {
if (err) {
throw new Error("something went wrong");
}
console.log(`Retuned status code: ${res.status}`);
});
Most often prtojects using superagent
for testing use its counterpart
supertest which extends the
it and provides helper methods for making assertions on the response,
such as checking the expected status code.
The entire API surface of supertest
is also supported:
const testAgent = expressception(app).supertest();
(async function() {
await testAgent
.post("/foo/bar")
.expect(201)
.expect("Content-Type", /^text\/plain/)
.expect("hello!");
})();
In addition to local tests the module is also tested against the
supertest
test suite. This is bundled in the tree and the current
baseline version is 4.0.2.