One of the most critical aspects of software development is ensuring your application operates as intended. As your apps grow more complex with added features and functionality, the probability of software bugs creeping in also amplifies.
Manually testing every aspect of your application could be laborious, time-consuming, and might not be the most reliable approach to ensure optimum program functioning. Hence, adopting a systematic testing approach can greatly enhance the reliability of your software over manual testing. Unit testing is one such method that validates individual components of your software work as intended.
This guide will get you up and running with unit testing in Node.js using Jest, a JavaScript testing framework that prioritizes simplicity.
Unit Testing Basics
Unit testing involves isolating and testing individual components or ‘units’ of your software application. The objective is to ensure every individual unit of code works as intended, enabling you to catch potential hitches early in the development process.
Getting Started With Jest in Node.js
Before you can get cracking with writing unit tests using Jest, you need to install it as a dependency in your Node.js project and set up the requisite test files.
Installation
To install Jest, run:
npm install --save-dev jest
After successful installation, create a folder for your Jest test files:
mkdir test
You can create a test file using the following command:
touch example.test.js
Additionally, update your package.json
file to include:
"scripts": { "test": "jest" }
This script enables you to trigger all your test suites with a single command.
Writing Tests Using Jest
Consider an isEven
function:
function isEven(number) {
if (number < 0) {
throw new Error("Number must be positive");
}
if (typeof number !== "number") {
throw new Error("Number must be a number");
}
return number % 2 === 0;
}
This function throws exceptions for negative numbers and non-numeric inputs and labels fractions as non-even.
To write a test suite for this function, import it into your test file and use Jest’s API to create tests.
const isEven = require("../index");
describe("isEven", () => {
test("returns true for even numbers", () => {
expect(isEven(2)).toBe(true);
});
/*...Additional tests...*/
});
You can run all your test suites using the command:
npm test
All your tests should ideally pass, affirming your function’s correctness.
Testing Asynchronous Code with Jest
In JavaScript, some operations such as API requests, timers or file read/write operations are asynchronous and take time to complete. Jest tests complete as soon as they reach the end of their execution path. For asynchronous tasks, you need to ensure Jest waits for them to complete before concluding the test.
You may encounter false negatives when testing asynchronous code. Jest provides a few techniques to tackle these.
Mocking with Jest
A commonly used practice in writing tests is mocking, where you replace a function or module dependency with a ‘fake’ or ‘mock’ function you have control over during the test. You can systematically structure the behavior of your mocks to mimic that of real functions.
This can be specifically handy when testing code that depends on external services or databases. By mocking, you can run your tests without needing to interact with these external services.
Let’s consider the function getUser(userId)
that interacts with an external database to fetch a user’s data:
async function getUser(userId) {
const user = await database.fetchUser(userId);
return user;
}
You can mock the database.fetchUser()
function which interacts with the external database. Jest allows you to replace the real function with a mock function that simulates the actual function:
jest.mock('./database');
database.fetchUser.mockReturnValue({
id: '123',
name: 'John'
});
const user = user.getUser('123');
expect(user).toBe({
id: '123',
name: 'John'
});
TDD vs BDD in Jest
Jest also empowers you to follow two popular approaches to software testing; Test-Driven Development (TDD) and Behavior-Driven Development (BDD).
TDD involves first writing a failing test case for a small unit of code, then writing the minimal amount of code to pass that test case. Once the test passes, you refactor the code as necessary and repeat the process for the next test case.
BDD, on the other hand, structures tests as ‘scenarios’ that reflect the expected software behavior from the user’s perspective. BDD tests often utilize a more comprehensible, natural language syntax so non-technical team members can also understand them.
Both TDD and BDD have their own strengths and you can choose either approach based on your project’s needs and your personal preference.
Jest vs. Mocha: Which One to Choose?
While Jest is simple to use and quite powerful, it’s not the only testing framework available. Mocha is another equally competent testing framework and might maybe a better fit for your needs in some cases.
Below is a brief comparison of the two:
| | Jest | Mocha |
|——-|——–|———|
| Configuration | Comes preconfigured | Requires manual configuration |
| Assertion Library | Built-in | Requires external libraries like Chai |
| Mocking | Built-in | Requires external libraries like Sinon |
| Performance | Faster out-of-the-box, parallelization | Can be optimized for speed using plugins |
Your choice between Jest and Mocha would depend on your project’s requirements and your personal preference.
To summarize, we covered how to perform unit testing in Node.js using Jest, create and run test suites, and use Jest’s mocking features. You also learned about TDD and BDD testing patterns, and how to use both with Jest. Finally, we compared Jest with Mocha to give you a wider array of testing frameworks to choose from.
Happy testing!
Tags: #Jest, #Nodejs, #UnitTesting, #TDD, #BDD