Jest for JavaScript (Node.js): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Jest and why is it needed?



Jest is a powerful and popular testing framework for JavaScript code, developed by Facebook (Meta). It provides everything you need for writing unit, integration, and end-to-end tests out of the box: tools for creating assertions, mocking, spies, timers, as well as a built-in code coverage tool.



Jest is widely used in the React ecosystem, but it is also perfectly suitable for testing any JavaScript code, including applications on Node.js, Angular, Vue.js, and other frameworks.



Why is Jest needed?

  • Automation of checks: Allows you to quickly and reliably verify that your code works correctly after each change.
  • Preventing regressions: Tests help ensure that new code does not break existing functionality.
  • Documentation: Well-written tests serve as living documentation, showing how each function or module should work.
  • Confidence in refactoring: Having tests allows you to safely rewrite and improve code.
  • Ease of setup: Jest works out of the box without the need for complex configuration.


Installing Jest



Jest is installed via the package manager npm or yarn. For a Node.js project, this is the standard method.



# Installation as a dev dependency (recommended)npm install --save-dev jest

# Or using yarnyarn add --dev jest


After installation, add a script to run tests to your package.json:



{  "scripts": {    "test": "jest",    "test:watch": "jest --watch", // Run in watch mode    "test:coverage": "jest --coverage" // Run with coverage report  }}


Now you can run tests with the command npm test.



Quick Start with Code Example



Suppose we have a simple module for mathematical operations math.js:



// math.jsfunction add(a, b) {  return a + b;}

function subtract(a, b) { return a - b;}

module.exports = { add, subtract };


Let's create a test file math.test.js (Jest by default looks for files with the extension .test.js or .spec.js):



// math.test.jsconst { add, subtract } = require('./math');

test('adds 1 + 2 to equal 3', () => { expect(add(1, 2)).toBe(3);});

test('subtracts 5 - 3 to equal 2', () => { expect(subtract(5, 3)).toBe(2);});


Run the tests:



npm test


You will see a report in the console about the test results. If everything is correct, both tests will be marked with a green ✓.



Main Jest Methods and Functions



Jest provides a rich API. Let's look at 10 key methods that will cover 90% of your tasks.



1. test(name, fn, timeout) / it(name, fn, timeout)

What it does: Defines a new test. test and it are synonyms. You can use either.

Parameters:

  • name (string) — the name of the test, which will be displayed in the report.
  • fn (function) — the function containing the test logic (assertions).
  • timeout (number, optional) — the test timeout in milliseconds (default is 5000).

Example:

test('two plus two equals four', () => {  expect(2 + 2).toBe(4);});

it('the string "hello" has a length of 5', () => { expect('hello').

Recommendations