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
- jest.config.js
- ts-jest
- Jest:Mock (Jest.mock)
- Jest:Mock:Axios - How to test an axios mockup in jest.
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:
When using it with babel, also install babel-jest 1
When testing TypeScript code, also install ts-jest
Also install @types/jest to provide jest type information when writing test units
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
- ReferenceError: document is not defined · Issue #422 · testing-library/react-testing-library
- Configuring Jest · Jest # testenvironment-string
If the following error is printed:
Add the following to the jest.config.js file.
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.
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
- Node.js
- vue-i18n-jest - Testing approach combining vue i18n, jest, and vue test utils.
Favorite site
- Jest web site
- Wikipedia (en) Jest
- Writing basic tests with Jest
- Setup and teardown for tests with Jest
References
- Axios
- Axios-mock-adapter
- Babel-jest
- JavaScript:Libraries
- JavaScript:Promise
- Jest:Mock:Axios
- Next.js
- Npm:Module
- SolutionStack:FullStackDeveloper:Curriculum
- Source code
- SuperTest
- Testing Library
- Ts-jest
- TypeScript:Example:Storage
- Vue CLI
- Vue I18n
- Vue Test Utils
- Vue-i18n-jest
- Vue.js
- Vue:ProjectTemplate:Vue2 Babel ESLint TypeScript Router Vuex Sass Jest
- Vue:ProjectTemplate:Vue2 Babel ESLint TypeScript Router Vuex Sass Jest Cypress
- Vue:ProjectTemplate:Vue2 Babel ESLint TypeScript Router Vuex Sass Jest Cypress PWA
- Vue:UnitTest