Testing in Node.js

Learn via video courses

Overview

Testing is a method to analyze the behavior of code. It helps to maintain the quality of code and reduces the unknown bugs in the application. It allows us to check whether the application is performing as expected. It also helps businesses to understand the risk factor for building an application. There are frameworks available for testing in nodejs: mocha, ava, tape, jasmine, and jest.

Introduction

  • In the above code block, We declared a variable testObject and assigned a value of string type. But the type of assigned value should be an object.
  • So, We will write the tests for the variable testObject and check that the type of testObject should be object.
  • Then, We fix the issue in our source code.
  • Hence, We enabled additional checkpoints to fix our source code.
    • First, We wrote test code to check the type of testObject.
    • Second, We fix our source code by assigning a value of object type to testObject.

What is Node.js Testing?

Nodejs is an open-source project and runtime environment for javascript. Structuring and executing the tests for node applications can be called nodejs testing. In this article, We will learn to test the small pieces of code, called unit testing. Unit testing helps to detect the issues early and prevent errors in our node app. Unit tests are easy to write and set up than other testing methods. The expected value must be equal to the actual result else the unit test fails.

Why is Testing Required?

Unit testing helps to detect the problems in our source code. We can run unit tests every time our source code changes. And identify whether our changed source code is working good as the previous source code. Without unit testing, We have to check a complete node app whether is working or not. Hence, unit testing reduces bugs and saves time which is good for customers as well as developers.

How to Get Started?

We already understood what is testing and why it is important? We can start testing our node app using the testing framework jest. We will create a simple calculator in nodejs and set up the testing framework jest for testing in nodejs.

Creating a Simple Node.js App

  • Create a folder named nodejs-testing and go to the folder nodejs-testing with the command line tool.
  • Initiate a node app using the command npm init -y and The npm command will create a file named package.json.
  • Open the vscode editor with the command code .

Implementing a Feature

  • We will implement a simple calculator that performs addition and multiplication.
  • We will create a new file named calculator.js and add the following code to calculator.js.

Manual Testing

  • We will run the node command node calculator.js. If the code does not throw any error, our code is good. Now, We will call the functions add() and multiply().
  • In the above code block, We are storing and printing the results of functions add() and multiply(). We will run node calculator.js and display the output as shown below.
  • The manual testing may work fine. But It will be difficult to test the code manually for thousands of lines of code. Hence, We use the testing tools to write the test cases for the nodejs application.
  • These test cases help us to verify the expected behavior of source code and keep the bugs away from our nodejs application. In the next section, We will test the calculator.js with a testing tool.

Test Framework Installation

  • We will use the test framework jest to write the first unit test for our source code.
  • jest can be Installed with the following npm command.
  • We will locate the scripts object in the file package.json and replace the test property as shown below.

Writing and Running Test for Node.js

  • Use the *.test.js or *.spec.js file extension to name the test files. Because these extensions help the test framework jest to identify the test files.
  • Create a new folder named test and a new file named calculator.test.js inside the folder test.
  • Add the following code to calculator.test.js.
  • Run the npm command npm run test and it will show the following output output npm run test command

Node.js Testing Frameworks

Mocha

  • It is a javascript test framework that helps to run test cases for testing in nodejs and browsers.
  • mocha can be installed for the current project and globally with the following npm command `
  • We set up test scripts by replacing the test property in the folder package.json as shown below.
  • Now, We will replace the code of calculator.test.js with the following code.
  • We will run the test with the npm command npm run test. The output will look like as shown below

output mocha testing framework

Chai

  • It is an assertion library for nodejs and browsers. Generally, Assertion helps to compare the expected output with the actual output and return a boolean: true or false. We can use it with any javascript test framework. chai is mostly used with the javascript test framework mocha.
  • The assertion library chai help us to write tests in plain English
  • chai can be installed with the following npm command
  • Now, We will replace the code of calculator.test.js with the following code.
  • We will run the test with the npm command npm run test. The output will look as shown below.

output chai testing framework

Note: Chai is an assertion library and not a test runner. therefore, we used mocha to run tests for the above code.

Ava

  • It is a lightweight and minimal testing framework and runs test cases for nodejs.
  • ava can be installed with the following npm command
  • We set up test scripts by replacing the test property in the folder package.json as shown below.
  • Now, We will replace the code of calculator.test.js with the following code.
  • We will run the test with the npm command npm run test. The output will look as shown below.

output ava testing framework

Jasmine

  • It is an open-source test framework. It can be run on any platform that is built for javascript.
  • jasmine can be installed with the following npm command.
  • We will run the npm command npx jasmine init and it will create a new file jasmine.json in the directory spec/support.
  • We will replace the code of jasmine.json with the following code
  • In the above code, spec_dir define the folder where we write our tests. And spec-files define the file extension that jasmine looks for while running the test.
  • We will create a new file named calculator.spec.js in the folder test/ and add the following code to calaculator.spec.js.
  • We set up test scripts by replacing the test property in the folder package.json as shown below.
  • We will run the test with the npm command npm run test. The output will look as shown below. output jasmine testing framework

Benefits of Node.js Testing

Testing helps to maintain code quality. Unit tests assure that our code is working as expected. By writing unit tests, We can reduce the chances of error and say bye-bye to surprises. Unit tests help to increase customer experience with your development project. Because unit tests eventually improve product quality. It helps to build a coverage report that tells how much part is covered by unit tests.

Anatomy of Node.js Testing

  • AAA pattern is the most common approach to writing structured test cases. The AAA pattern stands for arrange, act, and assert.
    • Arrange: It is required to set up all the things that are required before writing the tests. We can also store the expected result to a variable for a particular code that is to be tested.
    • Act: In this step, we invoke the function and store the result in a variable.
    • Assert: In this step, We verify whether the expected result matches the actual output or not.
  • We will apply the AAA pattern to calculator.spec.js and replace the code of calculator.spec.js with the following code.

Tips for Writing Node.js Tests

  • It is recommended to follow the AAA pattern while writing test cases.
  • Any test codes should not depend on any other test codes or external dependencies.
  • We must keep the test codes short.
  • There is another approach called the Happy path. It simply means that we should write the easiest test case first. If the simple test code pass, then we can cover the complex scenario later.
  • If any errors are found in the source code, it is recommended to write the test code first before resolving an issue. Because, if the same error persists again, we can detect and solve it easily.

Node.js Test Running Example

  • Now, We will test the server request and use mocha and chai for testing in nodejs.
  • Create a new nodejs app with a command line
  • Run the npm command npm init -y to initiate a node project.
  • Install mocha and chai as dev-dependencies with the following npm command
  • Create files named index.js inside the src/ folder.
  • Create files named index.test.js inside the test/ folder.
  • Set up test scripts by replacing the test property in the folder package.json as shown below.
  • We will run the test with the npm command npm run test. The output will look as shown below.

node js test running example

Conclusion

  • Testing helps us to avoid errors in a node application.
  • We can manually test the nodejs by running the source code.
  • We use the unit testing method to test the small pieces of code.
  • We use test frameworks like Mocha, Chai, Jasmine, and Jest etc for testing in nodejs.
  • Mocha is a test framework to run test cases.
  • Chai allows us to write test cases in plain English and provide various assertion options.
  • NPM pacakage chai-http allow us to test the HTTP request.