Skip to content

Commit 707f8b1

Browse files
authored
Revert to .react.js file convention
1 parent 6e197ac commit 707f8b1

8 files changed

Lines changed: 11 additions & 11 deletions

File tree

docs/SnapshotTesting.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ 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/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):
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/facebook/jest/blob/main/examples/snapshot/__tests__/link.react.test.js) for a [Link component](https://github.com/facebook/jest/blob/main/examples/snapshot/Link.react.js):
1313

1414
```tsx
1515
import renderer from 'react-test-renderer';
16-
import Link from '../Link';
16+
import Link from '../Link.react';
1717

1818
it('renders correctly', () => {
1919
const tree = renderer
@@ -23,7 +23,7 @@ it('renders correctly', () => {
2323
});
2424
```
2525

26-
The first time this test is run, Jest creates a [snapshot file](https://github.com/facebook/jest/blob/main/examples/snapshot/__tests__/__snapshots__/link.test.js.snap) that looks like this:
26+
The first time this test is run, Jest creates a [snapshot file](https://github.com/facebook/jest/blob/main/examples/snapshot/__tests__/__snapshots__/link.react.test.js.snap) that looks like this:
2727

2828
```javascript
2929
exports[`renders correctly 1`] = `
@@ -40,7 +40,7 @@ exports[`renders correctly 1`] = `
4040

4141
The snapshot artifact should be committed alongside code changes, and reviewed as part of your code review process. Jest uses [pretty-format](https://github.com/facebook/jest/tree/main/packages/pretty-format) to make snapshots human-readable during code review. On subsequent test runs, Jest will compare the rendered output with the previous snapshot. If they match, the test will pass. If they don't match, either the test runner found a bug in your code (in the `<Link>` component in this case) that should be fixed, or the implementation has changed and the snapshot needs to be updated.
4242

43-
> Note: The snapshot is directly scoped to the data you render – in our example the `<Link />` component with `page` prop passed to it. This implies that even if any other file has missing props (Say, `App.js`) in the `<Link />` component, it will still pass the test as the test doesn't know the usage of `<Link />` component and it's scoped only to the `Link.js`. Also, rendering the same component with different props in other snapshot tests will not affect the first one, as the tests don't know about each other.
43+
> Note: The snapshot is directly scoped to the data you render – in our example the `<Link />` component with `page` prop passed to it. This implies that even if any other file has missing props (Say, `App.js`) in the `<Link />` component, it will still pass the test as the test doesn't know the usage of `<Link />` component and it's scoped only to the `Link.react.js`. Also, rendering the same component with different props in other snapshot tests will not affect the first one, as the tests don't know about each other.
4444
4545
More information on how snapshot testing works and why we built it can be found on the [release blog post](/blog/2016/07/27/jest-14). We recommend reading [this blog post](http://benmccormick.org/2016/09/19/testing-with-jest-snapshots-first-impressions/) to get a good sense of when you should use snapshot testing. We also recommend watching this [egghead video](https://egghead.io/lessons/javascript-use-jest-s-snapshot-testing-feature?pl=testing-javascript-with-jest-a36c4074) on Snapshot Testing with Jest.
4646

@@ -226,7 +226,7 @@ The goal is to make it easy to review snapshots in pull requests, and fight agai
226226

227227
Your tests should be deterministic. Running the same tests multiple times on a component that has not changed should produce the same results every time. You're responsible for making sure your generated snapshots do not include platform specific or other non-deterministic data.
228228

229-
For example, if you have a [Clock](https://github.com/facebook/jest/blob/main/examples/snapshot/Clock.js) component that uses `Date.now()`, the snapshot generated from this component will be different every time the test case is run. In this case we can [mock the Date.now() method](MockFunctions.md) to return a consistent value every time the test is run:
229+
For example, if you have a [Clock](https://github.com/facebook/jest/blob/main/examples/snapshot/Clock.react.js) component that uses `Date.now()`, the snapshot generated from this component will be different every time the test case is run. In this case we can [mock the Date.now() method](MockFunctions.md) to return a consistent value every time the test is run:
230230

231231
```js
232232
Date.now = jest.fn(() => 1482363367071);

docs/TutorialReact.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = {
6161

6262
Let's create a [snapshot test](SnapshotTesting.md) for a Link component that renders hyperlinks:
6363

64-
```tsx title="Link.js"
64+
```tsx title="Link.react.js"
6565
import {useState} from 'react';
6666

6767
const STATUS = {
@@ -97,8 +97,8 @@ export default function Link({page, children}) {
9797
9898
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:
9999

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

104104
it('changes the class when hovered', () => {
@@ -128,7 +128,7 @@ it('changes the class when hovered', () => {
128128

129129
When you run `yarn test` or `jest`, this will produce an output file like this:
130130

131-
```javascript title="__tests__/__snapshots__/Link.test.js.snap"
131+
```javascript title="__tests__/__snapshots__/Link.react.test.js.snap"
132132
exports[`changes the class when hovered 1`] = `
133133
<a
134134
className="normal"

examples/snapshot/__tests__/__snapshots__/clock.test.js.snap renamed to examples/snapshot/__tests__/__snapshots__/clock.react.test.js.snap

File renamed without changes.

examples/snapshot/__tests__/__snapshots__/link.test.js.snap renamed to examples/snapshot/__tests__/__snapshots__/link.react.test.js.snap

File renamed without changes.

examples/snapshot/__tests__/clock.test.js renamed to examples/snapshot/__tests__/clock.react.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
'use strict';
44

5-
import Clock from '../Clock';
5+
import Clock from '../Clock.react';
66
import renderer from 'react-test-renderer';
77

88
jest.useFakeTimers();

examples/snapshot/__tests__/link.test.js renamed to examples/snapshot/__tests__/link.react.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
'use strict';
44

5-
import Link from '../Link';
5+
import Link from '../Link.react';
66
import renderer from 'react-test-renderer';
77

88
it('renders correctly', () => {

0 commit comments

Comments
 (0)