integration testing an api with mocha and superagent
This seemed to be the week people showed me testing triangles.
If you’ve never seen one then I’ve included one below.
This doesn’t give you the full range of tests but it does give you an overview.
The top of the triangle can includes acceptance tests, performance tests and security - anything that sits over the whole system.
The example I have used here is against a simple API, but tools like superagent could also run against the front-end of a website.
API
I’ve built a small API in express, this has a couple of routes that serve user data.
- /api/users - this gives a list of users
- /api/users/:id - this gives a 404 error if not found, or the requested user
These are self-contained integration tests, before all the tests a script starts a server.
Most of the time I’d be using a CI tool like Jenkins to run these tests against an actual service. Rather than having a static data file, this might be running against either a stub or database.
For these tests I’ve Mocha as a test runner and chai as an assertion library.
To drive requests to the website I have used superagent this can work both server and client side.
I did look at chai-http this mostly wraps superagent, so didn’t seem to add value.
Tests
With this api example there will be three tests
- test to get all users
- test to get user with valid id
- test to get user with invalid id
This will start up a server then run the tests against it, this could also be run on an external website, or against a HTML front-end.
The output of the tests are below.