Skip to content

Commit

Permalink
feat(options): Allow customSnapshotIdentifier to be a function (ameri…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jyrno42 authored and anescobar1991 committed Jun 7, 2019
1 parent 19bff61 commit 0277834
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
* Please note the `threshold` set in the `customDiffConfig` is the per pixel sensitivity threshold. For example with a source pixel colour of `#ffffff` (white) and a comparison pixel colour of `#fcfcfc` (really light grey) if you set the threshold to 0 then it would trigger a failure *on that pixel*. However if you were to use say 0.5 then it wouldn't, the colour difference would need to be much more extreme to trigger a failure on that pixel, say `#000000` (black)
* `customSnapshotsDir`: A custom absolute path of a directory to keep this snapshot in
* `customDiffDir`: A custom absolute path of a directory to keep this diff in
* `customSnapshotIdentifier`: A custom name to give this snapshot. If not provided one is computed automatically
* `customSnapshotIdentifier`: A custom name to give this snapshot. If not provided one is computed automatically. When a function is provided it is called with an object containing `testPath`, `currentTestName`, `counter` and `defaultIdentifier` as its first argument. The function must return an identifier to use for the snapshot.
* `diffDirection`: (default: `horizontal`) (options `horizontal` or `vertical`) Changes diff image layout direction
* `noColors`: (default `false`) Removes colouring from console output, useful if storing the results in a file
* `failureThreshold`: (default `0`) Sets the threshold that would trigger a test failure based on the `failureThresholdType` selected. This is different to the `customDiffConfig.threshold` above, that is the per pixel failure threshold, this is the failure threshold for the entire comparison.
Expand Down
2 changes: 2 additions & 0 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ See diff for details: path/to/result.png"
`;

exports[`toMatchImageSnapshot when retryTimes is set should throw an error when called without customSnapshotIdentifier 2`] = `"A unique customSnapshotIdentifier must be set when jest.retryTimes() is used"`;

exports[`toMatchImageSnapshot when retryTimes is set should throw an error when called without customSnapshotIdentifier 4`] = `"A unique customSnapshotIdentifier must be set when jest.retryTimes() is used"`;
19 changes: 19 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,22 @@ describe('toMatchImageSnapshot', () => {
const { runDiffImageToSnapshot } = require('../src/diff-snapshot');

expect(runDiffImageToSnapshot.mock.calls[0][0].snapshotIdentifier).toBe('custom-name');

matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: () => 'functional-name' });
expect(runDiffImageToSnapshot.mock.calls[1][0].snapshotIdentifier).toBe('functional-name');

matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: () => '' });
expect(runDiffImageToSnapshot.mock.calls[2][0].snapshotIdentifier).toBe('test-spec-js-test-3');

const mockCustomSnap = jest.fn();
matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: mockCustomSnap });

expect(mockCustomSnap).toHaveBeenCalledWith({
testPath: mockTestContext.testPath,
currentTestName: mockTestContext.currentTestName,
counter: 4,
defaultIdentifier: 'test-spec-js-test-4',
});
});

it('attempts to update snapshots if snapshotState has updateSnapshot flag set', () => {
Expand Down Expand Up @@ -445,6 +461,9 @@ describe('toMatchImageSnapshot', () => {

expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot())
.toThrowErrorMatchingSnapshot();

expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot({ customSnapshotIdentifier: () => '' }))
.toThrowErrorMatchingSnapshot();
});

it('should only increment unmatched when test fails in excess of retryTimes', () => {
Expand Down
19 changes: 18 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,29 @@ function createSnapshotIdentifier({
customSnapshotIdentifier,
snapshotState,
}) {
const snapshotIdentifier = customSnapshotIdentifier || kebabCase(`${path.basename(testPath)}-${currentTestName}-${snapshotState._counters.get(currentTestName)}`);
const counter = snapshotState._counters.get(currentTestName);
const defaultIdentifier = kebabCase(`${path.basename(testPath)}-${currentTestName}-${counter}`);

let snapshotIdentifier = customSnapshotIdentifier || defaultIdentifier;

if (typeof customSnapshotIdentifier === 'function') {
const customRes = customSnapshotIdentifier({
testPath, currentTestName, counter, defaultIdentifier,
});

if (retryTimes && !customRes) {
throw new Error('A unique customSnapshotIdentifier must be set when jest.retryTimes() is used');
}

snapshotIdentifier = customRes || defaultIdentifier;
}

if (retryTimes) {
if (!customSnapshotIdentifier) throw new Error('A unique customSnapshotIdentifier must be set when jest.retryTimes() is used');

timesCalled.set(snapshotIdentifier, (timesCalled.get(snapshotIdentifier) || 0) + 1);
}

return snapshotIdentifier;
}

Expand Down

0 comments on commit 0277834

Please sign in to comment.