Unit testing involves testing individual components or functions of a software application in isolation. The goal is to ensure that each unit behaves as expected and produces the correct output for a given set of inputs. In JavaScript, a unit could be a simple function, a class method, or a module.
Jest is a JavaScript testing framework that provides a complete solution for writing and running tests. It comes with built - in support for test runners, assertion libraries, mocking, and code coverage analysis. Some of the key features of Jest include:
To use Jest in your JavaScript project, you first need to install it as a development dependency. If you are using npm, you can run the following command in your project directory:
npm install --save - dev jest
After installation, you need to add a test script to your package.json
file. Open the package.json
file and add the following line to the scripts
section:
{
"scripts": {
"test": "jest"
}
}
Now you can run your tests using the command npm test
.
Let’s start by writing a simple function and a test for it. Create a file named math.js
with the following content:
// math.js
function sum(a, b) {
return a + b;
}
module.exports = { sum };
Next, create a test file named math.test.js
in the same directory:
// math.test.js
const { sum } = require('./math');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
In this example, the test
function is provided by Jest. It takes two arguments: a description of the test and a callback function that contains the actual test code. The expect
function is used to create an expectation, and the toBe
method is an assertion that checks if the result of the sum
function is equal to the expected value.
Run the test using the command npm test
. If everything is set up correctly, you should see an output indicating that the test has passed.
You can group related tests together using the describe
function. For example, let’s say we have multiple functions in our math.js
file:
// math.js
function sum(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = { sum, subtract };
We can group the tests for these functions in the math.test.js
file:
// math.test.js
const { sum, subtract } = require('./math');
describe('Math functions', () => {
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
test('subtracts 2 from 5 to equal 3', () => {
expect(subtract(5, 2)).toBe(3);
});
});
The describe
function helps in organizing the tests and provides a better structure for the test suite.
Jest provides support for testing asynchronous code. For example, if you have a function that returns a promise:
// asyncMath.js
function asyncSum(a, b) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(a + b);
}, 100);
});
}
module.exports = { asyncSum };
You can test it using the async/await
syntax in Jest:
// asyncMath.test.js
const { asyncSum } = require('./asyncMath');
test('async adds 1 + 2 to equal 3', async () => {
const result = await asyncSum(1, 2);
expect(result).toBe(3);
});
Each test should be independent of other tests. This means that the outcome of one test should not affect the outcome of another. If a test depends on the state or side - effects of another test, it can lead to flaky tests that are difficult to debug.
Use descriptive names for your tests. A good test name should clearly describe what the test is supposed to do. For example, instead of naming a test test1
, use a name like testSumFunctionWithPositiveNumbers
.
Mocking is a technique used to replace parts of the system under test with mock objects. This can be useful when testing functions that depend on external services or have side - effects. Jest provides built - in support for mocking. For example, if you have a function that makes an HTTP request:
// api.js
const axios = require('axios');
async function getData() {
const response = await axios.get('https://example.com/api/data');
return response.data;
}
module.exports = { getData };
You can mock the axios.get
function in your test:
// api.test.js
const axios = require('axios');
const { getData } = require('./api');
jest.mock('axios');
test('getData should return mocked data', async () => {
const mockData = { data: 'mocked data' };
axios.get.mockResolvedValue(mockData);
const result = await getData();
expect(result).toBe(mockData.data);
});
Jest is a powerful and easy - to - use testing framework for JavaScript. It provides a wide range of features that make it suitable for testing various types of JavaScript applications. By following the fundamental concepts, common practices, and best practices outlined in this blog, you can write effective unit tests that help in ensuring the quality and reliability of your JavaScript code.