Skip to content

Commit aa3692d

Browse files
authored
docs: add upgrading guide (#12633)
1 parent d2f3dc6 commit aa3692d

File tree

3 files changed

+187
-1
lines changed

3 files changed

+187
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
- `[docs, examples]` Update React examples to match with the new React guidelines for code examples ([#12217](https://github.com/facebook/jest/pull/12217))
100100
- `[docs]` Add clarity for module factory hoisting limitations ([#12453](https://github.com/facebook/jest/pull/12453))
101101
- `[docs]` Add more information about how code transformers work ([#12407](https://github.com/facebook/jest/pull/12407))
102+
- `[docs]` Add upgrading guide ([#12633](https://github.com/facebook/jest/pull/12633))
102103
- `[expect]` [**BREAKING**] Remove support for importing `build/utils` ([#12323](https://github.com/facebook/jest/pull/12323))
103104
- `[expect]` [**BREAKING**] Migrate to ESM ([#12344](https://github.com/facebook/jest/pull/12344))
104105
- `[expect]` [**BREAKING**] Snapshot matcher types are moved to `@jest/expect` ([#12404](https://github.com/facebook/jest/pull/12404))

docs/UpgradingToJest28.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
id: upgrading-to-jest28
3+
title: From v27 to v28
4+
---
5+
6+
Upgrading Jest from v27 to v28? This guide aims to help refactoring your configuration and tests.
7+
8+
:::info
9+
10+
See [changelog](https://github.com/facebook/jest/blob/main/CHANGELOG.md) for the full list of changes.
11+
12+
:::
13+
14+
## Compatibility
15+
16+
The supported Node versions are 12.13, 14.15, 16.13 and above.
17+
18+
If you plan to use type definitions of Jest (or any of its packages), make sure to install TypeScript version 4.3 or above.
19+
20+
## Configuration Options
21+
22+
### `extraGlobals`
23+
24+
The `extraGlobals` option was renamed to [`sandboxInjectedGlobals`](Configuration.md#sandboxinjectedglobals-arraystring):
25+
26+
```diff
27+
- extraGlobals: ['Math']
28+
+ sandboxInjectedGlobals: ['Math']
29+
```
30+
31+
### `timers`
32+
33+
The `timers` option was renamed to [`fakeTimers`](Configuration.md#faketimers-object). See [Fake Timers](#fake-timers) section bellow for details.
34+
35+
### `testURL`
36+
37+
The `testURL` option is removed. Now you should use [`testEnvironmentOptions`](Configuration.md#testenvironmentoptions-object) to pass `url` option to JSDOM environment:
38+
39+
```diff
40+
- testURL: 'https://jestjs.io'
41+
+ testEnvironmentOptions: {
42+
+ url: 'https://jestjs.io'
43+
+ }
44+
```
45+
46+
## Fake Timers
47+
48+
Fake timers were refactored to allow passing options to the underlying [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers).
49+
50+
### `fakeTimers`
51+
52+
The `timers` configuration option was renamed to [`fakeTimers`](Configuration.md#faketimers-object) and now takes an object with options:
53+
54+
```diff
55+
- timers: 'real'
56+
+ fakeTimers: {
57+
+ enableGlobally: false
58+
+ }
59+
```
60+
61+
```diff
62+
- timers: 'fake'
63+
+ fakeTimers: {
64+
+ enableGlobally: true
65+
+ }
66+
```
67+
68+
```diff
69+
- timers: 'modern'
70+
+ fakeTimers: {
71+
+ enableGlobally: true
72+
+ }
73+
```
74+
75+
```diff
76+
- timers: 'legacy'
77+
+ fakeTimers: {
78+
+ enableGlobally: true,
79+
+ legacyFakeTimers: true
80+
+ }
81+
```
82+
83+
### `jest.useFakeTimers()`
84+
85+
An object with options now should be passed to [`jest.useFakeTimers()`](JestObjectAPI.md#jestusefaketimersfaketimersconfig) as well:
86+
87+
```diff
88+
- jest.useFakeTimers('modern')
89+
+ jest.useFakeTimers()
90+
```
91+
92+
```diff
93+
- jest.useFakeTimers('legacy')
94+
+ jest.useFakeTimers({
95+
+ legacyFakeTimers: true
96+
+ })
97+
```
98+
99+
If legacy fake timers are enabled in Jest config file, but you would like to disable them in a particular test file:
100+
101+
```diff
102+
- jest.useFakeTimers('modern')
103+
+ jest.useFakeTimers({
104+
+ legacyFakeTimers: false
105+
+ })
106+
```
107+
108+
## Test Environment
109+
110+
### Custom Environment
111+
112+
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.
113+
114+
```diff
115+
class CustomEnvironment extends NodeEnvironment {
116+
- constructor(config) {
117+
- super(config);
118+
+ constructor({globalConfig, projectConfig}, context) {
119+
+ super({globalConfig, projectConfig}, context);
120+
+ const config = projectConfig;
121+
```
122+
123+
### `jsdom`
124+
125+
If you are using JSDOM [test environment](Configuration.md#testenvironment-string), `jest-environment-jsdom` package now must be installed separately:
126+
127+
```bash npm2yarn
128+
npm install --save-dev jest-environment-jsdom
129+
```
130+
131+
## Test Runner
132+
133+
If you are using Jasmine [test runner](Configuration.md#testrunner-string), `jest-jasmine2` package now must be installed separately:
134+
135+
```bash npm2yarn
136+
npm install --save-dev jest-jasmine2
137+
```
138+
139+
## Transformer
140+
141+
`process()` and `processAsync()` methods of a custom [transformer module](CodeTransformation.md) cannot return a string anymore. They must always return an object:
142+
143+
```diff
144+
process(sourceText, sourcePath, options) {
145+
- return `module.exports = ${JSON.stringify(path.basename(sourcePath))};`;
146+
+ return {
147+
+ code: `module.exports = ${JSON.stringify(path.basename(sourcePath))};`,
148+
+ };
149+
}
150+
```
151+
152+
## TypeScript
153+
154+
:::info
155+
156+
The TypeScript examples from this page will only work as document if you import `jest` from `'@jest/globals'`:
157+
158+
```ts
159+
import {jest} from '@jest/globals';
160+
```
161+
162+
:::
163+
164+
### `jest.fn()`
165+
166+
`jest.fn()` now takes only one generic type argument. See [Mock Functions API](MockFunctionAPI.md) page for more usage examples.
167+
168+
```diff
169+
import add from './add';
170+
- const mockAdd = jest.fn<ReturnType<typeof add>, Parameters<typeof add>>();
171+
+ const mockAdd = jest.fn<typeof add>();
172+
```
173+
174+
```diff
175+
- const mock = jest.fn<number, []>()
176+
+ const mock = jest.fn<() => number>()
177+
.mockReturnValue(42)
178+
.mockReturnValueOnce(12);
179+
180+
- const asyncMock = jest.fn<Promise<string>, []>()
181+
+ const asyncMock = jest.fn<() => Promise<string>>()
182+
.mockResolvedValue('default')
183+
.mockResolvedValueOnce('first call');
184+
```

website/sidebars.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"tutorial-react",
3333
"tutorial-react-native",
3434
"testing-frameworks"
35-
]
35+
],
36+
"Upgrade Guides": ["upgrading-to-jest28"]
3637
},
3738
"api": [
3839
"api",

0 commit comments

Comments
 (0)