Skip to content

Commit 6db8c8a

Browse files
authored
Create adr-011-snapshot-tests.md (#2780)
* Create adr-XXX-snapshot-tests.md * docs: update snapshot adr with impact * Update and rename adr-XXX-snapshot-tests.md to adr-011-snapshot-tests.md --------- Co-authored-by: Josh Black <joshblack@users.noreply.github.com>
1 parent 13651ba commit 6db8c8a

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# ADR 011: Snapshot tests
2+
3+
## Status
4+
5+
Approved
6+
7+
## Context
8+
9+
[Snapshot testing](https://jestjs.io/docs/snapshot-testing) is a strategy that allows a developer to "snapshot" a given value within a test suite. This value could be a primitive within JavaScript, an Array, an object, or the resulting HTML of rendering a component in React. The snapshot of the value is stored within the repo and is then compared against future test runs.
10+
11+
When a change is detected to a snapshot, the test will fail with a diff between the currently stored snapshot and the new snapshot. Within the space of React components, this may happen when a class name changes or the markup of a component changes. Is is then up to the developer to decide if the change is intentional and whether or not to accept this new snapshot as the baseline for future test runs.
12+
13+
A common practice is to snapshot a React component which will capture the HTML output of rendering a component. For example:
14+
15+
```jsx
16+
import renderer from 'react-test-renderer'
17+
import Link from '../Link'
18+
19+
it('renders correctly', () => {
20+
const tree = renderer.create(<Link page="http://www.facebook.com">Facebook</Link>).toJSON()
21+
expect(tree).toMatchSnapshot()
22+
})
23+
```
24+
25+
```js
26+
exports[`renders correctly 1`] = `
27+
<a
28+
className="normal"
29+
href="http://www.facebook.com"
30+
onMouseEnter={[Function]}
31+
onMouseLeave={[Function]}
32+
>
33+
Facebook
34+
</a>
35+
`
36+
```
37+
38+
[Source](https://jestjs.io/docs/snapshot-testing#snapshot-testing-with-jest)
39+
40+
As the number of snapshots within a project grows, there are a couple of challenges that emerge:
41+
42+
- Components with wide or deep trees emit large snapshots
43+
- Debugging what change lead to a broken snapshot can be difficult
44+
- It is unclear what the intent may be of a snapshot test when something does fail, in other words it does not always answer the question: what is this testing?
45+
46+
## Decision
47+
48+
- Avoid using "catch-all" snapshot tests for React components
49+
- Snapshots may be used for static objects which have a clear signal as to what to do if they are modified (such as the exports of a package)
50+
51+
<table>
52+
<thead><tr><th>Unpreferred</th><th>Preferred</th></tr></thead>
53+
<tbody>
54+
<tr><td>
55+
56+
```tsx
57+
it('renders correctly', () => {
58+
const tree = renderer.create(<Link page="http://www.github.com">GitHub</Link>).toJSON()
59+
expect(tree).toMatchSnapshot()
60+
})
61+
```
62+
63+
</td><td>
64+
65+
```tsx
66+
it('renders an element with role="link"', () => {
67+
render(<Link page="http://www.github.com">GitHub</Link>)
68+
expect(screen.getByRole('link', {name: 'GitHub'})).toBeInTheDocument()
69+
})
70+
```
71+
72+
</td></tr>
73+
</tbody></table>
74+
75+
### Impact
76+
77+
This decision will impact our test suite in two ways:
78+
79+
- Tests with calls to `toMatchSnapshot()` may need to be removed if the snapshot
80+
test is capturing the render tree
81+
- The `behavesAsComponent` helper, which uses `toMatchSnapshot` under-the-hood,
82+
will need to be updated to no longer include a snapshot test

0 commit comments

Comments
 (0)