Skip to content

Commit 4bad752

Browse files
authored
feat: add onlyDiff in options (americanexpress#317)
1 parent 8a37453 commit 4bad752

7 files changed

Lines changed: 69 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
113113
* `customReceivedDir`: A custom absolute path of a directory to keep this received image in
114114
* `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. If a path is given, the path will be created inside the snapshot/diff directories.
115115
* `diffDirection`: (default: `horizontal`) (options `horizontal` or `vertical`) Changes diff image layout direction
116+
* `onlyDiff`: (default: `false`) Either only include the difference between the baseline and the received image in the diff image, or include the 3 images (following the direction set by `diffDirection`).
116117
* `noColors`: Removes coloring from console output, useful if storing the results in a file
117118
* `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.
118119
* `failureThresholdType`: (default `pixel`) (options `percent` or `pixel`) Sets the type of threshold that would trigger a failure.

__tests__/__snapshots__/index.spec.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ exports[`toMatchImageSnapshot passes diffImageToSnapshot everything it needs to
4747
"diffDirection": "horizontal",
4848
"failureThreshold": 0,
4949
"failureThresholdType": "pixel",
50+
"onlyDiff": false,
5051
"receivedDir": undefined,
5152
"receivedImageBuffer": "pretendthisisanimagebuffer",
5253
"snapshotIdentifier": "test-spec-js-test-1-snap",

__tests__/index.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ describe('toMatchImageSnapshot', () => {
443443
comparisonMethod: 'pixelmatch',
444444
customDiffConfig: {},
445445
diffDirection: 'horizontal',
446+
onlyDiff: false,
446447
failureThreshold: 0,
447448
failureThresholdType: 'pixel',
448449
receivedImageBuffer: undefined,
@@ -513,6 +514,7 @@ describe('toMatchImageSnapshot', () => {
513514
storeReceivedOnFailure: true,
514515
diffDir: path.join('path', 'to', 'my-custom-diff-dir'),
515516
diffDirection: 'vertical',
517+
onlyDiff: false,
516518
updateSnapshot: false,
517519
updatePassedSnapshot: true,
518520
failureThreshold: 1,
@@ -572,6 +574,7 @@ describe('toMatchImageSnapshot', () => {
572574
receivedDir: path.join('path', 'to', 'my-custom-received-dir'),
573575
diffDir: path.join('path', 'to', 'my-custom-diff-dir'),
574576
diffDirection: 'horizontal',
577+
onlyDiff: false,
575578
storeReceivedOnFailure: true,
576579
updateSnapshot: false,
577580
updatePassedSnapshot: false,

__tests__/integration.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,45 @@ describe('toMatchImageSnapshot', () => {
410410
.toBe(false);
411411
});
412412

413+
it('only outputs the diff when onlyDiff is enabled', () => {
414+
const failureImageData = fs.readFileSync(fromStubs('TestImageUpdate1pxOff.png'));
415+
const imageFailureOnlyDiffData =
416+
fs.readFileSync(fromStubs('TestImageUpdate1pxOff-onlyDiff-diff.png'));
417+
418+
const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
419+
const pathToResultImage = path.join(__dirname, diffOutputDir(), `${customSnapshotIdentifier}-diff.png`);
420+
// First we need to write a new snapshot image
421+
expect(
422+
() => expect(imageData)
423+
.toMatchImageSnapshot({
424+
customSnapshotIdentifier,
425+
onlyDiff: true,
426+
})
427+
)
428+
.not
429+
.toThrowError();
430+
431+
// then test against a different image
432+
expect(
433+
() => expect(failureImageData)
434+
.toMatchImageSnapshot({
435+
customSnapshotIdentifier,
436+
onlyDiff: true,
437+
// required for coverage
438+
runInProcess: true,
439+
})
440+
)
441+
.toThrow(/Expected image to match or be a close match/);
442+
443+
expect(fs.existsSync(pathToResultImage))
444+
.toBe(true);
445+
446+
expect(fs.readFileSync(pathToResultImage)).toEqual(imageFailureOnlyDiffData);
447+
// just because file was written does not mean it is a png image
448+
expect(sizeOf(pathToResultImage))
449+
.toHaveProperty('type', 'png');
450+
});
451+
413452
it('handles diffs for large images', () => {
414453
const largeImageData = fs.readFileSync(fromStubs('LargeTestImage.png'));
415454
const largeFailureImageData = fs.readFileSync(fromStubs('LargeTestImageFailure.png'));
3.68 KB
Loading

src/diff-snapshot.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,24 @@ const shouldFail = ({
184184
};
185185
};
186186

187+
function composeDiff(options) {
188+
const {
189+
diffDirection, baselineImage, diffImage, receivedImage, imageWidth, imageHeight, onlyDiff,
190+
} = options;
191+
const composer = new ImageComposer({
192+
direction: diffDirection,
193+
});
194+
195+
if (onlyDiff) {
196+
composer.addImage(diffImage, imageWidth, imageHeight);
197+
} else {
198+
composer.addImage(baselineImage, imageWidth, imageHeight);
199+
composer.addImage(diffImage, imageWidth, imageHeight);
200+
composer.addImage(receivedImage, imageWidth, imageHeight);
201+
}
202+
return composer;
203+
}
204+
187205
function diffImageToSnapshot(options) {
188206
const {
189207
receivedImageBuffer,
@@ -193,6 +211,7 @@ function diffImageToSnapshot(options) {
193211
receivedDir = path.join(options.snapshotsDir, '__received_output__'),
194212
diffDir = path.join(options.snapshotsDir, '__diff_output__'),
195213
diffDirection,
214+
onlyDiff = false,
196215
updateSnapshot = false,
197216
updatePassedSnapshot = false,
198217
customDiffConfig = {},
@@ -281,14 +300,10 @@ function diffImageToSnapshot(options) {
281300
}
282301

283302
mkdirp.sync(path.dirname(diffOutputPath));
284-
const composer = new ImageComposer({
285-
direction: diffDirection,
303+
const composer = composeDiff({
304+
diffDirection, baselineImage, diffImage, receivedImage, imageWidth, imageHeight, onlyDiff,
286305
});
287306

288-
composer.addImage(baselineImage, imageWidth, imageHeight);
289-
composer.addImage(diffImage, imageWidth, imageHeight);
290-
composer.addImage(receivedImage, imageWidth, imageHeight);
291-
292307
const composerParams = composer.getParams();
293308

294309
const compositeResultImage = new PNG({
@@ -335,6 +350,7 @@ function diffImageToSnapshot(options) {
335350
return result;
336351
}
337352

353+
338354
function runDiffImageToSnapshot(options) {
339355
options.receivedImageBuffer = options.receivedImageBuffer.toString('base64');
340356

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ function configureToMatchImageSnapshot({
138138
storeReceivedOnFailure: commonStoreReceivedOnFailure = false,
139139
customReceivedDir: commonCustomReceivedDir,
140140
customDiffDir: commonCustomDiffDir,
141+
onlyDiff: commonOnlyDiff = false,
141142
diffDirection: commonDiffDirection = 'horizontal',
142143
noColors: commonNoColors,
143144
failureThreshold: commonFailureThreshold = 0,
@@ -156,6 +157,7 @@ function configureToMatchImageSnapshot({
156157
storeReceivedOnFailure = commonStoreReceivedOnFailure,
157158
customReceivedDir = commonCustomReceivedDir,
158159
customDiffDir = commonCustomDiffDir,
160+
onlyDiff = commonOnlyDiff,
159161
diffDirection = commonDiffDirection,
160162
customDiffConfig = {},
161163
noColors = commonNoColors,
@@ -218,6 +220,7 @@ function configureToMatchImageSnapshot({
218220
receivedDir,
219221
diffDir,
220222
diffDirection,
223+
onlyDiff,
221224
snapshotIdentifier,
222225
updateSnapshot: snapshotState._updateSnapshot === 'all',
223226
customDiffConfig: Object.assign({}, commonCustomDiffConfig, customDiffConfig),

0 commit comments

Comments
 (0)