Global web icon
jestjs.io
https://jestjs.io/
Jest · Delightful JavaScript Testing
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works with projects using: Babel, TypeScript, Node, React, Angular, Vue and more!
Global web icon
jestjs.io
https://jestjs.io/docs/getting-started
Getting Started - Jest
Let's get started by writing a test for a hypothetical function that adds two numbers. First, create a sum.js file:
Global web icon
jestjs.io
https://jestjs.io/docs/asynchronous
Testing Asynchronous Code · Jest
It's common in JavaScript for code to run asynchronously. When you have code that runs asynchronously, Jest needs to know when the code it is testing has completed, before it can move on to another test. Jest has several ways to handle this.
Global web icon
jestjs.io
https://jestjs.io/docs/tutorial-react
Testing React Apps · Jest
See React: Function and Class Components. Reminders that with Class components, we expect Jest to be used to test props and not methods directly. Now let's use React's test renderer and Jest's snapshot feature to interact with the component and capture the rendered output and create a snapshot file:
Global web icon
jestjs.io
https://jestjs.io/docs/expect
Expect - Jest
Since you are still testing promises, the test is still asynchronous. Hence, you will need to tell Jest to wait by returning the unwrapped assertion. Alternatively, you can use async/await in combination with .rejects.
Global web icon
jestjs.io
https://jestjs.io/docs/ecmascript-modules
ECMAScript Modules · Jest
import {jest, test} from '@jest/globals'; test('test esm-module', async () => { jest.unstable_mockModule('./esm-module.js', () => ({ default: () => 'default implementation', namedFn: () => 'namedFn implementation', })); const mockModule = await import('./esm-module.js'); console.log(mockModule.default()); // 'default implementation'
Global web icon
jestjs.io
https://jestjs.io/docs/testing-frameworks
Testing Web Frameworks · Jest
Jest is a universal testing platform, with the ability to adapt to any JavaScript library or framework. In this section, we'd like to link to community posts and articles about integrating Jest into popular JS libraries.
Global web icon
jestjs.io
https://jestjs.io/docs/api
Globals - Jest
In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do import {describe, expect, test} from '@jest/globals'.
Global web icon
jestjs.io
https://jestjs.io/docs/snapshot-testing
Snapshot Testing · Jest
More information on how snapshot testing works and why we built it can be found on the release blog post. We recommend reading this blog post to get a good sense of when you should use snapshot testing. We also recommend watching this egghead video on Snapshot Testing with Jest.
Global web icon
jestjs.io
https://jestjs.io/docs/setup-teardown
Setup and Teardown - Jest
If a test is failing, one of the first things to check should be whether the test is failing when it's the only test that runs. To run only one test with Jest, temporarily change that test command to a test.only: