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

fix '-u' suggestion and snapshot summary #9

Merged
merged 1 commit into from
Jun 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import { SnapshotState, toMatchSnapshot, addSerializer } from 'jest-snapshot';

const snapshotsStateMap = new Map();
let commonSnapshotState;

function getAbsolutePathToSnapshot(testPath, snapshotFile) {
return path.isAbsolute(snapshotFile)
Expand All @@ -19,13 +20,23 @@ afterAll(() => {
}

snapshotState.save();

if (commonSnapshotState) {
// Update common state so we get the report right with added/update/unmatched snapshots.
// Jest will display the "u" & "i" suggestion, plus displaying the right number of update/added/unmatched snapshots.
commonSnapshotState.unmatched += snapshotState.unmatched;
commonSnapshotState.matched += snapshotState.matched;
commonSnapshotState.updated += snapshotState.updated;
commonSnapshotState.added += snapshotState.added;
}
});
});

function toMatchSpecificSnapshot(received, snapshotFile, testName) {
const absoluteSnapshotFile = getAbsolutePathToSnapshot(this.testPath, snapshotFile);

const commonSnapshotState = this.snapshotState;
// store the common state to re-use it in "afterAll" hook.
commonSnapshotState = this.snapshotState;
Copy link
Owner

Choose a reason for hiding this comment

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

As I understand this will be replaced on every toMatchSpecificSnapshot call ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed its is the only way we can get the original snapshotState. Not sure it worth it to add some checks to only store it the first time we get there. Wdty ?

Copy link
Owner

Choose a reason for hiding this comment

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

I have to debug it a bit to understand the whole picture =)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you need more intel, please tell me :) Don't want to rush things, just would love to see this happen so it eases the process of updating snapshots in storyshots.

Copy link
Owner

Choose a reason for hiding this comment

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

Sorry, will get to it this week. Just was a bit out of focus.

let snapshotState = snapshotsStateMap.get(absoluteSnapshotFile);

if (!snapshotState) {
Expand Down