unit testing node js

4 min read

Unit testing with Node.js

Tomas Boccardo

Unit testing is a really important part of software development. Why? Simple, it can make the difference between a good piece of software and a mediocre one. This article then presents the what?, why? and how? of unit testing with Node.js

What?

Unit testing is a software development practice that helps the programmer gain confidence by writing and running tests for the functionality he’s building. But the main goal of unit testing is not to test the entire piece of software, but to isolate smaller components and test them separately to ensure that all the component’s requirements are fulfilled as expected.

Why?

To begin with, unit testing gives confidence that each component that’s being built is working according to the contract specified in the design. This is quite useful when making changes on existing code.

Imagine yourself jumping in a new project and having to make changes to existing functionality without tests. There is no way to be sure that what you’re changing won’t introduce bugs. Unit testing helps a lot in those cases as you can change what’s needed and run the tests to be sure that everything is working.

Also, unit testing helps the programmer separate the functionality he’s building into small modules with one single responsibility to make them easy to test. There is a development process that suggests writing the tests before writing the functionality. That way, you are forced to design your components first, write tests for them and finally implement those components that make the tests pass. That process is called Test Driven Design(TDD) and is part of a larger methodology called Extreme Programming(XP).

Unit testing serves as documentation for your code as well. They show the way a component is used and what’s the interface it exposes. Many programmers that want to use external components on their software just look at the tests to get examples on how they are used.

There is a problem with unit testing as well. It does not test interaction between components. It just tests the component on isolation. To solve that, you can use an other kind of testing called Integration Testing, but that will be covered in another article.

How?

Ok, it is already known what unit testing is about and why it’s useful. Now let’s discuss how it can be used on Node.js. First, you will need a test runner. Mocha is a good choice for this task as it provides great functionality and allows the programmer to customize the tools he will use to write the tests.

Once you have the test runner, you will need an assertion library. There are a lot out there, but I would suggest Chai. It provides both BDD and TDD style languages, so you can choose what you prefer. 

With these two tools, you can start writing simple unit tests. Here is an example of a test for the sum function.

As you can see, Mocha allows being quite descriptive with the tests names and chai provides a very easy to read interface for the assertion. This is useful, but when working with more complex components you will need a mocking library to be able to isolate your modules. For example, for testing how a callback is being called we can use Sinon.js. Here you have a little example on how this is done.

Also, there are some cases that we will need to consume external services via HTTP requests. nock is a great choice to achieve that. It’s really simple to use and provides a way to mock HTTP requests without having to worry about the method being used. Here’s an example on how it’s used

The last tool in the unit testing toolbox is Rewire, a dependency injector. It’s quite useful when you need to mock module dependencies. Let’s look at the example.

It’s been presented the Node.js Unit Testing Toolbox. You can achieve everything you need using this five tools, but there are plenty of others that can be used and get the same result. Feel free to research and choose the best for you.

Challenge everything!
About pattern image
  • Data and files
  • Confirmation

How can we help you?

Please provide information so we can contact you.
Please provide correct name
Please provide correct e-mail
Please provide correct description
Attach a file if you wish
You can upload up to 5 files. Max file size: 5MB Allowed file types: .pdf, .doc, .docx, .docm, .ppt, .pptx. File name length must be less than 50 characters File name must not contain two or more spaces in a row File name must not contain the following characters: \/?|<>:*'"+,;=[]&
    We adjusted your file name to our file naming convention. This file extension is not accepted. Please upload one of following file types: .pdf, .doc, .docx, .docm, .ppt, .pptx. This file is too large. The max upload file size is 5MB. Your file size is tiny, please check if you upload correct file.
    Please read and agree to the terms and conditions in order to continue.
    • The controller of personal data in relation to the recruitment is intive GmbH spółka z ograniczoną odpowiedzialnością Oddział w Polsce with its registered office in Warsaw, 1 Sierpnia 8, 02-134 Warsaw. More information on the principles of personal data processing, including the purposes of processing and the rights of individuals, is available in our Privacy Policy.

    Message Sent

    Thank you for your trust, {name}.
    We are looking forward to talk to you.
    Our representative will contact you.
    Dirk Heider

    VP, Project Delivery

    Oh no!

    We have run into problems while submitting your form. Please try again later.