Skip to content

Commit

Permalink
Add a 'best practices' section to the Snapshot Testing Guide:
Browse files Browse the repository at this point in the history
Issue: jestjs#5812

Tools get even better when it's clear how they're meant to be
used.

Created a 'best practices' section where patterns that are known
to improve the usefulness of Jest's snapshot feature can be
explained for the community's benefit.
  • Loading branch information
k-m-a-c committed Apr 30, 2018
1 parent 4c75933 commit 7701b85
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
([#5675](https://github.com/facebook/jest/pull/5675))
* `[docs]` Add versioned docs for v22.4
([##5733](https://github.com/facebook/jest/pull/#5733))
* `[docs]` Improve Snapshot Testing Guide ([#5812](https://github.com/facebook/jest/issues/5812))

## 22.4.2

Expand Down
28 changes: 19 additions & 9 deletions website/versioned_docs/version-22.3/SnapshotTesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,22 @@ From here you can choose to update that snapshot or skip to the next:

![](/jest/img/content/interactiveSnapshotUpdate.gif)

### Tests Should Be Deterministic
## Best Practices

Snapshots are a fantastic tool for identifying unexpected interface changes within your application – whether that interface is a UI, logs, or error messages; however, they're not a cure-all, and, as with any testing strategy, there are some best-practices you should be aware of, and guidelines you should follow, in order to use them effectively.

### 1. Treat Snapshots As First-Class Citizens

Commit snapshots and review them as part of your regular code review process. This implies treating snapshots as you would any other type of test, or code for that matter, in your project.

Ensure that your snapshots are _very_ readable by keeping them focused, short, and using tools that enforce these stylistic conventions.

As mentioned previously, Jest uses
[pretty-format](https://github.com/facebook/jest/tree/master/packages/pretty-format) to make snapshots human-readable, but you may find it useful to introduce additional tools, like [`eslint-plugin-jest`](https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/no-large-snapshots.md) with its `'no-large-snapshots'` option, and [`snapshot-diff`](https://github.com/jest-community/snapshot-diff) with its component snapshot comparison feature, to promote committing short, focused assertions.

The aim here is to avoid the dangerous habits of glossing over snapshots in pull requests, and simply regenerating snapshots when test suites fail instead of examining the root causes of their failure.

### 2. Tests Should Be Deterministic

Your tests should be deterministic. That is, running the same tests multiple
times on a component that has not changed should produce the same results every
Expand All @@ -158,16 +173,11 @@ Now, every time the snapshot test case runs, `Date.now()` will return
`1482363367071` consistently. This will result in the same snapshot being
generated for this component regardless of when the test is run.

### Snapshots are not written automatically on Continuous Integration systems (CI)
## Frequently Asked Questions

As of Jest 20, snapshots in Jest are not automatically written when Jest is run
in a CI system without explicitly passing `--updateSnapshot`. It is expected
that all snapshots are part of the code that is run on CI and since new
snapshots automatically pass, they should not pass a test run on a CI system. It
is recommended to always commit all snapshots and to keep them in version
control.
### Are snapshots written automatically on Continuous Integration (CI) systems?

## Frequently Asked Questions
No, as of Jest 20, snapshots in Jest are not automatically written when Jest is run in a CI system without explicitly passing `--updateSnapshot`. It is expected that all snapshots are part of the code that is run on CI and since new snapshots automatically pass, they should not pass a test run on a CI system. It is recommended to always commit all snapshots and to keep them in version control.

### Should snapshot files be committed?

Expand Down

0 comments on commit 7701b85

Please sign in to comment.