Skip to content
Search

Jest

lastmod 2023-07-05created 2020-09-2832 edits

Jest is a JavaScript testing framework maintained by Facebook, Inc. with a focus on simplicity. It works with projects using Babel, TypeScript, Node.js, React, Angular, Vue.js, and Svelte.

Categories

About

Since Facebook calls Jest a “testing framework” rather than just a testing library, it differs from existing JavaScript testing libraries. Before Jest, testing JavaScript code usually meant combining several testing libraries. For example, Mocha or Jasmin was used as the Test Runner, a Test Mathcher such as Chai or Expect was used, and Test Mock libraries such as Sinon and Testdouble were also needed. These libraries were very similar but had slightly different APIs, which sometimes confused JavaScript developers working across multiple projects. Jest, however, provides a Test Runner, a Test Mathcher, and even a Test Mock framework with a single library installation, so it feels quite convenient.

Installation

Install Jest:

yarn add -D jest

When using it with babel, also install babel-jest 1

yarn add -D babel-jest

When testing TypeScript code, also install ts-jest

yarn add -D ts-jest

Also install @types/jest to provide jest type information when writing test units

yarn add -D @types/jest

With webpack

Options

Isolated Modules option in TypeScript

See ts-jest#Isolated Modules option.

Basic syntax

Async test

test('the data is peanut butter', async () => {
  await expect(fetchData()).resolves.toBe('peanut butter');
});

Error Type test

test("Test description", () => {
  const t = () => {
    throw new TypeError("UNKNOWN ERROR");
  };
  expect(t).toThrow(TypeError);
  expect(t).toThrow("UNKNOWN ERROR");
});

Promise test

Use resolves.

it('works with resolves', () => {
  expect.assertions(1);
  return expect(user.getUserName(5)).resolves.toEqual('Paul');
});

For a failed Promise, use rejects instead.

// Testing for async errors using `.rejects`.
it('tests error with rejects', () => {
  expect.assertions(1);
  return expect(user.getUserName(3)).rejects.toEqual({
    error: 'User with 3 not found.',
  });
});

// Or using async/await with `.rejects`.
it('tests error with async/await and rejects', async () => {
  expect.assertions(1);
  await expect(user.getUserName(3)).rejects.toEqual({
    error: 'User with 3 not found.',
  });
});

async/await test

// async/await can be used.
it('works with async/await', async () => {
  expect.assertions(1);
  const data = await user.getUserName(4);
  expect(data).toEqual('Mark');
});

// async/await can also be used with `.resolves`.
it('works with async/await and resolves', async () => {
  expect.assertions(1);
  await expect(user.getUserName(5)).resolves.toEqual('Paul');
});

Subprocess server test

Troubleshooting

document is not defined

If the following error is printed:

ReferenceError: document is not defined

Add the following to the jest.config.js file.

{
  "testEnvironment": "jsdom"
}

Test environment jest-environment-jsdom cannot be found

Error: Test environment jest-environment-jsdom cannot be found. Make sure the testEnvironment configuration option points to an existing node module.

Install jest-environment-jsdom.

yarn add -D jest-environment-jsdom

When every type is treated as the Error type in toThrow

See JavaScript:Object#Object prototype mismatch.

TypeScript isolatedModules Error

When using jest in TypeScript, the isolatedModules-related error caused by not using import is solved as follows:

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

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

See also

Favorite site

References

author Zer0 sitemap