Skip to content

Commit

Permalink
Revert to .react.js file convention
Browse files Browse the repository at this point in the history
  • Loading branch information
charpeni committed Jan 5, 2022
1 parent 6e197ac commit 707f8b1
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions docs/SnapshotTesting.md
Expand Up @@ -9,11 +9,11 @@ A typical snapshot test case renders a UI component, takes a snapshot, then comp

## Snapshot Testing with Jest

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):
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):

```tsx
import renderer from 'react-test-renderer';
import Link from '../Link';
import Link from '../Link.react';

it('renders correctly', () => {
const tree = renderer
Expand All @@ -23,7 +23,7 @@ it('renders correctly', () => {
});
```

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:
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:

```javascript
exports[`renders correctly 1`] = `
Expand All @@ -40,7 +40,7 @@ exports[`renders correctly 1`] = `

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.

> 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.
> 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.
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.

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

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.

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:
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:

```js
Date.now = jest.fn(() => 1482363367071);
Expand Down
8 changes: 4 additions & 4 deletions docs/TutorialReact.md
Expand Up @@ -61,7 +61,7 @@ module.exports = {

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

```tsx title="Link.js"
```tsx title="Link.react.js"
import {useState} from 'react';

const STATUS = {
Expand Down Expand Up @@ -97,8 +97,8 @@ export default function Link({page, children}) {
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:

```tsx title="Link.test.js"
import Link from '../Link';
```tsx title="Link.react.test.js"
import Link from '../Link.react';
import renderer from 'react-test-renderer';

it('changes the class when hovered', () => {
Expand Down Expand Up @@ -128,7 +128,7 @@ it('changes the class when hovered', () => {

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

```javascript title="__tests__/__snapshots__/Link.test.js.snap"
```javascript title="__tests__/__snapshots__/Link.react.test.js.snap"
exports[`changes the class when hovered 1`] = `
<a
className="normal"
Expand Down
File renamed without changes.
File renamed without changes.
Expand Up @@ -2,7 +2,7 @@

'use strict';

import Clock from '../Clock';
import Clock from '../Clock.react';
import renderer from 'react-test-renderer';

jest.useFakeTimers();
Expand Down
Expand Up @@ -2,7 +2,7 @@

'use strict';

import Link from '../Link';
import Link from '../Link.react';
import renderer from 'react-test-renderer';

it('renders correctly', () => {
Expand Down

0 comments on commit 707f8b1

Please sign in to comment.