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.
4 min tiempo de lectura
Unit testing with Node.js
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
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.
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.