Testing an Express Server
A couple weeks ago, I wrote a suite of tests for a new Node-Express service.
You will need one or more of the following tools:
- Mocha: a test runner. In package.json, simply add
"test": "mocha"under “scripts” and all tests in the
testdirectory will run.
- Supertest: an HTTP testing library that lets you easily send HTTP requests to a local server.
- Node-tap: a test library implementing the Test-Anything-Protocol for Node.
Most Valuable Player: Supertest
My favorite new tool on this list is the Supertest library. It is SO easy to hit each Express endpoint with a Supertest request and examine the response.
The way it works: require Supertest and pass it an Express application. This returns a supertest object onto which other methods can be chained. For example, you can set attributes with “get(‘/path’)” or “set(‘Content-Type’, ‘application/json’)” that modify both the object and your eventual request to the server. Expectations for the response are also chained onto the supertest object.
1 2 3 4 5 6 7 8 9
One interesting aspect of writing tests for an Express server is the fact that your tests must run asynchronously. After all, you’re sending a request to your server; if your tests proceeded onwards full steam ahead, you might run into an expectation that checks for a response before the response has even arrived. Mocha also needs wait for the current test to finish before heading off to other tests.
Mocha has two ways of handling asynchronous tests:
- pass in a
donecallback that is called when the test ends, or
- use Mocha’s built-in Promise handling.
Now that Mocha actually handles Promises in-house, it’s considered better practice to use
.catch rather than
1 2 3 4 5 6 7 8 9 10
Node-tap can also be used this way, with
.catch callbacks on a Promise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Now sit back, relax, and watch your tests run.