Skip to content

Commit d7aacd9

Browse files
authored
feat(examples): replace react-test-renderer reference with @testing-library/react for snapshot testing (#15806)
1 parent b097e43 commit d7aacd9

File tree

14 files changed

+176
-260
lines changed

14 files changed

+176
-260
lines changed

docs/SnapshotTesting.md

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ A typical snapshot test case renders a UI component, takes a snapshot, then comp
99

1010
## Snapshot Testing with Jest
1111

12-
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/jestjs/jest/blob/main/examples/snapshot/__tests__/link.test.js) for a [Link component](https://github.com/jestjs/jest/blob/main/examples/snapshot/Link.js):
12+
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 component. Consider this [example test](https://github.com/jestjs/jest/blob/main/examples/snapshot/__tests__/link.test.js) for a [Link component](https://github.com/jestjs/jest/blob/main/examples/snapshot/Link.js):
1313

1414
```tsx
15-
import renderer from 'react-test-renderer';
15+
import {render} from '@testing-library/react';
1616
import Link from '../Link';
1717

1818
it('renders correctly', () => {
19-
const tree = renderer
20-
.create(<Link page="http://www.facebook.com">Facebook</Link>)
21-
.toJSON();
22-
expect(tree).toMatchSnapshot();
19+
const {container} = render(
20+
<Link page="http://www.facebook.com">Facebook</Link>,
21+
);
22+
expect(container.firstChild).toMatchSnapshot();
2323
});
2424
```
2525

@@ -28,10 +28,9 @@ The first time this test is run, Jest creates a [snapshot file](https://github.c
2828
```javascript
2929
exports[`renders correctly 1`] = `
3030
<a
31-
className="normal"
31+
aria-label="normal
32+
class="normal"
3233
href="http://www.facebook.com"
33-
onMouseEnter={[Function]}
34-
onMouseLeave={[Function]}
3534
>
3635
Facebook
3736
</a>
@@ -61,10 +60,10 @@ One such situation can arise if we intentionally change the address the Link com
6160
```tsx
6261
// Updated test case with a Link to a different address
6362
it('renders correctly', () => {
64-
const tree = renderer
65-
.create(<Link page="http://www.instagram.com">Instagram</Link>)
66-
.toJSON();
67-
expect(tree).toMatchSnapshot();
63+
const {container} = render(
64+
<Link page="http://www.instagram.com">Instagram</Link>,
65+
);
66+
expect(container.firstChild).toMatchSnapshot();
6867
});
6968
```
7069

@@ -112,26 +111,25 @@ First, you write a test, calling `.toMatchInlineSnapshot()` with no arguments:
112111

113112
```tsx
114113
it('renders correctly', () => {
115-
const tree = renderer
116-
.create(<Link page="https://example.com">Example Site</Link>)
117-
.toJSON();
118-
expect(tree).toMatchInlineSnapshot();
114+
const {container} = render(
115+
<Link page="https://example.com">Example Site</Link>,
116+
);
117+
expect(container.firstChild).toMatchInlineSnapshot();
119118
});
120119
```
121120

122121
The next time you run Jest, `tree` will be evaluated, and a snapshot will be written as an argument to `toMatchInlineSnapshot`:
123122

124123
```tsx
125124
it('renders correctly', () => {
126-
const tree = renderer
127-
.create(<Link page="https://example.com">Example Site</Link>)
128-
.toJSON();
129-
expect(tree).toMatchInlineSnapshot(`
125+
const {container} = render(
126+
<Link page="https://example.com">Example Site</Link>,
127+
);
128+
expect(container.firstChild).toMatchInlineSnapshot(`
130129
<a
130+
aria-label="normal"
131131
className="normal"
132132
href="https://example.com"
133-
onMouseEnter={[Function]}
134-
onMouseLeave={[Function]}
135133
>
136134
Example Site
137135
</a>

e2e/__tests__/toMatchInlineSnapshotWithJSX.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ const babelConfig = {
2727

2828
const pkg: PackageJson = {
2929
dependencies: {
30-
react: '^17.0.0',
30+
react: '^18.0.0',
31+
'react-dom': '^18.0.0',
3132
},
3233
devDependencies: {
3334
'@babel/core': '^7.14.4',
3435
'@babel/preset-env': '^7.14.4',
3536
'@babel/preset-react': '^7.13.13',
36-
'react-test-renderer': '^17.0.2',
37+
'@testing-library/dom': '^10.4.1',
38+
'@testing-library/react': '^16.3.0',
3739
},
3840
jest: {
3941
testEnvironment: 'jsdom',
@@ -48,10 +50,10 @@ beforeEach(() => {
4850
writeFiles(DIR, {
4951
'__tests__/MismatchingSnapshot.test.js': `
5052
import React from 'react';
51-
import renderer from 'react-test-renderer';
53+
import {render} from '@testing-library/react';
5254
5355
test('<div>x</div>', () => {
54-
expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
56+
expect(render(<div>x</div>).container.firstChild).toMatchInlineSnapshot(\`
5557
<div>
5658
y
5759
</div>
@@ -94,13 +96,13 @@ it('successfully runs the tests with external babel config', () => {
9496
9597
3 |
9698
4 | test('<div>x</div>', () => {
97-
> 5 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
98-
| ^
99+
> 5 | expect(render(<div>x</div>).container.firstChild).toMatchInlineSnapshot(\`
100+
| ^
99101
6 | <div>
100102
7 | y
101103
8 | </div>
102104
103-
at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:5:50)
105+
at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:5:53)
104106
"
105107
`);
106108

@@ -143,13 +145,13 @@ it('successfully runs the tests with inline babel config', () => {
143145
144146
3 |
145147
4 | test('<div>x</div>', () => {
146-
> 5 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\`
147-
| ^
148+
> 5 | expect(render(<div>x</div>).container.firstChild).toMatchInlineSnapshot(\`
149+
| ^
148150
6 | <div>
149151
7 | y
150152
8 | </div>
151153
152-
at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:5:50)
154+
at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:5:53)
153155
"
154156
`);
155157

e2e/transform/multiple-transformers/__tests__/__snapshots__/multipleTransformers.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
exports[`generates a snapshot with correctly transformed dependencies 1`] = `
44
<div
5-
className="App-root"
5+
class="App-root"
66
>
77
<div
8-
className="App-header"
8+
class="App-header"
99
>
1010
<img
1111
alt="logo"
12-
className="App-logo"
12+
class="App-logo"
1313
src="logo.svg"
1414
/>
1515
<h2>
1616
Welcome to React
1717
</h2>
1818
</div>
1919
<p
20-
className="App-intro"
20+
class="App-intro"
2121
>
2222
To get started, edit
2323
<code>
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
15
/**
26
* Copyright (c) Meta Platforms, Inc. and affiliates.
37
*
@@ -6,10 +10,10 @@
610
*/
711

812
import React from 'react';
9-
import renderer from 'react-test-renderer';
13+
import {render} from '@testing-library/react';
1014
import App from '../src/App';
1115

1216
it('generates a snapshot with correctly transformed dependencies', () => {
13-
const tree = renderer.create(<App />).toJSON();
14-
expect(tree).toMatchSnapshot();
17+
const {container} = render(<App />);
18+
expect(container.firstChild).toMatchSnapshot();
1519
});

e2e/transform/multiple-transformers/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"@babel/core": "^7.0.0",
1818
"@babel/preset-env": "^7.0.0",
1919
"@babel/preset-react": "^7.0.0",
20+
"@testing-library/react": "^16.3.0",
2021
"react": "18.2.0",
21-
"react-dom": "18.2.0",
22-
"react-test-renderer": "18.2.0"
22+
"react-dom": "18.2.0"
2323
}
2424
}

e2e/transform/multiple-transformers/yarn.lock

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,13 @@ __metadata:
11311131
languageName: node
11321132
linkType: hard
11331133

1134+
"@babel/runtime@npm:^7.12.5":
1135+
version: 7.28.3
1136+
resolution: "@babel/runtime@npm:7.28.3"
1137+
checksum: 10/f2415e4dbface7496f6fc561d640b44be203071fb0dfb63fbe338c7d2d2047419cb054ef13d1ebb8fc11e35d2b55aa3045def4b985e8b82aea5d7e58e1133e52
1138+
languageName: node
1139+
linkType: hard
1140+
11341141
"@babel/template@npm:^7.27.1":
11351142
version: 7.27.2
11361143
resolution: "@babel/template@npm:7.27.2"
@@ -1209,6 +1216,26 @@ __metadata:
12091216
languageName: node
12101217
linkType: hard
12111218

1219+
"@testing-library/react@npm:^16.3.0":
1220+
version: 16.3.0
1221+
resolution: "@testing-library/react@npm:16.3.0"
1222+
dependencies:
1223+
"@babel/runtime": "npm:^7.12.5"
1224+
peerDependencies:
1225+
"@testing-library/dom": ^10.0.0
1226+
"@types/react": ^18.0.0 || ^19.0.0
1227+
"@types/react-dom": ^18.0.0 || ^19.0.0
1228+
react: ^18.0.0 || ^19.0.0
1229+
react-dom: ^18.0.0 || ^19.0.0
1230+
peerDependenciesMeta:
1231+
"@types/react":
1232+
optional: true
1233+
"@types/react-dom":
1234+
optional: true
1235+
checksum: 10/0ee9e31dd0d2396a924682d0e61a4ecc6bfab8eaff23dbf8a72c3c2ce22c116fa578148baeb4de75b968ef99d22e6e6aa0a00dba40286f71184918bb6bb5b06a
1236+
languageName: node
1237+
linkType: hard
1238+
12121239
"babel-plugin-polyfill-corejs2@npm:^0.4.10":
12131240
version: 0.4.13
12141241
resolution: "babel-plugin-polyfill-corejs2@npm:0.4.13"
@@ -1429,13 +1456,6 @@ __metadata:
14291456
languageName: node
14301457
linkType: hard
14311458

1432-
"object-assign@npm:^4.1.1":
1433-
version: 4.1.1
1434-
resolution: "object-assign@npm:4.1.1"
1435-
checksum: 10/fcc6e4ea8c7fe48abfbb552578b1c53e0d194086e2e6bbbf59e0a536381a292f39943c6e9628af05b5528aa5e3318bb30d6b2e53cadaf5b8fe9e12c4b69af23f
1436-
languageName: node
1437-
linkType: hard
1438-
14391459
"path-parse@npm:^1.0.7":
14401460
version: 1.0.7
14411461
resolution: "path-parse@npm:1.0.7"
@@ -1462,38 +1482,6 @@ __metadata:
14621482
languageName: node
14631483
linkType: hard
14641484

1465-
"react-is@npm:^16.12.0 || ^17.0.0 || ^18.0.0, react-is@npm:^18.2.0":
1466-
version: 18.3.1
1467-
resolution: "react-is@npm:18.3.1"
1468-
checksum: 10/d5f60c87d285af24b1e1e7eaeb123ec256c3c8bdea7061ab3932e3e14685708221bf234ec50b21e10dd07f008f1b966a2730a0ce4ff67905b3872ff2042aec22
1469-
languageName: node
1470-
linkType: hard
1471-
1472-
"react-shallow-renderer@npm:^16.15.0":
1473-
version: 16.15.0
1474-
resolution: "react-shallow-renderer@npm:16.15.0"
1475-
dependencies:
1476-
object-assign: "npm:^4.1.1"
1477-
react-is: "npm:^16.12.0 || ^17.0.0 || ^18.0.0"
1478-
peerDependencies:
1479-
react: ^16.0.0 || ^17.0.0 || ^18.0.0
1480-
checksum: 10/06457fe5bcaa44aeca998905b6849304742ea1cc2d3841e4a0964c745ff392bc4dec07f8c779f317faacce3a0bf6f84e15020ac0fa81adb931067dbb0baf707b
1481-
languageName: node
1482-
linkType: hard
1483-
1484-
"react-test-renderer@npm:18.2.0":
1485-
version: 18.2.0
1486-
resolution: "react-test-renderer@npm:18.2.0"
1487-
dependencies:
1488-
react-is: "npm:^18.2.0"
1489-
react-shallow-renderer: "npm:^16.15.0"
1490-
scheduler: "npm:^0.23.0"
1491-
peerDependencies:
1492-
react: ^18.2.0
1493-
checksum: 10/39473e43f64eec92da35db9d4411f3887b368038670787d49dd23172eb3a29953eb13767d1bfa34cbe2665b6e25632cad146e362e8910ce33755d343537763ae
1494-
languageName: node
1495-
linkType: hard
1496-
14971485
"react@npm:18.2.0":
14981486
version: 18.2.0
14991487
resolution: "react@npm:18.2.0"
@@ -1584,9 +1572,9 @@ __metadata:
15841572
"@babel/core": "npm:^7.0.0"
15851573
"@babel/preset-env": "npm:^7.0.0"
15861574
"@babel/preset-react": "npm:^7.0.0"
1575+
"@testing-library/react": "npm:^16.3.0"
15871576
react: "npm:18.2.0"
15881577
react-dom: "npm:18.2.0"
1589-
react-test-renderer: "npm:18.2.0"
15901578
languageName: unknown
15911579
linkType: soft
15921580

examples/react-testing-library/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"@babel/core": "^7.27.4",
1111
"@babel/preset-env": "^7.27.2",
1212
"@babel/preset-react": "^7.27.1",
13-
"@testing-library/react": "^14.0.0",
13+
"@testing-library/react": "^16.3.0",
1414
"babel-jest": "workspace:*",
1515
"jest": "workspace:*",
1616
"jest-environment-jsdom": "workspace:*"

examples/snapshot/Link.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default function Link({page, children}) {
2020

2121
return (
2222
<a
23+
aria-label={status}
2324
className={status}
2425
href={page || '#'}
2526
onMouseEnter={onMouseEnter}

0 commit comments

Comments
 (0)