-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add upgrading guide #12633
Merged
Merged
docs: add upgrading guide #12633
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
--- | ||
id: upgrading-to-jest28 | ||
title: From v27 to v28 | ||
--- | ||
|
||
Upgrading Jest from v27 to v28? This guide aims to help refactoring your configuration and tests. | ||
|
||
:::info | ||
|
||
See [changelog](https://github.com/facebook/jest/blob/main/CHANGELOG.md) for the full list of changes. | ||
|
||
::: | ||
|
||
## Compatibility | ||
|
||
The supported Node versions are 12.13, 14.15, 16.13 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 | ||
|
||
### `extraGlobals` | ||
|
||
The `extraGlobals` option was renamed to [`sandboxInjectedGlobals`](Configuration.md#sandboxinjectedglobals-arraystring): | ||
|
||
```diff | ||
- extraGlobals: ['Math'] | ||
+ sandboxInjectedGlobals: ['Math'] | ||
``` | ||
|
||
### `timers` | ||
|
||
The `timers` option was renamed to [`fakeTimers`](Configuration.md#faketimers-object). See [Fake Timers](#fake-timers) section bellow for details. | ||
|
||
### `testURL` | ||
|
||
The `testURL` option is removed. Now you should use [`testEnvironmentOptions`](Configuration.md#testenvironmentoptions-object) to pass `url` option to JSDOM environment: | ||
|
||
```diff | ||
- testURL: 'https://jestjs.io' | ||
+ testEnvironmentOptions: { | ||
+ url: 'https://jestjs.io' | ||
+ } | ||
``` | ||
|
||
## Fake Timers | ||
|
||
Fake timers were refactored to allow passing options to the underlying [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers). | ||
|
||
### `fakeTimers` | ||
|
||
The `timers` configuration option was renamed to [`fakeTimers`](Configuration.md#faketimers-object) and now takes an object with options: | ||
|
||
```diff | ||
- timers: 'real' | ||
+ fakeTimers: { | ||
+ enableGlobally: false | ||
+ } | ||
``` | ||
|
||
```diff | ||
- timers: 'fake' | ||
+ fakeTimers: { | ||
+ enableGlobally: true | ||
+ } | ||
``` | ||
|
||
```diff | ||
- timers: 'modern' | ||
+ fakeTimers: { | ||
+ enableGlobally: true | ||
+ } | ||
``` | ||
|
||
```diff | ||
- timers: 'legacy' | ||
+ fakeTimers: { | ||
+ enableGlobally: true, | ||
+ legacyFakeTimers: true | ||
+ } | ||
``` | ||
|
||
### `jest.useFakeTimers()` | ||
|
||
An object with options now should be passed to [`jest.useFakeTimers()`](JestObjectAPI.md#jestusefaketimersfaketimersconfig) as well: | ||
|
||
```diff | ||
- jest.useFakeTimers('modern') | ||
+ jest.useFakeTimers() | ||
``` | ||
|
||
```diff | ||
- 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: | ||
|
||
```diff | ||
- jest.useFakeTimers('modern') | ||
+ jest.useFakeTimers({ | ||
+ legacyFakeTimers: false | ||
+ }) | ||
``` | ||
|
||
## Test Environment | ||
|
||
### Custom Environment | ||
|
||
The constructor of [test environment](Configuration.md#testenvironment-string) class now receives an object with Jest's `globalConfig` and `projectConfig` as its first argument. The second argument is now mandatory. | ||
|
||
```diff | ||
class CustomEnvironment extends NodeEnvironment { | ||
- constructor(config) { | ||
- super(config); | ||
+ constructor({globalConfig, projectConfig}, context) { | ||
+ super({globalConfig, projectConfig}, context); | ||
+ const config = projectConfig; | ||
``` | ||
|
||
### `jsdom` | ||
|
||
If you are using JSDOM [test environment](Configuration.md#testenvironment-string), `jest-environment-jsdom` package now must be installed separately: | ||
|
||
```bash npm2yarn | ||
npm install --save-dev jest-environment-jsdom | ||
``` | ||
|
||
## Test Runner | ||
|
||
If you are using Jasmine [test runner](Configuration.md#testrunner-string), `jest-jasmine2` package now must be installed separately: | ||
|
||
```bash npm2yarn | ||
npm install --save-dev jest-jasmine2 | ||
``` | ||
|
||
## Transformer | ||
|
||
`process()` and `processAsync()` methods of a custom [transformer module](CodeTransformation.md) cannot return a string anymore. They must always return an object: | ||
|
||
```diff | ||
process(sourceText, sourcePath, options) { | ||
- return `module.exports = ${JSON.stringify(path.basename(sourcePath))};`; | ||
+ return { | ||
+ code: `module.exports = ${JSON.stringify(path.basename(sourcePath))};`, | ||
+ }; | ||
} | ||
``` | ||
|
||
## TypeScript | ||
|
||
:::info | ||
|
||
The TypeScript examples from this page will only work as document if you import `jest` from `'@jest/globals'`: | ||
|
||
```ts | ||
import {jest} from '@jest/globals'; | ||
``` | ||
|
||
::: | ||
|
||
### `jest.fn()` | ||
|
||
`jest.fn()` now takes only one generic type argument. See [Mock Functions API](MockFunctionAPI.md) page for more usage examples. | ||
|
||
```diff | ||
import add from './add'; | ||
- const mockAdd = jest.fn<ReturnType<typeof add>, Parameters<typeof add>>(); | ||
+ const mockAdd = jest.fn<typeof add>(); | ||
``` | ||
|
||
```diff | ||
- 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') | ||
.mockResolvedValueOnce('first call'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume
@types/jest
will be updated. Something about "until@types/jest@28
is released"?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, updating
@types/jest
would be the best. I just think, if that will be fixed, we will be adding it upgrading guide. Sort-of: "you should use this version of@types/jest
". So this note will disappear naturally. Perhaps?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reasonable 👍