Skip to main content
Course/Module 6/Topic 1 of 4Advanced

Unit Testing with Jest

Learn unit testing with Jest for Node.js applications

45 minBy Priygop TeamLast updated: Feb 2026

Jest Testing Framework

Jest is a popular JavaScript testing framework that provides a complete testing solution for Node.js applications. It includes test runners, assertion libraries, mocking capabilities, and code coverage.

Jest Setup and Configuration

Example
// Install Jest
npm install --save-dev jest

// package.json
{
  "scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
    "test:coverage": "jest --coverage"
  },
  "jest": {
    "testEnvironment": "node",
    "collectCoverageFrom": [
      "src/**/*.js",
      "!src/**/*.test.js"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 80,
        "functions": 80,
        "lines": 80,
        "statements": 80
      }
    }
  }
}

// Basic test example
// src/utils/calculator.test.js
const { add, subtract, multiply, divide } = require('./calculator');

describe('Calculator', () => {
  test('should add two numbers correctly', () => {
    expect(add(2, 3)).toBe(5);
    expect(add(-1, 1)).toBe(0);
  });

  test('should subtract two numbers correctly', () => {
    expect(subtract(5, 3)).toBe(2);
    expect(subtract(0, 5)).toBe(-5);
  });

  test('should handle division by zero', () => {
    expect(() => divide(5, 0)).toThrow('Division by zero');
  });
});
Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep