From v28 to v29

Upgrading Jest from v28 to v29? This guide aims to help refactoring your configuration and tests.

From v28 to v29 - 图1信息

See changelog for the full list of changes.

From v28 to v29 - 图2备注

Upgrading from an older version? You can see the upgrade guide from v27 to v28 here.

Compatibility

The supported Node versions are 14.15, 16.10, 18.0 and above.

Snapshot format

As announced in the Jest 28 blog post, Jest 29 has changed the default snapshot formatting to {escapeString: false, printBasicPrototype: false}.

If you want to keep the old behavior, you can set the snapshotFormat property to:

  1. + snapshotFormat: {
  2. + escapeString: true,
  3. + printBasicPrototype: true
  4. + }

JSDOM upgrade

jest-environment-jsdom has upgraded jsdom from v19 to v20.

From v28 to v29 - 图3信息

If you use jest-environment-jsdom, the minimum TypeScript version is set to 4.5.

Notably, jsdom@20 includes support for crypto.getRandomValues(), which means packages like uuid and nanoid, which doesn’t work properly in Jest@28, can work without extra polyfills.

pretty-format

ConvertAnsi plugin is removed from pretty-format package in favour of jest-serializer-ansi-escapes.

jest-mock

Exports of Mocked* utility types from jest-mock package have changed. MaybeMockedDeep and MaybeMocked now are exported as Mocked and MockedShallow respectively; only deep mocked variants of MockedClass, MockedFunction and MockedObject are exposed.

TypeScript

From v28 to v29 - 图4信息

The TypeScript examples from this page will only work as documented if you explicitly import Jest APIs:

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

Consult the Getting Started guide for details on how to setup Jest with TypeScript.

jest.mocked()

The jest.mocked() helper method now wraps types of deep members of passed object by default. If you have used the method with true as the second argument, remove it to avoid type errors:

  1. - const mockedObject = jest.mocked(someObject, true);
  2. + const mockedObject = jest.mocked(someObject);

To have the old shallow mocked behavior, pass {shallow: true} as the second argument:

  1. - const mockedObject = jest.mocked(someObject);
  2. + const mockedObject = jest.mocked(someObject, {shallow: true});