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

[WIP] Snapshot functionality #1000

Closed
wants to merge 33 commits into from

Conversation

kentaromiura
Copy link
Contributor

This commit introduce a new matcher to jest:
toMatchSnapshot.
this new matcher can be called as usual:
expect(something).toMatchSnapshot()
the main idea is that the first time the test run it will store whatever is inside expect in a snapshot file,
the next time jest runs it will check against it to see if something has changed.

In order to store something on file we need to serialize it, so I currently implemented two different serialization strategies, if we are expecting a React element to match a snapshot representation then it will be currently be serialized using the React.renderToString method, otherwise it fallbacks to jest's pretty printer.
I choose it because it provides out of the box support for Maps and Sets other than Objects and Arrays.
As the title mention this is a work in progress as I plan to iterate on it to improve the serialization strategies and possible the output when it fails the expectation.

The matcher uses jasmine Spec getFullName as the key in the snapshot file, adding an index at the end, this enable us to use multiple toMatchSnapshot, not just in the same Suite, but also in the same Spec, when failing it will highlight which snapshot has failed prepending # in front of an index of it (it starts at 0, but we can change it to 1 if it creates confusion).

When a test fails one can either fix the code breaking the expectation or, if the expectation is not up to date anymore, pass --overwriteSnapshot to jest to update it:

screen shot 2016-05-12 at 4 57 56 pm

This is an example of a failing test for a React component
screen shot 2016-05-12 at 5 25 33 pm

@ghost ghost added the CLA Signed ✔️ label May 12, 2016
@@ -188,6 +188,13 @@ function processArgs() {
),
type: 'boolean',
},
overwriteSnapshot: {
Copy link
Member

Choose a reason for hiding this comment

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

can we call this updateSnapshot and give it -u as a shortcut?

removing the default object serialize using JasminePretty printed,
still using it for formatting the diff
since I'm going to rewrite the part that save the file only at the end
of a file run, this will not work anyway
Moved almost all file/path access to it (todo moving the last in index),
Create a snapshotState on jest-jasmine2 that gets passed to the
`getMatchers` function, so that common can be deleted.
`getMatchers` uses the snapshotState to gather the Spec fullname and to
increment a counter (I may use an autoincrement getter here in the
future, so that even this state is not handled here).
`initialize` is now `getSnapshotState` which returns the state
management relative to a single file,
TestSnapshot is fully rewritten to load and save a file only once.
…` factory

move all path management to TestSnapShot, also only export a `forFile`
factory as per @cpojer comment, also rename file to `SnapshotFile` as
per another comment.
fix creation of a snapshot file per test file if toMatchSnapshot hasn't
been called.
adding basic unit tests
also adds some more tests in the integration tests part
@kentaromiura
Copy link
Contributor Author

I rebased to 12.1.0 so it should be easier to rebase later.

@cpojer
Copy link
Member

cpojer commented May 24, 2016

@facebook-github-bot import

@ghost
Copy link

ghost commented May 24, 2016

Thanks for importing. If you are an FB employee go to Phabricator to review.

@cpojer
Copy link
Member

cpojer commented May 25, 2016

@facebook-github-bot shipit

@ghost
Copy link

ghost commented May 25, 2016

Thanks for importing. If you are an FB employee go to Phabricator to review.

@ghost ghost closed this in dc7f3a9 May 25, 2016
@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 14, 2021
This pull request was closed.
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.

2 participants