The Jest Testing Framework
8 min read·Jan 1, 2025
In programming, software testing is a method to check whether the actual product (i.e. the software) matches the expected requirements.
It also ensures that the software:
- Is defect free.
- Responds correctly to all kinds of inputs.
- Performs its functions within an acceptable time.
- Runs properly in the intended environment.
- Is usable.
Although manually testing an application at each step of the development process remains the easiest and fastest method to verify that the implementation at-hand works, it is unfortunately not enough to assert that every component is defect free, or in other words, that they perform as expected and handle errors properly.
To ensure that the application is consistently functioning in a predictable manner and reduce the overhead caused by manual testing, we can automate these tests using a testing framework, such as Jest, which is a tool that provides a structured and standardized way to write and execute tests.
Installing and setting up Jest
Jest is a clean and concise JavaScript testing framework developed and maintained by Facebook.
To install Jest as a development dependency, which as a reminder is a package that is not included in the production build of the application, you can use the following command:
$ npm install --save-dev jest
Once installed, you can add the following script to your package.json file:
{
"scripts": {
"test": "jest"
}
}
Test files naming convention
By convention, Jest will treat any file with the .test.js file extension as a test file.
Even though test files can virtually be placed anywhere within your project's directory, it is recommended to keep them grouped together in an explicit directory, such as tests, usually placed at the root level of your project.
server
├── ...
├── tests
│ ├── file.test.js
│ └── ...
└── app.js
Test suites
Since projects often include several kinds of tests, such as unit tests or integration tests (which we'll cover this in the next lesson), Jest offers the possibility to run these tests in a more targeted fashion, by prefixing each test file with a keyword that describes its type using the following syntax:
<name>.<type>.test.js
For example, this filename implies that the test file contains unit tests for the signin module:
signin.unit.test.js
Running test files
To automatically run all the test files located in your project's directory, you can execute the test script using the following npm command:
$ npm run test
Running a single test file
To selectively run a single test file, you can use the following command:
$ npm run test -- <file>
Where:
--is used to indicate that following arguments should be treated as positional arguments and not options of the command.fileis the path to the file you want to run.
For example:
$ npm run test -- tests/auth/signin.test.js
Running test suites
To run a specific test suite (i.e. test files prefixed with the test type) you must first add the corresponding script to the package.json file:
{
"scripts": {
"<script_name>": "jest <test_type>"
}
}
Where:
script_nameis the name of the npm test script.test_typeis the type of test described by the specified file extension.
For example, the test-unit script will run Jest on all the files with a .unit.test.js file extension:
{
"scripts": {
"test-unit": "jest unit"
}
}
Interpreting test results
Once executed, Jest outputs a complete status report of all the tests within each test suite.
When all the tests in a test suite succeed, Jest outputs the word PASS, followed by the description of each test:
PASS ./sum.test.js
✓ it should return 4 (2 ms)
✓ it should throw an error (2 ms)
On the other hand, when either one of the tests in a test suite fails, Jest outputs the word FAIL, followed by the failed test, the reason of the failure, and the specific line that caused it:
FAIL ./sum.test.js
✓ it should return 4 (2 ms)
✕ it should throw an error
● it should throw an error
expect(received).toThrow(expected)
Expected constructor: TypeError
Received function did not throw
7 |
8 | test('it should throw an error', () => {
- 9 | expect(() => sum(1, 'a')).toThrow(TypeError);
| ^
10 | });
11 |
at Object.toThrow (sum.test.js:9:29)
Finally, Jest concludes with a test summary containing the number of test suites and individual tests ran, their status, and the overall execution time:
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.303 s
Ran all test suites.
Summary
Here's a summary of what you've learned in this lesson:
- Software testing is a method to check whether the software matches the expected requirements.
- Jest is a framework used to write and execute automated tests.
- Jest considers any file with a
.test.jsfile extension as a test file. - The
npm run testcommand is used to run all the test files present in the current working directory. - The
npm run test -- <file>command is used to run a single test file.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery