DEV Community

Cover image for Testing Express routes with Tape and Nock
Godwin Alexander
Godwin Alexander

Posted on • Updated on

Testing Express routes with Tape and Nock

Testing is a very important aspect of software development. There are many testing frameworks out there, the likes of

  • mocha
  • jest
  • jasmine

etc

For the sake of simplicity we'll be using Tape as the testing framework in this article.
Why Tape?

  • simplicity
  • little or no configuration required
  • easy to read and maintain.

With all that said let's dive in and explore testing with tape.

var test = require('tape');

test('addition test', () => {
   t.equal(1 + 1, 2, '1+1=2')
});
Enter fullscreen mode Exit fullscreen mode

Running the piece of code should result in
Alt Text

For this tutorial we'll be testing a simple express route which makes an API call to get some resource.
The complete code(repo) for this tutorial can be found at github
clone the repo to follow along with the tutorial because we'll be working in the test directory here.
Alt Text

In the code snippet above we export the app object so we can run our test on it.
run

npm install tape nock get-port -D
Enter fullscreen mode Exit fullscreen mode

to install tape, nock and get-port as development dependencies.
nock is an npm package used to mock http request so as to prevent us from making live request during testing.
get-port helps us get a random available port.

npm install bent --save
Enter fullscreen mode Exit fullscreen mode

bent would be used to make api calls during test.
Now, create a directory called tests, make a file called index.js where tests would be written then require all our npm packages in the following way.
Alt Text

We have to use nock to mock every http request to our resource url. To do that add the following codes to your index.js file

const scope = nock(`https://nodejs.org`)
    .get(`/dist/index.json`)
    .reply(200, response)
Enter fullscreen mode Exit fullscreen mode

The code above means that nock will intercept all api calls to https://nodejs.org and return a response, which we had required earlier.
To get this response we create a getResponse.js file in the same directory as our index.js, then edit the file to contain the following codes.

Alt Text

The code makes an api call to our resource and stores the response in a file called response.json, this response.json is the file we required earlier on in the index.js. This is then served by nock as our response object.
Next we create a context object and fill it up so that our index.js looks exactly like

Alt Text

Our app will listen on whatever port it receives from getPort().

Now to finally test our route,
the '/latest-release' route gets the latest version of each release of nodejs.

tape('test latest release', async(t) => {
    const json = await getJSON(`${context.origin}/latest-release`)
    const v14max = json[`v14`].version
    const v13max = json[`v13`].version


    t.equal(v14max, 'v14.9.0', 'v14 should match')
    t.equal(v13max, 'v13.14.0', 'v13 should match')
    t.end()
})
Enter fullscreen mode Exit fullscreen mode

The t.equal() recieves 3 arguments (actual, expected, msg). we test the version returned from the route against the version we expect.

Finally we close all test by adding the following code to the end of our index.js
Alt Text

Run node test/index.js in your terminal and you should get the following result.
Alt Text

We've sucessfully tested a route using tape and nock in this simple tutorial. Go ahead and test many more, study more advanced tutorials and don't forget to write more tests. Please for everyone's sake write test... lol

tape npm
nock npm

Top comments (1)

Collapse
 
moonman0010 profile image
Moonman0010

where did you call getRespponce.js