Last modified: Jul 21, 2025 By Alexander Williams
How to Install Jest in Node.js - Quick Guide
Jest is a popular JavaScript testing framework. It works with Node.js and browsers. It simplifies testing with built-in tools.
This guide will help you install Jest in Node.js. Follow the steps carefully for a smooth setup.
Prerequisites
Before installing Jest, ensure you have Node.js installed. You can check by running node -v
in your terminal.
node -v
v18.12.1
If Node.js is not installed, download it from the official website. Also, ensure npm (Node Package Manager) is working.
Install Jest Locally
To install Jest in your project, run the following command. This adds Jest as a dev dependency.
npm install --save-dev jest
+ [email protected]
added 1 package in 2s
Note: Using --save-dev
ensures Jest is only for development. It won't be included in production.
Configure Jest
After installation, configure Jest in your package.json
. Add a test script to run Jest easily.
{
"scripts": {
"test": "jest"
}
}
Save the file. Now you can run tests using npm test
.
Write Your First Test
Create a simple test file to verify Jest works. Name it sum.test.js
.
// sum.test.js
const sum = (a, b) => a + b;
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Run the test using npm test
. Jest will execute the test and show results.
PASS ./sum.test.js
✓ adds 1 + 2 to equal 3 (2 ms)
Advanced Configuration
For larger projects, you may need advanced Jest configuration. Create a jest.config.js
file.
// jest.config.js
module.exports = {
verbose: true,
testEnvironment: 'node',
};
This file allows customizing Jest behavior. For example, enabling verbose output.
Using TypeScript with Jest
If you use TypeScript, install @types/jest
and ts-jest
. Check our guide on installing TypeScript in Node.js.
npm install --save-dev @types/jest ts-jest
Configure Jest to work with TypeScript in jest.config.js
.
Integrating with Other Tools
Jest works well with tools like Webpack and ESLint. Ensure compatibility by checking their docs.
For React projects, Jest pairs nicely with React. It supports JSX out of the box.
Conclusion
Installing Jest in Node.js is straightforward. Follow these steps to set up testing quickly.
Jest simplifies testing with minimal configuration. It's perfect for both small and large projects.
Start writing tests today to improve your code quality. Happy testing!