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')
});
Running the piece of code should result in
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.
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
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
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.
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)
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.
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
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()
})
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
Run node test/index.js in your terminal and you should get the following result.
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
Top comments (1)
where did you call getRespponce.js