-
-
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
Add snapshot version support #2896
Conversation
@@ -98,7 +98,7 @@ describe('multiple-transformers', () => { | |||
}); | |||
|
|||
it('transforms dependencies using specific transformers', () => { | |||
const {json, stderr} = runJest.json(dir, ['--no-cache']); | |||
const {json, stderr} = runJest.json(dir, ['--no-cache', '-u']); |
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.
Had to add that, so the snapshot version is updated, otherwise it fails.
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.
hm. can't you update snapshot in the underlying test directory?
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.
Oh I know what happened, I've updated it manually before this change #2896 (comment), so it wasn't updated. You're totally right and fix is already applied, thanks!
@@ -8,9 +10,9 @@ exports[`trimAndFormatPath() trims dirname 1`] = `"[2m...234567890/[22m[1m123 | |||
|
|||
exports[`trimAndFormatPath() trims dirname and basename 1`] = `"[1m...1234.js[22m"`; | |||
|
|||
exports[`wrapAnsiString() returns the string unaltered if given a terminal width of zero 1`] = `"This string shouldn\'t cause you any trouble"`; | |||
exports[`wrapAnsiString() returns the string unaltered if given a terminal width of zero 1`] = `"This string shouldn't cause you any trouble"`; |
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.
Shouldn't this be fixed?
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.
omg yes. Not sure what is going on here?
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 you remove this snapshot and run the test again and use the result of that?
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.
Wait, actually this is correct now. I don't understand why it wasn't updated before. cc @vjeux.
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.
Maybe because before it was checking the unserialized state so they were both the same and therefore didn't needed an update? idk
packages/jest-snapshot/src/State.js
Outdated
@@ -83,7 +83,7 @@ class SnapshotState { | |||
|
|||
const isEmpty = Object.keys(this._snapshotData).length === 0; | |||
|
|||
if ((this._dirty || this._uncheckedKeys.size) && !isEmpty) { | |||
if ((this._dirty || this._uncheckedKeys.size || update) && !isEmpty) { |
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.
This is added because otherwise saveSnapshotFile
wouldn't be called (adding a version comment doesn't mark snapshot "dirty")
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 make this so we only write the file if update is given & the snapshot header is either missing or there is a mismatch? I wanna prevent writes from happening if we don't need them.
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.
Updated. I'm making snapshot "dirty" if it is in update mode and would throw invalid version error. I'm not very happy with returning a tuple from getSnapshotData
but it's past midnight here and it seems to work :p
snapshotContents = fs.readFileSync(snapshotPath, 'utf8'); | ||
// eslint-disable-next-line no-new-func | ||
const populate = new Function('exports', snapshotContents); | ||
populate(data); |
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.
As already changed here: #2629
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.
👍
@@ -1,3 +1,5 @@ | |||
// Jest Snapshot v1 |
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.
Let's do:
// Jest Snapshot v1, https://goo.gl/fbAQLP
which links to our snapshot testing guide.
cc @kentcdodds who will be pleased.
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.
Added the link
packages/jest-snapshot/src/utils.js
Outdated
`// Jest Snapshot v${SNAPSHOT_VERSION}`; | ||
|
||
const validateSnapshotVersion = (snapshotContents: string) => { | ||
const versionTest = SNAPSHOT_VERSION_REGEXP.exec(snapshotContents); |
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.
I guess you are already making sure we only match the beginning of the string with ^. I'm not sure why the regex has "g" though? We only want to find the version in the beginning of the file, once.
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.
👍 Fixed
packages/jest-snapshot/src/utils.js
Outdated
|
||
const validateSnapshotVersion = (snapshotContents: string) => { | ||
const versionTest = SNAPSHOT_VERSION_REGEXP.exec(snapshotContents); | ||
const version = versionTest && versionTest[1] || '0'; |
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 you do (versionTest && versionTest[1]) || '0';
just for readability?
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.
sure!
packages/jest-snapshot/src/utils.js
Outdated
`Stored snapshot version is outdated.\n` + | ||
`Expected: v${SNAPSHOT_VERSION}, but received: v${version}\n` + | ||
`Update the snapshot to remove this error.` | ||
); |
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.
This is a great error but I think we should aim to make this more informative and absolutely solid in messaging. Here is my recommendation:
- If no snapshot header is not found:
Outdated snapshot: No snapshot header found. Jest 19 introduced versioned snapshots to ensure all people on a project are using the same version of Jest. Please update all snapshots during this upgrade of Jest.
More information: jest-19-blogpost (I will update this later)
- If snapshot header is lower than current version of Jest:
Outdated snapshot: The version of the snapshot file associated with this test is outdated. The snapshot file version ensures that all people on a project are using the same version of Jest. Please update all snapshots during this upgrade of Jest.
Expected: v1
Received: v0
More information: jest-19-blogpost (I will update this later)
- If snapshot header is higher than current version of Jest:
Outdated Jest version: the version of this snapshot file indicates that this project is meant to be used with a newer version of Jest. The snapshot file version ensures that all people on a project are using the same version of Jest. Please update your version of Jest and re-run the tests.
Expected: v1
Received: v2
More information: jest-19-blogpost (I will update this later)
We must also include a warning (I think this was brought up by @gaearon yesterday) for the first two to indicate that local change to tests shall be reverted during this update to avoid saving wrong snapshot state. For the first two, how about:
Warning: It is advised to revert any local changes to tests or other code during this upgrade to ensure that no invalid state is stored as a snapshot.
What do you think?
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.
I'm all for it. Updated.
I think this is really solid. Thanks for working on this. I'm pretty excited about providing a solid user experience for this workflow – nothing worse than snapshots changing back and forth from one version to another because two people use different versions of Jest. |
if (update && snapshotContents) { | ||
try { | ||
validateSnapshotVersion(snapshotContents); | ||
} catch (error) { |
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.
* Add snapshot version support * Add missing snapshots * Add link to snapshot guide, small regexp fixes * Update examples snapshots * Add parens for readability * Remove unnecessary -u flag from transform test * Update snapshot version messages to be more exhaustive * Fix error formatting * Don't write to fs if not needed
* Add snapshot version support * Add missing snapshots * Add link to snapshot guide, small regexp fixes * Update examples snapshots * Add parens for readability * Remove unnecessary -u flag from transform test * Update snapshot version messages to be more exhaustive * Fix error formatting * Don't write to fs if not needed
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. |
Summary
Fixes #2853
Here's how the error looks like:
Test plan
I think I should also include one more test for checking if
saveSnapshotFile()
is called when updating, although test-examples catch this issue.