Skip to content

Commit a501c2c

Browse files
Merge branch 'facebook:main' into eli5-video-update
2 parents 5468f74 + faef0b4 commit a501c2c

File tree

36 files changed

+413
-224
lines changed

36 files changed

+413
-224
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

CHANGELOG.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,41 @@
22

33
### Features
44

5-
- `[expect]` [**BREAKING**] Migrate to ESM ([#12344](https://github.com/facebook/jest/pull/12344))
65
- `[jest-config]` [**BREAKING**] Stop shipping `jest-environment-jsdom` by default ([#12354](https://github.com/facebook/jest/pull/12354))
76
- `[jest-config]` [**BREAKING**] Stop shipping `jest-jasmine2` by default ([#12355](https://github.com/facebook/jest/pull/12355))
87
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade jsdom to 19.0.0 ([#12290](https://github.com/facebook/jest/pull/12290))
98
- `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condition to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924))
10-
- `[jest-environment-jsdom]` [**BREAKING**] Migrate to ESM ([#12340](https://github.com/facebook/jest/pull/12340))
119
- `[jest-environment-node]` [**BREAKING**] Add default `node` and `node-addon` conditions to `exportConditions` for `node` environment ([#11924](https://github.com/facebook/jest/pull/11924))
12-
- `[jest-environment-node]` [**BREAKING**] Migrate to ESM ([#12340](https://github.com/facebook/jest/pull/12340))
1310
- `[@jest/expect-utils]` New module exporting utils for `expect` ([#12323](https://github.com/facebook/jest/pull/12323))
14-
- `[jest-jasmine2, jest-runtime]` [**BREAKING**] Use `Symbol` to pass `jest.setTimeout` value instead of `jasmine` specific logic ([#12124](https://github.com/facebook/jest/pull/12124))
15-
- `[jest-jasmine2, jest-types]` [**BREAKING**] Move all `jasmine` specific types from `@jest/types` to its own package ([#12125](https://github.com/facebook/jest/pull/12125))
1611
- `[jest-resolver]` [**BREAKING**] Add support for `package.json` `exports` ([11961](https://github.com/facebook/jest/pull/11961))
17-
- `[jest-snapshot]` [**BREAKING**] Migrate to ESM ([#12342](https://github.com/facebook/jest/pull/12342))
1812
- `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343))
1913

2014
### Fixes
2115

2216
- `[expect]` Move typings of `.not`, `.rejects` and `.resolves` modifiers outside of `Matchers` interface ([#12346](https://github.com/facebook/jest/pull/12346))
17+
- `[expect]` Expose `AsymmetricMatchers` and `RawMatcherFn` interfaces ([#12363](https://github.com/facebook/jest/pull/12363))
2318
- `[jest-environment-jsdom]` Make `jsdom` accessible to extending environments again ([#12232](https://github.com/facebook/jest/pull/12232))
24-
- `[jest-phabricator]` [**BREAKING**] Convert to ESM ([#12341](https://github.com/facebook/jest/pull/12341))
19+
- `[jest-jasmine2, jest-types]` [**BREAKING**] Move all `jasmine` specific types from `@jest/types` to its own package ([#12125](https://github.com/facebook/jest/pull/12125))
2520

2621
### Chore & Maintenance
2722

2823
- `[*]` [**BREAKING**] Drop support for Node v10 and v15 and target first LTS `16.13.0` ([#12220](https://github.com/facebook/jest/pull/12220))
2924
- `[*]` [**BREAKING**] Drop support for `[email protected]`, minimum version is now `4.2` ([#11142](https://github.com/facebook/jest/pull/11142))
3025
- `[*]` Bundle all `.d.ts` files into a single `index.d.ts` per module ([#12345](https://github.com/facebook/jest/pull/12345))
26+
- `[docs, examples]` Update React examples to match with the new React guidelines for code examples ([#12217](https://github.com/facebook/jest/pull/12217))
3127
- `[expect]` [**BREAKING**] Remove support for importing `build/utils` ([#12323](https://github.com/facebook/jest/pull/12323))
28+
- `[expect]` [**BREAKING**] Migrate to ESM ([#12344](https://github.com/facebook/jest/pull/12344))
3229
- `[jest-cli]` Update `yargs` to v17 ([#12357](https://github.com/facebook/jest/pull/12357))
3330
- `[jest-config]` [**BREAKING**] Remove `getTestEnvironment` export ([#12353](https://github.com/facebook/jest/pull/12353))
3431
- `[@jest/core]` Use `index.ts` instead of `jest.ts` as main export ([#12329](https://github.com/facebook/jest/pull/12329))
32+
- `[jest-environment-jsdom]` [**BREAKING**] Migrate to ESM ([#12340](https://github.com/facebook/jest/pull/12340))
33+
- `[jest-environment-node]` [**BREAKING**] Migrate to ESM ([#12340](https://github.com/facebook/jest/pull/12340))
3534
- `[@jest/fake-timers]` Update `@sinonjs/fake_timers` to v9 ([#12357](https://github.com/facebook/jest/pull/12357))
35+
- `[jest-jasmine2, jest-runtime]` [**BREAKING**] Use `Symbol` to pass `jest.setTimeout` value instead of `jasmine` specific logic ([#12124](https://github.com/facebook/jest/pull/12124))
36+
- `[jest-phabricator]` [**BREAKING**] Migrate to ESM ([#12341](https://github.com/facebook/jest/pull/12341))
3637
- `[jest-resolve]` [**BREAKING**] Make `requireResolveFunction` argument mandatory ([#12353](https://github.com/facebook/jest/pull/12353))
3738
- `[jest-runner]` [**BREAKING**] Remove some type exports from `@jest/test-result` ([#12353](https://github.com/facebook/jest/pull/12353))
39+
- `[jest-snapshot]` [**BREAKING**] Migrate to ESM ([#12342](https://github.com/facebook/jest/pull/12342))
3840
- `[jest-transform]` Update `write-file-atomic` to v4 ([#12357](https://github.com/facebook/jest/pull/12357))
3941
- `[jest]` Use `index.ts` instead of `jest.ts` as main export ([#12329](https://github.com/facebook/jest/pull/12329))
4042

docs/SnapshotTesting.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ A typical snapshot test case renders a UI component, takes a snapshot, then comp
1212
A similar approach can be taken when it comes to testing your React components. Instead of rendering the graphical UI, which would require building the entire app, you can use a test renderer to quickly generate a serializable value for your React tree. Consider this [example test](https://github.com/facebook/jest/blob/main/examples/snapshot/__tests__/link.test.js) for a [Link component](https://github.com/facebook/jest/blob/main/examples/snapshot/Link.js):
1313

1414
```tsx
15-
import React from 'react';
1615
import renderer from 'react-test-renderer';
1716
import Link from '../Link';
1817

docs/TutorialReact.md

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ Your `package.json` should look something like this (where `<current-version>` i
4848

4949
```js title="babel.config.js"
5050
module.exports = {
51-
presets: ['@babel/preset-env', '@babel/preset-react'],
51+
presets: [
52+
'@babel/preset-env',
53+
['@babel/preset-react', {runtime: 'automatic'}],
54+
],
5255
};
5356
```
5457

@@ -59,14 +62,14 @@ module.exports = {
5962
Let's create a [snapshot test](SnapshotTesting.md) for a Link component that renders hyperlinks:
6063

6164
```tsx title="Link.js"
62-
import React, {useState} from 'react';
65+
import {useState} from 'react';
6366

6467
const STATUS = {
6568
HOVERED: 'hovered',
6669
NORMAL: 'normal',
6770
};
6871

69-
const Link = ({page, children}) => {
72+
export default function Link({page, children}) {
7073
const [status, setStatus] = useState(STATUS.NORMAL);
7174

7275
const onMouseEnter = () => {
@@ -87,35 +90,36 @@ const Link = ({page, children}) => {
8790
{children}
8891
</a>
8992
);
90-
};
91-
92-
export default Link;
93+
}
9394
```
9495

9596
> Note: Examples are using Function components, but Class components can be tested in the same way. See [React: Function and Class Components](https://reactjs.org/docs/components-and-props.html#function-and-class-components). **Reminders** that with Class components, we expect Jest to be used to test props and not methods directly.
9697
9798
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:
9899

99100
```tsx title="Link.react.test.js"
100-
import React from 'react';
101-
import renderer from 'react-test-renderer';
102101
import Link from '../Link.react';
102+
import renderer from 'react-test-renderer';
103103

104-
test('Link changes the class when hovered', () => {
104+
it('changes the class when hovered', () => {
105105
const component = renderer.create(
106106
<Link page="http://www.facebook.com">Facebook</Link>,
107107
);
108108
let tree = component.toJSON();
109109
expect(tree).toMatchSnapshot();
110110

111111
// manually trigger the callback
112-
tree.props.onMouseEnter();
112+
renderer.act(() => {
113+
tree.props.onMouseEnter();
114+
});
113115
// re-rendering
114116
tree = component.toJSON();
115117
expect(tree).toMatchSnapshot();
116118

117119
// manually trigger the callback
118-
tree.props.onMouseLeave();
120+
renderer.act(() => {
121+
tree.props.onMouseLeave();
122+
});
119123
// re-rendering
120124
tree = component.toJSON();
121125
expect(tree).toMatchSnapshot();
@@ -125,32 +129,35 @@ test('Link changes the class when hovered', () => {
125129
When you run `yarn test` or `jest`, this will produce an output file like this:
126130

127131
```javascript title="__tests__/__snapshots__/Link.react.test.js.snap"
128-
exports[`Link changes the class when hovered 1`] = `
132+
exports[`changes the class when hovered 1`] = `
129133
<a
130134
className="normal"
131135
href="http://www.facebook.com"
132136
onMouseEnter={[Function]}
133-
onMouseLeave={[Function]}>
137+
onMouseLeave={[Function]}
138+
>
134139
Facebook
135140
</a>
136141
`;
137142

138-
exports[`Link changes the class when hovered 2`] = `
143+
exports[`changes the class when hovered 2`] = `
139144
<a
140145
className="hovered"
141146
href="http://www.facebook.com"
142147
onMouseEnter={[Function]}
143-
onMouseLeave={[Function]}>
148+
onMouseLeave={[Function]}
149+
>
144150
Facebook
145151
</a>
146152
`;
147153

148-
exports[`Link changes the class when hovered 3`] = `
154+
exports[`changes the class when hovered 3`] = `
149155
<a
150156
className="normal"
151157
href="http://www.facebook.com"
152158
onMouseEnter={[Function]}
153-
onMouseLeave={[Function]}>
159+
onMouseLeave={[Function]}
160+
>
154161
Facebook
155162
</a>
156163
`;
@@ -160,7 +167,7 @@ The next time you run the tests, the rendered output will be compared to the pre
160167

161168
The code for this example is available at [examples/snapshot](https://github.com/facebook/jest/tree/main/examples/snapshot).
162169

163-
#### Snapshot Testing with Mocks, Enzyme and React 16
170+
#### Snapshot Testing with Mocks, Enzyme and React 16+
164171

165172
There's a caveat around snapshot testing when using Enzyme and React 16+. If you mock out a module using the following style:
166173

@@ -205,9 +212,9 @@ You have to run `yarn add --dev @testing-library/react` to use react-testing-lib
205212
Let's implement a checkbox which swaps between two labels:
206213
207214
```tsx title="CheckboxWithLabel.js"
208-
import React, {useState} from 'react';
215+
import {useState} from 'react';
209216
210-
const CheckboxWithLabel = ({labelOn, labelOff}) => {
217+
export default function CheckboxWithLabel({labelOn, labelOff}) {
211218
const [isChecked, setIsChecked] = useState(false);
212219
213220
const onChange = () => {
@@ -220,13 +227,10 @@ const CheckboxWithLabel = ({labelOn, labelOff}) => {
220227
{isChecked ? labelOn : labelOff}
221228
</label>
222229
);
223-
};
224-
225-
export default CheckboxWithLabel;
230+
}
226231
```
227232
228233
```tsx title="__tests__/CheckboxWithLabel-test.js"
229-
import React from 'react';
230234
import {cleanup, fireEvent, render} from '@testing-library/react';
231235
import CheckboxWithLabel from '../CheckboxWithLabel';
232236
@@ -256,11 +260,14 @@ You have to run `yarn add --dev enzyme` to use Enzyme. If you are using a React
256260
Let's rewrite the test from above using Enzyme instead of react-testing-library. We use Enzyme's [shallow renderer](http://airbnb.io/enzyme/docs/api/shallow.html) in this example.
257261
258262
```tsx title="__tests__/CheckboxWithLabel-test.js"
259-
import React from 'react';
260-
import {shallow} from 'enzyme';
263+
import Enzyme, {shallow} from 'enzyme';
264+
import Adapter from 'enzyme-adapter-react-16';
265+
266+
Enzyme.configure({adapter: new Adapter()});
267+
261268
import CheckboxWithLabel from '../CheckboxWithLabel';
262269
263-
test('CheckboxWithLabel changes the text after click', () => {
270+
it('CheckboxWithLabel changes the text after click', () => {
264271
// Render a checkbox with label in the document
265272
const checkbox = shallow(<CheckboxWithLabel labelOn="On" labelOff="Off" />);
266273

e2e/__tests__/failures.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ test('errors after test has completed', () => {
9494
const {stderr} = runJest(dir, ['errorAfterTestComplete.test.js']);
9595

9696
expect(stderr).toMatch(
97-
/Error: Caught error after test environment was torn down/,
97+
/Error(WithStack)?: Caught error after test environment was torn down/,
9898
);
9999
expect(stderr).toMatch(/Failed: "fail async"/);
100100
});

examples/enzyme/.babelrc.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
22

33
module.exports = {
4-
presets: ['@babel/preset-env', '@babel/preset-react'],
5-
plugins: ['@babel/plugin-proposal-class-properties']
4+
presets: [
5+
'@babel/preset-env',
6+
['@babel/preset-react', {runtime: 'automatic'}],
7+
],
68
};
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
// Copyright 2004-present Facebook. All Rights Reserved.
22

3-
import React from 'react';
3+
import {useState} from 'react';
44

5-
export default class CheckboxWithLabel extends React.Component {
6-
state = {
7-
isChecked: false,
8-
};
5+
export default function CheckboxWithLabel({
6+
labelRef,
7+
inputRef,
8+
labelOn,
9+
labelOff,
10+
}) {
11+
const [isChecked, setIsChecked] = useState(false);
912

10-
onChange = () => {
11-
this.setState({isChecked: !this.state.isChecked});
13+
const onChange = () => {
14+
setIsChecked(!isChecked);
1215
};
1316

14-
render() {
15-
return (
16-
<label>
17-
<input
18-
type="checkbox"
19-
checked={this.state.isChecked}
20-
onChange={this.onChange}
21-
/>
22-
{this.state.isChecked ? this.props.labelOn : this.props.labelOff}
23-
</label>
24-
);
25-
}
17+
return (
18+
<label ref={labelRef}>
19+
<input
20+
ref={inputRef}
21+
type="checkbox"
22+
checked={isChecked}
23+
onChange={onChange}
24+
/>
25+
{isChecked ? labelOn : labelOff}
26+
</label>
27+
);
2628
}

examples/enzyme/__tests__/CheckboxWithLabel-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright 2004-present Facebook. All Rights Reserved.
22

3-
import React from 'react';
43
import Enzyme, {shallow} from 'enzyme';
54
import Adapter from 'enzyme-adapter-react-16';
65

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {expect, test} from '@jest/globals';
9+
import '../toBeWithinRange';
10+
11+
test('is within range', () => expect(100).toBeWithinRange(90, 110));
12+
13+
test('is NOT within range', () => expect(101).not.toBeWithinRange(0, 100));
14+
15+
test('asymmetric ranges', () => {
16+
expect({apples: 6, bananas: 3}).toEqual({
17+
apples: expect.toBeWithinRange(1, 10),
18+
bananas: expect.not.toBeWithinRange(11, 20),
19+
});
20+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"private": true,
3+
"version": "0.0.0",
4+
"name": "example-expect-extend",
5+
"devDependencies": {
6+
"@babel/core": "*",
7+
"@babel/preset-env": "*",
8+
"@babel/preset-typescript": "*",
9+
"@jest/globals": "workspace:*",
10+
"babel-jest": "workspace:*",
11+
"expect": "workspace:*",
12+
"jest": "workspace:*"
13+
},
14+
"scripts": {
15+
"test": "jest"
16+
},
17+
"babel": {
18+
"presets": [
19+
[
20+
"@babel/preset-env",
21+
{
22+
"targets": {
23+
"node": "current"
24+
}
25+
}
26+
],
27+
"@babel/preset-typescript"
28+
]
29+
}
30+
}

0 commit comments

Comments
 (0)