Introduction
Tests are important; they provide a safeguard for your applications or APIs. As beginners, it is possible to be oblivious of the need to write tests that cover the important parts of what you are building. Yet you will meet with it as you make progress as a developer.
In a previous tutorial, you learned how to build an API with Node.js. If you have not gone through it, I suggest you do so before continuing with this. In this tutorial, you will be writing tests for an API built using Node.js and Express. At the end of this tutorial, you will know how testing works in Node.js, and you will be able to build functional and tested APIs.
Testing Tools
You will be making use of Mocha, Expect, and Supertest.
Mocha is a JavaScript test framework which makes asynchronous testing simple and fun. Mocha comes with tons of great features which can be found on the website. You will be making use of a handful of them.
Expect is an assertion library that makes it easy for you to make better assertions. You will see how that works. Supertest provides a high-level abstraction for testing HTTP. This is needful as you will be testing an API.
Enough talk, time to write some code.
Project Setup
I have a to-do list project already set up for you. The model, config file, and part of the app.js are already done for you. Go over to GitHub and clone the repository. Or you can simply do:
git clone http://ift.tt/2zEcMu6
Your package.json should look like this.
#package.json { "name": "node-todo-api", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "start": "node server/server.js", "test": "export NODE_ENV=test || SET \"NODE_ENV=test\" && mocha server/**/*.test.js", "test-watch": "nodemon --exec 'npm test'" }, "engines": { "node": "8.0.0" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "body-parser": "^1.17.2", "express": "^4.15.3", "lodash": "^4.17.4", "mongodb": "^2.2.29", "mongoose": "^4.11.1" }, "devDependencies": { "expect": "^1.20.2", "mocha": "^3.4.2", "nodemon": "^1.11.0", "supertest": "^3.0.0" } }
Now run the command to install the dependencies.
npm install
POST Request Test
For your test, create a new folder called test; this folder should be in your server directory. Now create a new file where you will write your test. Name the file server.test.js.
In this file, start by requiring the modules you installed. You also need to require your server file and your model.
#server/test/server.test.js const expect = require('expect') const request = require('supertest') const {ObjectId} = require('mongodb') const {app} = require('./../server') const {Todo} = require('./../models/todo')
You need to have a few to-dos that will be used during tests. But these to-dos will be deleted from the test database each time you run your test suite. To handle that, create two tests like so.
#server/test/server.test.js const todos = [{ _id: new ObjectId(), text: "First test todo" }, { _id: new ObjectId(), text: "Second test todo", completed: true, completedAt: 333 }] beforeEach((done) => { Todo.remove({}).then(() => { return Todo.insertMany(todos) }).then(() => done()) })
The before block cleans your Todo database, then inserts the to-dos set above. This ensures that you have a stable amount of entries in your database so your tests do not run into issues.
With that done, you can write the test for the POST request. For your POST request, you will write two tests. The first will make a request for a valid to-do and be successful. The second will make a request with an invalid body, and this should not create a new to-do.
Here is what the test should look like.
#server/test/server.test.js describe('POST /todos', () => { // 1 it('should create a new todo', (done) => { let text = 'Test todo text' // 2 request(app) // 3 .post('/todos') .send({text}) .expect(200) .expect((res) => { expect(res.body.text).toBe(text) }) .end((err, res) => { // 4 if (err) { return done(err) } Todo.find({text}).then((todos) => { // 5 expect(todos.length).toBe(1) expect(todos[0].text).toBe(text) done() }).catch((e) => done(e)) }) }) it('should not create todo with invalid body data', (done) => { // 6 request(app) // 7 .post('/todos') .send({}) .expect(400) .end((err, res) => { if (err) { return done(err) } Todo.find().then((todos) => { // 8 expect(todos.length).toBe(2) done() }).catch((e) => done(e)) }) }) })
Run the test using the command:
npm run test
It should fail. Here is what is happening:
- Describes the test.
- Creates a new to-do and saves it as a value to text.
- You make a POST request to the /todos path of your API, sending the to-do you created as the body of the request. You expect the status code for the request to be 200 and the body of the to-do to equal the value of
text
. - An error is returned if there is any, and this will cause the request to end. Thus the next block of code will not run. If no error is encountered, the flow continues.
- A request is made to the database to find the created to-do. You expect the length of the to-dos in the database to be 1, and the text of the to-do to be equal to the value of text.
- This is the test made with invalid body data.
- A POST request is made to the /todos path. This time, you are sending a request with no body. You expect to get a 400 request. No need to check the body as you are not sending any. If an error is encountered, it gets returned and the code stops running.
- If no error is encountered, a request is made to the database to check the length of the to-dos. We expect that the database will contain only 2, which are the to-dos created at the beginning.
To get this test passing, go to your server.js file and drop in the code needed for the POST request.
#server/server.js app.post('/todos', (req, res) => { let todo = new Todo({ text: req.body.text }) todo.save().then((doc) => { res.send(doc) }, (e) => { res.status(400).send(e) }) })
GET Request Test
This is simple—the test should return the length of to-dos available in the database. As you already know, the length of the todos should be 2. Why? You guessed right. At the beginning of the test suite, you created a beforeEach
block that cleans your database and inserts new to-dos each time the test suite is run.
To test that the request to get all to-dos works, here is the code for that.
#server/test/server.test.js describe('GET /todos', () => { it('should get all todos', (done) => { request(app) .get('/todos') .expect(200) .expect((res) => { expect(res.body.todos.length).toBe(2) }) .end(done) }) })
In the above, you are making a GET request to the /todos path. This time, you are not passing anything as the body of the request because it is a GET request. You expect to get a response with the status code of 200. Then you expect that the length of the to-dos will be 2.
When you run the test, you should get an error. Try getting the error to pass on your own.
I bet you got that to work. Here is the code to get the test to pass; compare it with your solution.
#server/server.js app.get('/todos', (req, res) => { Todo.find().then((todos) => { res.send({todos}) }, (e) => { res.status(400).send(e) }) })
When a GET request is made to the /todos path, you want to find every to-do in the Todo collection and return them as to-dos. Adding this to server.js causes the test to pass.
Next, you'll write three tests for the GET request made to retrieve individual to-dos. The first should retrieve and return the to-do. The second and third should return 404 error in cases where the to-do is not found.
Open up your server.test.js and create a new describe block with the first test case.
#server/server.test.js describe('GET /todos/:id', () => { it('should return todo doc', (done) => { request(app) .get(`/todos/${todos[0]._id.toHexString()}`) .expect(200) .expect((res) => { expect(res.body.todo.text).toBe(todos[0].text) }) .end(done) })
This test makes a GET request to retrieve the first to-do available in your database. You expect to get a 200 status code, and then check that the text value of the to-do is the same as the one created.
The next test looks like this.
it('should return 404 if todo is not found', (done) => { let _id = new ObjectId('5967989ee978311656e93a59') request(app) .get(`/todos/${todos/_id.toHexString()}`) .expect(404) .end(done) })
Here you are looking for a to-do using an ID that does not correspond to the ID of any to-do saved in your database. The test expects this request to return a 404 error.
The last test is like the first but a little different; here is how it looks.
it('should return 404 for non-object ids', (done) => { let hexId = '5967989ee978311656e93a5312' request(app) .get(`/todos/${todos/hexId}`) .expect(404) .end(done) }) })
Here, you create an invalid ObjectId
and try to query the database to get a to-do matching the ObjectId
created. The test expects the request to return a 404 error.
When you run the test, they should all fail. To get them to pass, add the code below to your server.js file.
#server/server.js app.get('/todos/:id', (req, res) => { let id = req.params.id // 1 if (!ObjectId.isValid(id)) { // 2 return res.status(404).send('ID is not valid') } Todo.findById(id).then((todo) => { if (!todo) { // 3 return res.status(404).send() } res.send({todo}) //4 }).catch((e) => { res.status(400).send() }) })
- Get the ID of the to-do requested from the params.
- Check to see if the ID is valid. If the ID is not valid, an error stating this is sent.
- If the ID is valid, try to find a to-do that matches that ID using the
findById
method. If no to-do is found with that ID, a 404 error is sent. - If a to-do is found, the to-do is sent. Then you catch any error that occurs and sends it.
Run the test command once more, and it should pass.
DELETE Request Test
The test for your DELETE request will be a little like what you have for your GET request.
First, you want to test that a to-do is deleted.
#server/test/server.test.js describe('DELETE /todos/:id', () => { it('should delete a todo', (done) => { let hexId = todos[0]._id.toHexString() // 1 request(app) .delete(`/todos/${hexId}`) .expect(200) .expect((res) => { expect(res.body.todo._id).toBe(hexId) }) .end((err, res) => { // 2 if (err) { return done(err) } }) Todo.findById(hexId).then((todo) => { // 3 expect(todo.hexId).toNotExist() done() }).catch((e) => done(e)) })
Here is what is happening above:
- You set the id of the to-do to a variable called
hexId
. Next, a DELETE request is made to the path of the to-do, using the ID. You expect to get a 200 response, and the to-do obtained to match the value ofhexId
. - If any error is encountered, it is returned.
- If no error, the test goes further to query your database using the ID saved as the value of
hexId
. Since a DELETE request has been previously sent, the test expects that the to-do matching that ID does not exist.
Next, you want to test that when a request is made to delete a to-do that does not exist, the response contains a 404 error. It is important to have this, as it is not possible to delete a to-do twice. Here is how the test for this should look.
#server/test/server.test.js it('should return 404 if todo is not found', (done) => { let hexId = new ObjectId().toHexString() request(app) .delete(`/todos/${todos/hexId}`) .expect(404) .end(done) })
This creates an ID for a to-do that does not exist in the database. Then a DELETE request is made to delete the to-do. The test is expected to return a 404 error as the to-do does not exist.
You want to test that a DELETE using an invalid ID returns a 404 error, like so.
#server/test/server.test.js it('should return 404 for non-object ids', (done) => { request(app) .delete('/todos/123abc') .expect(404) .end(done) }) })
To get the test to pass, open your server.js and drop this.
#server/server.js app.delete('/todos/:id', (req, res) => { let id = req.params.id if (!ObjectId.isValid(id)) { return res.status(404).send() } Todo.findByIdAndRemove(id).then((todo) => { if (!todo) { return res.status(404).send() } res.send({todo}) }).catch((e) => { res.status(400).send() }) })
Run the command to test:
npm run test
And your tests should be passing.
Conclusion
At this point, you now know how to set up a test suite when building an API using Node.js. You have used Mocha, Expect, and Supertest to test an API. One advantage of doing this is that you do not need to always fire up Postman when building your API. With your test, you can get to know what is broken.
Before we wrap up, note that JavaScript has become one of the de facto languages of working on the web. It’s not without its learning curves, and there are plenty of frameworks and libraries to keep you busy, as well. If you’re looking for additional resources to study or to use in your work, check out what we have available on Envato Market.
Using what you now know, you are good to explore the world of testing.
No comments:
Post a Comment