How to Test Your API With Supertest
Supertest is a fantastic tool for testing your API. Here’s how I use it to test APIs.
Setup
You can configure supertest in a couple ways. If you’re testing an external site, you can configure supertest with the site’s base url:
1
|
|
If you’re testing an Express app, you can pass the app to supertest, and let it worry about setup/teardown of your site.
1 2 |
|
The great thing about this approach is that supertest takes care of starting up and shutting down your site on a random port. This allows you to just write your tests, without worrying about starting apps, conflicting ports, etc.
Writing a Test
Once you’ve imported supertest, it’s quite simple to use. In my examples, I’ll be using the test framework provided by Mocha.
1 2 3 4 5 6 7 |
|
This is about as simple as the tests get. We call the root of the site, and expect the response body to equal “Hello, World!”.
Since the request is async, we take in a “done” parameter (which Mocha passes to us). We call that at the end of the test when we’ve actually had a chance to verify the server response.
You can easily test a JSON response with supertest:
1 2 3 |
|
Headers are also simple to verify.
1 2 3 |
|
Status codes are even easier.
1 2 3 |
|
You can do non-GET requests.
1 2 3 4 5 |
|
You can set headers for the request.
1 2 3 4 |
|
You can match regular expressions against headers and bodies.
1 2 3 4 |
|
If you need to do multiple requests in series, you can chain the “expect” callbacks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Finally, if you need to do some custom processing on a result, use the .end()
function.
1 2 3 4 5 6 7 |
|
There are probably more features that I’m not aware of, but this is a rundown of the ones I’m most familiar with.