Jest Testing Framework CLI

30.3.0 · active · verified Sun Apr 19

Jest is a popular and delightful JavaScript testing framework known for its focus on simplicity, performance, and features like snapshot testing and built-in mocking. It offers a powerful command-line interface (CLI) to run tests, generate coverage reports, and interact with the test runner. The current stable version is 30.3.0, released after a significant three-year development cycle for version 30.0.0, with a stated aim for more frequent major releases going forward. Jest differentiates itself with an 'all-in-one' approach, providing an assertion library, test runner, and mocking capabilities out of the box, often requiring minimal configuration for many projects. It supports TypeScript out-of-the-box and is widely adopted across various JavaScript environments, including Node.js, React, Angular, and Vue projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic Jest test structure using `describe`, `test`, and `expect` with TypeScript, along with setup instructions for a minimal Jest project.

import { describe, expect, test } from '@jest/globals';

function sum(a: number, b: number): number {
  return a + b;
}

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

  test('object assignment', () => {
    const data = { one: 1 };
    data['two'] = 2;
    expect(data).toEqual({ one: 1, two: 2 });
  });

  test('null', () => {
    const n = null;
    expect(n).toBeNull();
    expect(n).toBeDefined();
    expect(n).not.toBeUndefined();
    expect(n).not.toBeTruthy();
    expect(n).toBeFalsy();
  });

  test('truthy or falsy', () => {
    const z = 0;
    const a = 1;
    expect(z).not.toBeTruthy();
    expect(a).toBeTruthy();
    expect(z).toBeFalsy();
  });
});

// To run this test:
// 1. Install Jest: npm install --save-dev jest typescript ts-jest @types/jest
// 2. Create a jest.config.ts:
//    import { defineConfig } from 'jest-config';
//    export default defineConfig({
//      preset: 'ts-jest',
//      testEnvironment: 'node',
//    });
// 3. Add to package.json scripts: "test": "jest"
// 4. Run: npm test

view raw JSON →