-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Conversation
@@ -188,6 +188,13 @@ function processArgs() { | |||
), | |||
type: 'boolean', | |||
}, | |||
overwriteSnapshot: { |
There was a problem hiding this comment.
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
00f5b32
to
c19bf15
Compare
I rebased to 12.1.0 so it should be easier to rebase later. |
@facebook-github-bot import |
Thanks for importing. If you are an FB employee go to Phabricator to review. |
@facebook-github-bot shipit |
Thanks for importing. If you are an FB employee go to Phabricator to review. |
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. |
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 theReact.renderToString
method, otherwise it fallbacks tojest
's pretty printer.I choose it because it provides out of the box support for
Maps
andSets
other thanObject
s andArray
s.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 multipletoMatchSnapshot
, not just in the sameSuite
, but also in the sameSpec
, 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
tojest
to update it:This is an example of a failing test for a React component
