In this post, there will be an introduction in Mocha, as a test framework along with should.js and supertest. Testing supertest lets you test your HTTP assertions and testing API endpoints.This is what I learn from one of the good book called “Node.js essentials” .
The tests usually placed in the root folder
/test .Tests are completely separate from any of the source code. Well-written tests with good coverage can serve as a readme for its API since it clearly describes the behavior of the entire app. With many unit testing frameworks, Mocha uses assertion to ensure that a test runs correctly. If an error is thrown and is not handled then the test is considered to have failed. What assertion libs do is throw errors when an unexpected value is passed.Mocha provides many ways to create tests and these are called interfaces and the default is called BDD.
To ensure that mocha gets installed we need to install it globally. Run
npm install -g mocha
This is an example of basic tests in Mocha
And we will put all the test files in the folder
src/test/name-of-file.js , we will test the basic CRUD operations by creating a new
todo object, retrieve, edit and delete the
todo object. This is an example test for the creation of a new
todo is shown as follows:
We create a new POST request for a new actor to the database and then we send the request with the
.send() function. Similar tests are present for GET and DELETE requests as given in the following snippet code:
To test PUT request, we will edit the
is_done of our first todo object as follows:
To explain a bit, the first part of the test modifies the todo object and then send a PUT request for
/todos/1 (:id) and then saves the new info to the database. Then, the 2nd part of the test check whether the database entry for the todo object has been changed. The todo object is checked against their expected values using
In addition to performing CRUD actions on the todo object we can also perform these actions to the todos we add to each to . And this piece of code shows a test to add a new todo to our first todo with the id of 1.
This code test creates a new plan object and send POST request to add the plan as an array to the todo object. The second part send GET request to retrieve the todo with id which should include array of new plan
We can similarly delete the plan entries like this:
This code has the 1st part tests that send a DELETE request specifying the todo id and plan id will delete that plan entry. We make sure that the entry no longer exists by submit GET request to view the todo objects which no plans should be listed there.
In addition to ensuring that the basic CRUD operations work, we also test our schema validation. Here is how you can do it. We will make sure 2 todos with the same id do not exists since id is defined as unique
We should expect 400 status code for bad request if we try to create an todo object that already exists in the database.
Along with many testing frameworks, there are also many assertion frameworks and one of which is Chai. Instead of just using built-in assertion provided by Node.js we can use module like Chai to extend more possibilities.Chat has 3 sets of interfaces, should, expect and assert. I will give one example that cover expect. Some examples of Chai in use :
The point is when you write your tests, you want to be only be testing units of code. It could be method, provide it some input and expect an output of some kind. Then , you have to think of your app where it can not talk to the outside world.For example, how to communicate to database or make any kind of external requests.And you will probably want to mock the methods that you know it is going to execute. A great module to do this is Sinon.js and it allows you to create stubs and spies to make sure that the correct data returns from other methods and to ensure that they were called in the first place.Sinon.js provides many helpers, ex. spy. A spy is used mainly to just wrap a function to see that its input and output was. Once a spy has been applied to a function to the outside world it behaves the same
We can use a spy to check if a function is called
or what arguments were passed for each call:
Now, this is a real scenario example. Let’s say we have this methods
As getUser() will throw an error if it can not find it. And we want to test that when a user is found it returns their name. We will stub a method:
Here is some basic tests with Express.js when you implement CRUD operations with API endpoints.Hope that helps! And happy testing :D