Skip to content

Commit

Permalink
fix(matcher): update suggestion and snapshot summary to display corre…
Browse files Browse the repository at this point in the history
…ct values
  • Loading branch information
thomasbertet authored and anescobar1991 committed Jun 7, 2018
1 parent c503314 commit 0ce55c6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
13 changes: 13 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ describe('toMatchImageSnapshot', () => {
}

beforeEach(() => {
// In tests, skip reporting (skip snapshotState update to not mess with our test report)
global.UNSTABLE_SKIP_REPORTING = true;
jest.resetModules();
jest.resetAllMocks();
});
Expand Down Expand Up @@ -363,3 +365,14 @@ describe('toMatchImageSnapshot', () => {
});
});
});

describe('updateSnapshotState', () => {
it('mutates original state', () => {
const { updateSnapshotState } = require('../src/index');
global.UNSTABLE_SKIP_REPORTING = false;
const originalState = { some: 'value' };
updateSnapshotState(originalState, { another: 'val' });

expect(originalState).toEqual({ some: 'value', another: 'val' });
});
});
2 changes: 2 additions & 0 deletions __tests__/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ describe('toMatchImageSnapshot', () => {
const diffExists = identifier => fs.existsSync(path.join(__dirname, diffOutputDir(), `${identifier}-diff.png`));

beforeAll(() => {
// In tests, skip reporting (skip snapshotState update to not mess with our test report)
global.UNSTABLE_SKIP_REPORTING = true;
const { toMatchImageSnapshot } = require('../src'); // eslint-disable-line global-require
expect.extend({ toMatchImageSnapshot });
});
Expand Down
19 changes: 13 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ const fs = require('fs');

const SNAPSHOTS_DIR = '__image_snapshots__';

function updateSnapshotState(oldSnapshotState, newSnapshotState) {
return merge({}, oldSnapshotState, newSnapshotState);
function updateSnapshotState(originalSnapshotState, partialSnapshotState) {
if (global.UNSTABLE_SKIP_REPORTING) {
return originalSnapshotState;
}
return merge(originalSnapshotState, partialSnapshotState);
}

function configureToMatchImageSnapshot({
Expand All @@ -40,12 +43,14 @@ function configureToMatchImageSnapshot({
failureThreshold = commonFailureThreshold,
failureThresholdType = commonFailureThresholdType,
} = {}) {
const { testPath, currentTestName, isNot } = this;
const {
testPath, currentTestName, isNot, snapshotState,
} = this;
const chalk = new Chalk({ enabled: !noColors });

let { snapshotState } = this;
if (isNot) { throw new Error('Jest: `.not` cannot be used with `.toMatchImageSnapshot()`.'); }


updateSnapshotState(snapshotState, { _counters: snapshotState._counters.set(currentTestName, (snapshotState._counters.get(currentTestName) || 0) + 1) }); // eslint-disable-line max-len
const snapshotIdentifier = customSnapshotIdentifier || kebabCase(`${path.basename(testPath)}-${currentTestName}-${snapshotState._counters.get(currentTestName)}`);

Expand Down Expand Up @@ -82,13 +87,14 @@ function configureToMatchImageSnapshot({
if (result.updated) {
// once transition away from jasmine is done this will be a lot more elegant and pure
// https://github.com/facebook/jest/pull/3668
snapshotState = updateSnapshotState(snapshotState, { updated: snapshotState.updated += 1 });
updateSnapshotState(snapshotState, { updated: snapshotState.updated + 1 });
} else if (result.added) {
snapshotState = updateSnapshotState(snapshotState, { added: snapshotState.added += 1 });
updateSnapshotState(snapshotState, { added: snapshotState.added + 1 });
} else {
({ pass } = result);

if (!pass) {
updateSnapshotState(snapshotState, { unmatched: snapshotState.unmatched + 1 });
const differencePercentage = result.diffRatio * 100;
message = () => `Expected image to match or be a close match to snapshot but was ${differencePercentage}% different from snapshot (${result.diffPixelCount} differing pixels).\n`
+ `${chalk.bold.red('See diff for details:')} ${chalk.red(result.diffOutputPath)}`;
Expand All @@ -105,4 +111,5 @@ function configureToMatchImageSnapshot({
module.exports = {
toMatchImageSnapshot: configureToMatchImageSnapshot(),
configureToMatchImageSnapshot,
updateSnapshotState,
};

0 comments on commit 0ce55c6

Please sign in to comment.