From v27 to v28
info
See changelog for the full list of changes.
The supported Node versions are 12.13, 14.15, 16.10 and above.
If you plan to use type definitions of Jest (or any of its packages), make sure to install TypeScript version 4.3 or above.
Configuration Options
The extraGlobals
option was renamed to sandboxInjectedGlobals:
timers
The timers
option was renamed to fakeTimers. See section below for details.
testURL
The testURL
option is removed. Now you should use to pass url
option to JSDOM environment:
- testURL: 'https://jestjs.io'
+ testEnvironmentOptions: {
+ url: 'https://jestjs.io'
+ }
expect
In versions prior to Jest 28, toHaveProperty
checked for equality instead of existence, which means that e.g. expect({}).toHaveProperty('a', undefined)
is a passing test. This has been changed in Jest 28 to fail.
Additionally, if you import expect
directly, it has been changed from default export to a named export.
- import expect from 'expect';
+ import {expect} from 'expect';
+ const {expect} = require('expect');
Fake timers were refactored to allow passing options to the underlying .
fakeTimers
The configuration option was renamed to and now takes an object with options:
- timers: 'real'
+ fakeTimers: {
+ enableGlobally: false
+ }
- timers: 'fake'
+ fakeTimers: {
+ enableGlobally: true
+ }
- timers: 'legacy'
+ fakeTimers: {
+ enableGlobally: true,
+ legacyFakeTimers: true
+ }
jest.useFakeTimers()
An object with options now should be passed to as well:
- jest.useFakeTimers('modern')
+ jest.useFakeTimers()
- jest.useFakeTimers('legacy')
+ jest.useFakeTimers({
+ legacyFakeTimers: true
+ })
If legacy fake timers are enabled in Jest config file, but you would like to disable them in a particular test file:
- jest.useFakeTimers('modern')
+ jest.useFakeTimers({
+ legacyFakeTimers: false
+ })
Test Environment
class CustomEnvironment extends NodeEnvironment {
- constructor(config) {
+ constructor({globalConfig, projectConfig}, context) {
+ super({globalConfig, projectConfig}, context);
+ const config = projectConfig;
jsdom
If you are using JSDOM test environment, package now must be installed separately:
- npm
- Yarn
yarn add --dev jest-environment-jsdom
Test Runner
If you are using Jasmine test runner, jest-jasmine2
package now must be installed separately:
- npm
- Yarn
npm install --save-dev jest-jasmine2
yarn add --dev jest-jasmine2
process()
and processAsync()
methods of a custom cannot return a string anymore. They must always return an object:
process(sourceText, sourcePath, options) {
- return `module.exports = ${JSON.stringify(path.basename(sourcePath))};`;
+ return {
+ code: `module.exports = ${JSON.stringify(path.basename(sourcePath))};`,
+ };
}
package.json
exports
Jest now includes full support for , which might mean that files you import are not resolved correctly. Additionally, Jest now supplies more conditions. jest-environment-node
has node
and node-addons
, while jest-environment-jsdom
has browser
. As a result, you might e.g. get browser code which assumes ESM, when Jest provides ['require', 'browser']
. You can either report a bug to the library (or Jest, the implementation is new and might have bugs!) or override the conditions Jest passes.
TypeScript
info
The TypeScript examples from this page will only work as document if you import jest
from '@jest/globals'
:
import {jest} from '@jest/globals';
jest.fn()
jest.fn()
now takes only one generic type argument. See page for more usage examples.
- const mock = jest.fn<number, []>()
+ const mock = jest.fn<() => number>()
.mockReturnValue(42)
.mockReturnValueOnce(12);
- const asyncMock = jest.fn<Promise<string>, []>()
+ const asyncMock = jest.fn<() => Promise<string>>()
.mockResolvedValue('default')