Skip to content

Commit

Permalink
correctly support --ci (#67)
Browse files Browse the repository at this point in the history
* feat(diff-snapshot): correctly support continuous integration
  • Loading branch information
fringd authored and anescobar1991 committed Apr 17, 2018
1 parent 19ef5c5 commit 967d6f8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
60 changes: 54 additions & 6 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('toMatchImageSnapshot', () => {
isNot: false,
snapshotState: {
_counters: new Map(),
_updateSnapshot: 'none',
_updateSnapshot: 'new',
updated: undefined,
added: true,
},
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('toMatchImageSnapshot', () => {
isNot: false,
snapshotState: {
_counters: new Map(),
_updateSnapshot: 'none',
_updateSnapshot: 'new',
updated: undefined,
added: true,
},
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('toMatchImageSnapshot', () => {
isNot: false,
snapshotState: {
_counters: new Map(),
_updateSnapshot: 'none',
_updateSnapshot: 'new',
updated: undefined,
added: true,
},
Expand Down Expand Up @@ -233,16 +233,64 @@ describe('toMatchImageSnapshot', () => {
snapshotState: {
_counters: new Map(),
update: false,
_updateSnapshot: 'new',
updated: undefined,
added: true,
},
};
const mockDiffResult = { added: true };
const mockDiff = jest.fn();
jest.doMock('../src/diff-snapshot', () => ({
diffImageToSnapshot: mockDiff,
}));

const mockFs = Object.assign({}, fs, {
existsSync: jest.fn(),
unlinkSync: jest.fn(),
});

mockFs.existsSync.mockReturnValueOnce(false);
mockDiff.mockReturnValueOnce({ added: true });

setupMock(mockDiffResult);
const { toMatchImageSnapshot } = require('../src/index');
const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
expect(() => matcherAtTest('pretendthisisanimagebuffer')).not.toThrow();
expect(matcherAtTest('pretendthisisanimagebuffer')).toHaveProperty('pass', true);
expect(mockDiff).toHaveBeenCalled();
});

it('should fail when a new snapshot is added in ci', () => {
const mockTestContext = {
testPath: 'path/to/test.spec.js',
currentTestName: 'test1',
isNot: false,
snapshotState: {
_counters: new Map(),
update: false,
_updateSnapshot: 'none',
updated: undefined,
added: true,
},
};

const mockDiff = jest.fn();
jest.doMock('../src/diff-snapshot', () => ({
diffImageToSnapshot: mockDiff,
}));

const mockFs = Object.assign({}, fs, {
existsSync: jest.fn(),
unlinkSync: jest.fn(),
});

mockFs.existsSync.mockReturnValueOnce(false);


const { toMatchImageSnapshot } = require('../src/index');
const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
const result = matcherAtTest('pretendthisisanimagebuffer');
expect(result).toHaveProperty('pass', false);
expect(result).toHaveProperty('message');
expect(result.message()).toContain('continuous integration');
expect(mockDiff).not.toHaveBeenCalled();
});

it('should work when a snapshot is updated', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"lint": "eslint ./ --ignore-path .gitignore --ext .js",
"test": "jest",
"test": "jest --ci=false",
"posttest": "npm run lint"
},
"keywords": [
Expand Down
32 changes: 23 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const merge = require('lodash/merge');
const path = require('path');
const Chalk = require('chalk').constructor;
const { diffImageToSnapshot } = require('./diff-snapshot');
const fs = require('fs');

const SNAPSHOTS_DIR = '__image_snapshots__';

Expand Down Expand Up @@ -47,15 +48,28 @@ function configureToMatchImageSnapshot({
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)}`);

const result = diffImageToSnapshot({
receivedImageBuffer: received,
snapshotIdentifier,
snapshotsDir: customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR),
updateSnapshot: snapshotState._updateSnapshot === 'all',
customDiffConfig: Object.assign({}, commonCustomDiffConfig, customDiffConfig),
failureThreshold,
failureThresholdType,
});
const snapshotsDir = customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR);
const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}-snap.png`);

if (snapshotState._updateSnapshot === 'none' && !fs.existsSync(baselineSnapshotPath)) {
return {
pass: false,
message: () => `New snapshot was ${chalk.bold.red('not written')}. The update flag must be explicitly ` +
'passed to write a new snapshot.\n\n + This is likely because this test is run in a continuous ' +
'integration (CI) environment in which snapshots are not written by default.\n\n',
};
}

const result =
diffImageToSnapshot({
receivedImageBuffer: received,
snapshotsDir,
snapshotIdentifier,
updateSnapshot: snapshotState._updateSnapshot === 'all',
customDiffConfig: Object.assign({}, commonCustomDiffConfig, customDiffConfig),
failureThreshold,
failureThresholdType,
});

let pass = true;
/*
Expand Down

0 comments on commit 967d6f8

Please sign in to comment.