NodeJS App testing with Jest

NodeJS


In this post we are going to test a pretty production grade NodeJS app, created in our earlier post.

Te instruction to use Jest is very well mentioned in their official website. We have also opened our previous backend code in VS Code.

Next, as per the instructions we have installed jest in our project as dev dependency.

Next, we need to add the jest.config.js file in our root directory and also notice the important testMatch should be enabled. The code for the same can be taken from the github at the end of this post.

Also, in the package.json add the test script as shown below.

We are going to test the important post endpoint of register in our auth.js file. We have also created a tests folder in our root directory.

It is very difficult to test callback functions as in above case. So, we will create a controllers folder in the root directory and create a file auth.js inside it. We have moved the callback function here and also named it authRegisterConroller.

Back in the auth.js file from the routes, we have removed the callback function. And added the function of authRegisterConroller.

Finally, we will create an auth.test.js file inside the tests folder. Here, we have first imported the desired dependencies. After that we are first mocking the User and the crypto-js with jest.mock().

After that from a describe block, we are writing the tests. Here, in the first test in the req.body we are saving a dummy username, email and password.

Next, we are creating a variable of saveMock, which will again use jest.fn(). It will save the resolved value which we will get back after save() is performed.

Next, we will use the User.mockReturnValue() to save he data from saveMock. After that we are creating a res variable, where we are returning the status and the json.

Finally, we are calling the authRegisterConroller function with req and res. We are performing our first expect on User, where it will be called once. We are also expecting it to be called with username, email and password.

Now, we will run the npm run test from the command line and it will pass.

Now, we will write some more tests. Here, we are expecting the saveMock to have been called once, the status to be 201 and also the json to be as passed.

Again running the npm run test, will pass all tests.

We will write another test for error case, when the user will not be found. Here, we are first creating the req object again. Then with User.mockReturnValue(), we are getting back the error, with the mock calls.

Again, we are creating the res object and again calling the auhRegisterConroller with req and res.

Finally, we are performing our two tests of getting a status of 500and also the message of Save error.

Now, on running the npm run test our test fails because we didn’t received the correct object.

To fix this error, we have to update the auth.js code in the controllers folder. Here, we have updated the object with message as key and err.message as value.

Now, our test runs successfully.

With this we complete our post on NodeJS testing through Jest. You can find the code for the same here.