Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a 'best practices' section to the Snapshot Testing Guide #6101

Merged
merged 4 commits into from May 5, 2018

Conversation

k-m-a-c
Copy link
Contributor

@k-m-a-c k-m-a-c commented Apr 30, 2018

Fixes #5812

cc: @thymikee @SimenB

Summary

As explained in the issue referenced above (opened by @thymikee), Jest's Snapshot Testing Guide could benefit from some clearer usage guidelines.

The issue's wording specifically called out adding content that would help devs avoid common mistakes and misuses of snapshot testing. After giving the page a good read it seemed like the guide already had the nucleus of this content with the "Tests Should Be Deterministic" section.

Added some content that closely follows the recommendations outlined in the @kentcdodds's article Effective Snapshot Testing referenced in the issue, then grouped this and the existing "Tests Should Be Deterministic" section under a "Best Practices" heading.

Moved the note about snapshots not being automatically written on CI into the FAQ section, since it seemed more like an FYI than part of the guide's core content.

This is just a first-pass, so feedback is warmly welcomed! 😄

Test plan

N/A (this is an isolated docs change)

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.
Copy link
Contributor

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I approve of this PR :)

@rickhanlonii
Copy link
Member

rickhanlonii commented Apr 30, 2018

I think this is great, thanks! What do you think about adding a third item?

3. Use descriptive snapshot names

Always strive to use descriptive test and/or snapshot names for snapshots. The best names describe the expected snapshot content. This makes it easier for reviewers to verify the snapshots during review, and for anyone to know whether or not an outdated snapshot is the correct behavior before updating.

For example, compare:

exports[`<UserName /> should handle some test case`] = `null`;

exports[`<UserName /> should handle some other test case`] = `
<div>
  Alan Turing
</div>
`;

To:

exports[`<UserName /> should render null`] = `null`;

exports[`<UserName /> should render Alan Turing`] = `
<div>
  Alan Turing
</div>
`;

Since the later describes exactly what's expected in the output, it's easy to see when it's wrong:

exports[`<UserName /> should render null`] = `
<div>
  Alan Turing
</div>
`;

exports[`<UserName /> should render Alan Turing`] = `null`

@rickhanlonii
Copy link
Member

I linted the markdown for you 👌

@codecov-io
Copy link

codecov-io commented May 1, 2018

Codecov Report

Merging #6101 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #6101   +/-   ##
======================================
  Coverage    64.1%   64.1%           
======================================
  Files         217     217           
  Lines        8335    8335           
  Branches        4       3    -1     
======================================
  Hits         5343    5343           
  Misses       2991    2991           
  Partials        1       1

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4c75933...6c97871. Read the comment docs.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thanks! Left some minor comments, but it would be great to include the section @rickhanlonii added in a comment as well 🙂

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a loooong sentence! Mind splitting it up? I think just swapping the ; for a . should be enough 🙂

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure 🤷‍♂️

[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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here link to the specific rule

@@ -158,17 +189,17 @@ 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was phrased like this because of SEO. I guess Google should handle this though

@cpojer
Copy link
Member

cpojer commented May 4, 2018

Can somebody make the changes @SimenB asked for?

@rickhanlonii rickhanlonii merged commit 9f7c977 into jestjs:master May 5, 2018
@rickhanlonii
Copy link
Member

Thanks @TrustyKnave!

@SimenB
Copy link
Member

SimenB commented May 5, 2018

This landed in version-22.3/SnapshotTesting.md. Should at least be in master, maybe 22.4 as well

@rickhanlonii
Copy link
Member

@SimenB, PR submitted - no need to do 22.4 specifically as the snapshot doc is inheriting from 22.3

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Snapshot testing guide should mention caveats and how to overcome some of them
8 participants