Guide · Open source
How to use Jest: install, write your first test and run it
Updated · written with AI from the project's own documentation and checked against our catalogue data
Jest is an open-source testing tool for JavaScript code. You write small test files that call your functions and check the results against expected values, and Jest runs them and reports which pass or fail. This guide is for developers new to Jest, and by the end you will be able to install it, write and run a test, and set up a basic configuration.
Quick start
- 1Check prerequisites. Make sure Node.js and npm or yarn are installed on your machine.
- 2Install Jest. Run npm install --save-dev jest or yarn add --dev jest in your project.
- 3Write the code to test. Create a file such as sum.js with the function you want to check.
- 4Write a test file. Create sum.test.js, call your function and use expect with a matcher such as toBe.
- 5Add a test script. Set "test": "jest" under scripts in package.json.
- 6Run the tests. Run yarn test or npm test and read the PASS or FAIL output.
Install Jest with npm or yarn
Jest is installed as a dev dependency in your project. The README shows two options: npm or yarn. Both work; the Jest documentation uses yarn commands, but npm works the same way.
You need Node.js, plus npm or yarn, installed first.
- With npm: install it as a dev dependency
- With yarn: add it as a dev dependency
npm install --save-dev jest
yarn add --dev jestWrite your first test
Start with a small function and a test file for it. Create sum.js with a function that adds two numbers and exports it.
Then create sum.test.js. This file calls the function and uses expect and toBe to check that the result is exactly 3.
- sum.js holds the code you want to test
- sum.test.js holds the test that checks it
- toBe checks that two values are exactly identical
function sum(a, b) {
return a + b;
}
module.exports = sum;
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});Add a test script and run Jest
Add a test script to the scripts section of your package.json that runs jest. Then run your test script with yarn or npm. Jest prints each test file with PASS or FAIL and lists each test result.
To learn about the other things Jest can check, read the Using Matchers page on jestjs.io.
- Add "test": "jest" under scripts in package.json
- Run yarn test or npm test
{
"scripts": {
"test": "jest"
}
}Run Jest from the command line
You can run Jest directly with the jest command if it is available in your PATH, for example after a global install. You can pass options, such as a file name pattern, a configuration file, and a native OS notification after the run.
The Jest CLI Options page on jestjs.io lists the full set of options.
- --notify displays a native OS notification after the run
- --config points to a configuration file
jest my-test --notify --config=config.jsonGenerate a basic configuration file
If you want a configuration file, Jest can create one for you. It asks a few questions about your project and writes a basic configuration file with a short description for each option.
Jest also has a dedicated configuration reference on jestjs.io for changing options later.
- Run the create command once in your project
yarn create jestUse Jest with Babel, TypeScript or a bundler
Jest works with Babel for transforming code. The README lists install steps for Babel with babel-jest, @babel/core and @babel/preset-env, and suggests a babel.config.js targeting the current Node version. Jest sets process.env.NODE_ENV to 'test' when it is not set, which you can use in your Babel config.
For TypeScript, Jest supports it via Babel using @babel/preset-typescript. Be aware that Babel does not type-check your tests as they run. If you want type checking, the README suggests ts-jest or running the TypeScript compiler tsc separately.
The README also links guides for webpack, Parcel and Vite. It notes that Jest is not fully supported by Vite, so check the limitations before you adopt that setup.
- Babel: install babel-jest, @babel/core and @babel/preset-env
- TypeScript: add @babel/preset-typescript, or use ts-jest or tsc for type checking
- webpack and Parcel: see the linked guides
- Vite: check the stated limitations first
yarn add --dev babel-jest @babel/core @babel/preset-env
yarn add --dev @babel/preset-typescriptWhat people use it for
- Checking that a utility function such as sum returns the expected value
- Adding automated tests to an existing Node-based or front-end project
- Re-running only the tests related to files you changed while editing, using watch mode
- Saving snapshots of large objects or outputs to see when they change over time
- Running tests on a TypeScript project through Babel, with type checking handled separately
Frequently asked questions
Is Jest free to use?
Yes. The repository is released under the MIT licence, as shown in the README badge and the LICENSE link.
What do I need to install Jest?
The README lists Node.js, plus npm or yarn as the package manager. You install Jest as a dev dependency in your project.
Can Jest test TypeScript?
Yes, through Babel with @babel/preset-typescript. The README notes that this does not type-check tests, so use ts-jest or tsc if you need type checking.
Does Jest do browser end-to-end testing?
The README describes Jest as a unit-style test runner and does not mention browser automation. For end-to-end browser tests, a different tool is needed.
What is the difference between Jest and Vitest?
Vitest is listed as a comparable project. This guide does not cover a detailed comparison, so check each project's documentation for differences that matter to your project.
How do I create a Jest config file?
Run yarn create jest. Jest asks a few questions based on your project and creates a basic configuration file with a description for each option.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
Jest suits JavaScript and TypeScript developers who want unit-style tests that work with little configuration, especially if they want watch mode or snapshots. If you need full browser end-to-end testing or type checking inside tests without extra tools, pick a different setup or add ts-jest or tsc alongside it.
Install options & downloads