#AD Top Online Web Development & Programming Courses | Udemy

Introduction to unit testing in JavaScript with Jest

Last modified April 1st 2022 | #js

In this tutorial you’ll get an introduction to unit testing in JavaScript using Jest. If you’re unfamiliar with unit testing it’s simply a type of testing that is performed on individual components or modules, and Jest is a framework that simplifies the process of writing these tests.

Let’s get started by installing Jest via NPM:

npm install jest --save-dev

Once installed edit the scripts section of your package.json file to include jest:

"scripts": { // other scripts... "test": "jest" },
Code language: JavaScript (javascript)

Next we’ll create a simple function four our first unit test:

function isAdmin(username) { if (username === "w3collective") { return true; } else { return false; } } module.exports = isAdmin;
Code language: JavaScript (javascript)

Jest will perform the tests in any files that include test in the filename before the .js extension. As we’ll be creating a test for code in the index.js file let’s go ahead and create a test file called index.test.js with the following test:

const isAdmin = require('./index'); test('is the username an admin', () => { const username = "w3collective"; expect(isAdmin(username)).toBeTruthy(); });
Code language: PHP (php)

This test expects the result of the function to be true (toBeTruthy), this is known as a matcher in Jest. There is a range of different matchers available depending on the type of data you are testing (strings, numbers, arrays, etc…). You can view the full list of matchers available in the official Jest documentation here.

We can now run the test using the following command:

npm run test

You should see the following message indicating the test passed:

PASS ./index.test.js ✓ is the username an admin (2 ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 0.365 s, estimated 1 s Ran all test suites.
Code language: Bash (bash)

Organising tests into suites

To help organise larger projects tests can be grouped into suites. A suite is a collection of tests that collectively test a bigger unit of functionality. For example we could create a suite that groups all modules that work with user data.

Let’s create another function to our index.js file to include in a suite:

function getUser(id) { if (id === 1) { return "w3collective"; } else if (id === 2) { return "johnsmith"; } else { return null; } } function isAdmin(username) { if (username === "w3collective") { return true; } else { return false; } } module.exports = { getUser, isAdmin, };
Code language: JavaScript (javascript)

We create a test suite by using describe as follows:

const { getUser, isAdmin } = require("./index"); describe("users", () => { test("does a user with id exist", () => { expect(getUser(1)).toBeDefined(); }); test("is the username an admin", () => { expect(isAdmin("w3collective")).toBeTruthy(); }); });
Code language: PHP (php)

Once again run the test and you’ll see that 1 test suite with 2 tests passed.

That’s all for this tutorial, hopefully it has inspired to start unit testing your own projects. This was just an introduction to using Jest, we’ll be adding a more tutorials with more complex test scenarios in the future.

Related Posts

#AD Shop Web Developer T-Shirts