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 snapshot version support #2896

Merged
merged 9 commits into from Feb 16, 2017
Merged

Add snapshot version support #2896

merged 9 commits into from Feb 16, 2017

Conversation

thymikee
Copy link
Collaborator

@thymikee thymikee commented Feb 15, 2017

Summary

Fixes #2853

Here's how the error looks like:

screen shot 2017-02-15 at 20 26 29

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.

@@ -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']);
Copy link
Collaborator Author

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.

Copy link
Contributor

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?

Copy link
Collaborator Author

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`] = `"...234567890/123

exports[`trimAndFormatPath() trims dirname and basename 1`] = `"...1234.js"`;

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"`;
Copy link
Collaborator Author

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?

Copy link
Member

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?

Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor

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

@@ -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) {
Copy link
Collaborator Author

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")

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 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.

Copy link
Collaborator Author

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);
Copy link
Collaborator Author

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

Copy link
Member

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

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added the link

`// Jest Snapshot v${SNAPSHOT_VERSION}`;

const validateSnapshotVersion = (snapshotContents: string) => {
const versionTest = SNAPSHOT_VERSION_REGEXP.exec(snapshotContents);
Copy link
Member

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍 Fixed


const validateSnapshotVersion = (snapshotContents: string) => {
const versionTest = SNAPSHOT_VERSION_REGEXP.exec(snapshotContents);
const version = versionTest && versionTest[1] || '0';
Copy link
Member

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sure!

`Stored snapshot version is outdated.\n` +
`Expected: v${SNAPSHOT_VERSION}, but received: v${version}\n` +
`Update the snapshot to remove this error.`
);
Copy link
Member

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?

Copy link
Collaborator Author

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.

@cpojer
Copy link
Member

cpojer commented Feb 15, 2017

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

Choose a reason for hiding this comment

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

I kinda feel like we could clean this up to return an error and throw in #105 if an error is returned. Here on #109, instead of try-catch, call the validator and set dirty to true if it returns a value. What do you think as a follow-up?

@cpojer cpojer merged commit 22c89b4 into jestjs:master Feb 16, 2017
@thymikee thymikee deleted the snaphot-version branch February 19, 2017 16:30
skovhus pushed a commit to skovhus/jest that referenced this pull request Apr 29, 2017
* 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
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* 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
@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 13, 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.

Jest Snapshot Version Header
5 participants