Testing Express routes with Tape and Nock
Tags: testing, node, javascript, express
Published: 2020-09-04 20:21:35

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.
```javascript
var test = require('tape');

test('addition test', () => {
   t.equal(1 + 1, 2, '1+1=2')
});
```
Running the piece of code should result in 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yut57jgy9n4crs08epxb.jpg)

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](https://github.com/oviecodes/testingRouteTape.git)
clone the repo to follow along with the tutorial because we'll be working in the test directory here.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zif0d1djcotepu5yaf30.jpg)

In the code snippet above we export the app object so we can run our test on it.
run
```javascript
npm install tape nock get-port -D
```
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.

```javascript
npm install bent --save
```
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](https://dev-to-uploads.s3.amazonaws.com/i/rq73d89pfkxgaqbg33zm.jpg)

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
```javascript
const scope = nock(`https://nodejs.org`)
    .get(`/dist/index.json`)
    .reply(200, response)
```
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](https://dev-to-uploads.s3.amazonaws.com/i/cnkduf35drkss4ui5aix.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/i/5i1ofnggt1h85xruai22.jpg)

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.

```javascript
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()
})
```
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](https://dev-to-uploads.s3.amazonaws.com/i/wiyfn93s3h8yum2x5um0.jpg)

Run node test/index.js in your terminal and you should get the following result.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/arizc2849sxkkdatk0tt.jpg)

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](https://www.npmjs.com/package/tape)
[nock npm](https://www.npmjs.com/package/nock)