Jest tutorial shows how to use Jest framework to perform unit testing in JavaScript applications.
Jest
Jest JavaScript resting framework with a focus on simplicity. Jest was created by Facebook engineers for its React project.
Unit testing is a software testing where individual units (components) of a software are tested. The purpose of unit testing is to validate that each unit of the software performs as designed. A unit is the smallest testable part of any software.
Mocking is technique where code parts are replaced by dummy implementations that emulate real code. Mocking helps achieve isolation of tests. Mocking is primarily used in unit testing.
In our tests we check that values meet certain conditions. The expect() function gives us a number of matchers that let us validate different things, such as toBe(), toBeFalsy(), or toEqual().
In this tutorial we work with Jest in a Node application.
By default, jest only gives a rudimentary output. To get more information about test runs, we use the --verbose flag.
Jest running tests
Tests are run with npm test command. The test files must have the test term in their names.
$ npm test
> jest-test@1.0.0 test C:\Users\Jano\Documents\js\jest-test
> jest
PASS ./math-utils.test.js
PASS ./arith.test.js
PASS ./arith-params.test.js
PASS ./arith-skip.test.js
PASS ./string-utils.test.js
PASS ./arith-mock.test.js
PASS ./users.test.js
Test Suites: 7 passed, 7 total
Tests: 2 skipped, 35 passed, 37 total
Snapshots: 0 total
Time: 5.19s
Ran all test suites.
This is an sample output running tests with Jest. This is a terse output. For more information, we can use the --verbose option.
To run an individual test, we can use the npx jest testname command.
We can configure Jest to run tests in a specified test directory.
Testing arithmetic functions with Jest
The following is a classic scholarly example for demostrating unit testing with Jest.arith.js
const add = (a, b) => a + b;
const mul = (a, b) => a * b;
const sub = (a, b) => a - b;
const div = (a, b) => a / b;
module.exports = { add, mul, sub, div };
We have four basic arithmetic functions in a module.arith.test.js
We test the add() method with test() function. The first parameter is the name of the test, the second parameter is the function to be run. We are testing that the add() function returns correct answer for sample data.
$ npx jest arith.test.js
PASS ./arith.test.js
√ 2 + 3 = 5 (3ms)
√ 3 * 4 = 12 (6ms)
√ 5 - 6 = -1
√ 8 / 4 = 2 (1ms)
Test Suites: 1 passed, 1 total
Tests: 4 passed, 4 total
Snapshots: 0 total
Time: 10.981s
Ran all test suites matching /arith.test.js/i.
This is the output.
Jest skipping tests
Tests may take considerable time to finish. We can skip some tests if needed.arith-skip.test.js
The each() method receives an array of arrays with the arguments that are passed into the test function for each row. The %i are format specifiers that expect integers. This is for output that is shown with --verbose option.
The beforeAll() function is part of a test setup. It runs a function before any of the tests in this file run. If the function returns a promise or is a generator, Jest waits for that promise to resolve before running tests.math-utils.js
const sum = (vals) => {
let sum = 0;
vals.forEach((val) => {
sum += val;
});
return sum;
}
const positive = (vals) => {
return vals.filter((x) => { return x > 0; });
}
const negative = (vals) => {
return vals.filter((x) => { return x < 0; });
}
module.exports = { sum, positive, negative };
We have a math-utils module, which contains three functions: sum(), positive(), and negative().math-utils.test.js
const { sum, positive, negative } = require('./math-utils');
let vals;
let sum_of_vals;
let pos_vals;
let neg_vals;
beforeAll(() => {
pos_vals = [2, 1, 3];
neg_vals = [-2, -1, -1];
vals = pos_vals.concat(neg_vals);
sum_of_vals = vals.reduce((x, y) => x + y, 0);
})
test('the sum of vals should be 2', () => {
expect(sum(vals)).toBe(sum_of_vals);
});
test('should get positive values', () => {
expect(positive(vals)).toEqual(pos_vals);
});
test('should get negative values', () => {
expect(negative(vals)).toEqual(neg_vals);
});
In the test file, we use the beforeAll() function to initialize test data before the tests are run.
test('should get positive values', () => {
expect(positive(vals)).toEqual(pos_vals);
});
To test the positive() function, we use the toEqual() matcher. We test that the function returns an array of positive values equal to the predefined test array of values.
Jest grouping tests
In Jest, tests are grouped into units with describe(). It creates a block that groups together several related tests.string-utils.js
We have string-utils.js module with two functions: isPalindrome() and isAnagram().math-utils.js
const sum = (vals) => {
let sum = 0;
vals.forEach((val) => {
sum += val;
});
return sum;
}
const positive = (vals) => {
return vals.filter((x) => { return x > 0; });
}
const negative = (vals) => {
return vals.filter((x) => { return x < 0; });
}
module.exports = { sum, positive, negative };
We have again the math-utils.js module.groups.test.js
const { sum, positive, negative } = require('./math-utils');
const { isPalindrome, isAnagram } = require('./string-utils');
describe('testing math utilities', () => {
let vals;
let sum_of_vals;
let pos_vals;
let neg_vals;
beforeAll(() => {
pos_vals = [2, 1, 3];
neg_vals = [-2, -1, -1];
vals = pos_vals.concat(neg_vals);
sum_of_vals = vals.reduce((x, y) => x + y, 0);
})
test('the sum of vals should be 2', () => {
expect(sum(vals)).toBe(sum_of_vals);
});
test('should get positive values', () => {
expect(positive(vals)).toEqual(pos_vals);
});
test('should get negative values', () => {
expect(negative(vals)).toEqual(neg_vals);
});
});
describe('testing string utilities', () => {
test.each(["racecar", "radar", "level", "refer", "deified", "civic"])(
'testing %s for palindrome', (word) => {
expect(isPalindrome(word)).toBeTruthy();
},
);
test.each([["arc", "car"], ["cat", "act"], ["cider", "cried"]])(
'testing if %s and %s are anagrams ', (word1, word2) => {
expect(isAnagram(word1, word2)).toBeTruthy();
},
);
});
With describe(), we have created two isolated test groups for string and math utilities. For instance, the beforeAll() is only applied for the math utilities.
$ npx jest groups.test.js
PASS ./groups.test.js
testing math utilities
√ the sum of vals should be 2 (3ms)
√ should get positive values (1ms)
√ should get negative values
testing string utilities
√ testing racecar for palindrome (1ms)
√ testing radar for palindrome
√ testing level for palindrome
√ testing refer for palindrome
√ testing deified for palindrome (1ms)
√ testing civic for palindrome
√ testing if arc and car are anagrams
√ testing if cat and act are anagrams
√ testing if cider and cried are anagrams (1ms)
Test Suites: 1 passed, 1 total
Tests: 12 passed, 12 total
Snapshots: 0 total
Time: 1.786s
Ran all test suites matching /groups.test.js/i.
We run the tests.
Jest testing Axios
In the following section, we test JavaScript code that uses Axios library. In the beginning, we have installed axios and json-server modules.users.json
$ npx jest users.test.js
PASS ./users.test.js
√ should fetch users (4ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.818s
Ran all test suites matching /users.test.js/i.
We run the test.
In this tutorial, we have used Jest to do unit testing in JavaScript applications.