jest-image-snapshot 2.9.0 → 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/.travis.yml +8 -0
  2. package/CODEOWNERS +3 -0
  3. package/README.md +145 -74
  4. package/jest-image-snapshot.png +0 -0
  5. package/package.json +2 -1
  6. package/src/diff-snapshot.js +10 -1
  7. package/src/index.js +23 -7
  8. package/__tests__/.eslintrc.json +0 -3
  9. package/__tests__/__image_snapshots__/integration-6-snap.png +0 -0
  10. package/__tests__/__image_snapshots__/integration-spec-js-to-match-image-snapshot-happy-path-matches-an-identical-snapshot-1-snap.png +0 -0
  11. package/__tests__/__image_snapshots__/integration-update-snap.png +0 -0
  12. package/__tests__/__snapshots__/diff-snapshot.spec.js.snap +0 -3
  13. package/__tests__/__snapshots__/index.spec.js.snap +0 -32
  14. package/__tests__/diff-snapshot.spec.js +0 -584
  15. package/__tests__/image-composer.spec.js +0 -81
  16. package/__tests__/index.spec.js +0 -514
  17. package/__tests__/integration.spec.js +0 -310
  18. package/__tests__/stubs/TestImage.png +0 -0
  19. package/__tests__/stubs/TestImage150x150.png +0 -0
  20. package/__tests__/stubs/TestImageFailure.png +0 -0
  21. package/__tests__/stubs/TestImageFailureOversize.png +0 -0
  22. package/__tests__/stubs/TestImageUpdate1pxOff.png +0 -0
  23. package/examples/.eslintrc.json +0 -3
  24. package/examples/README.md +0 -9
  25. package/examples/__tests__/__image_snapshots__/local-image-spec-js-works-reading-an-image-from-the-local-file-system-1-snap.png +0 -0
  26. package/examples/__tests__/__image_snapshots__/puppeteer-example-spec-js-jest-image-snapshot-usage-with-an-image-received-from-puppeteer-works-1-snap.png +0 -0
  27. package/examples/__tests__/local-image.spec.js +0 -24
  28. package/examples/__tests__/puppeteer-example.spec.js +0 -37
  29. package/examples/__tests__/stubs/image.png +0 -0
  30. package/examples/image-reporter.js +0 -46
  31. package/examples/jest-setup.js +0 -19
  32. package/examples/package.json +0 -26
@@ -1,514 +0,0 @@
1
- /*
2
- * Copyright (c) 2017 American Express Travel Related Services Company, Inc.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
- * in compliance with the License. You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software distributed under the License
10
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
- * or implied. See the License for the specific language governing permissions and limitations under
12
- * the License.
13
- */
14
-
15
- /* eslint-disable global-require */
16
- const fs = require('fs');
17
- const path = require('path');
18
-
19
- describe('toMatchImageSnapshot', () => {
20
- function setupMock(diffImageToSnapshotResult) {
21
- jest.doMock('../src/diff-snapshot', () => ({
22
- runDiffImageToSnapshot: jest.fn(() => diffImageToSnapshotResult),
23
- }));
24
-
25
- const mockFs = Object.assign({}, fs, {
26
- existsSync: jest.fn(),
27
- unlinkSync: jest.fn(),
28
- });
29
- mockFs.existsSync.mockImplementation(p => p === 'test/path');
30
- jest.mock('fs', () => mockFs);
31
-
32
- return {
33
- mockFs,
34
- };
35
- }
36
-
37
- beforeEach(() => {
38
- // In tests, skip reporting (skip snapshotState update to not mess with our test report)
39
- global.UNSTABLE_SKIP_REPORTING = true;
40
- jest.resetModules();
41
- jest.resetAllMocks();
42
- });
43
-
44
- afterEach(() => {
45
- jest.unmock('fs');
46
- });
47
-
48
- it('should throw an error if used with .not matcher', () => {
49
- const mockDiffResult = {
50
- pass: true,
51
- diffOutputPath: 'path/to/result.png',
52
- diffRatio: 0,
53
- diffPixelCount: 0,
54
- };
55
-
56
- setupMock(mockDiffResult);
57
- const { toMatchImageSnapshot } = require('../src/index');
58
- expect.extend({ toMatchImageSnapshot });
59
-
60
- expect(() => expect('pretendthisisanimagebuffer').not.toMatchImageSnapshot())
61
- .toThrowErrorMatchingSnapshot();
62
- });
63
-
64
- it('should pass when snapshot is similar enough or same as baseline snapshot', () => {
65
- const mockDiffResult = {
66
- pass: true,
67
- diffOutputPath: 'path/to/result.png',
68
- diffRatio: 0,
69
- diffPixelCount: 0,
70
- };
71
- setupMock(mockDiffResult);
72
-
73
- const { toMatchImageSnapshot } = require('../src/index');
74
- expect.extend({ toMatchImageSnapshot });
75
-
76
- expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot())
77
- .not.toThrow();
78
- });
79
-
80
- it('should fail when snapshot has a difference beyond allowed threshold', () => {
81
- const mockDiffResult = {
82
- pass: false,
83
- diffOutputPath: 'path/to/result.png',
84
- diffRatio: 0.8,
85
- diffPixelCount: 600,
86
- };
87
-
88
- setupMock(mockDiffResult);
89
- const { toMatchImageSnapshot } = require('../src/index');
90
- expect.extend({ toMatchImageSnapshot });
91
-
92
- expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot())
93
- .toThrowErrorMatchingSnapshot();
94
- });
95
-
96
- it('should fail when snapshot is a different size than the baseline', () => {
97
- const mockDiffResult = {
98
- pass: false,
99
- diffSize: true,
100
- imageDimensions: {
101
- receivedHeight: 100,
102
- receivedWidth: 100,
103
- baselineHeight: 10,
104
- baselineWidth: 10,
105
- },
106
- diffOutputPath: 'path/to/result.png',
107
- diffRatio: 0.8,
108
- diffPixelCount: 600,
109
- };
110
-
111
- setupMock(mockDiffResult);
112
- const { toMatchImageSnapshot } = require('../src/index');
113
- expect.extend({ toMatchImageSnapshot });
114
-
115
- expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot())
116
- .toThrow(/Expected image to be the same size as the snapshot/);
117
- });
118
-
119
- it('should use noColors options if passed as true and not style error message', () => {
120
- const mockDiffResult = {
121
- pass: false,
122
- diffOutputPath: 'path/to/result.png',
123
- diffRatio: 0.4,
124
- diffPixelCount: 600,
125
- };
126
-
127
- setupMock(mockDiffResult);
128
- const { toMatchImageSnapshot } = require('../src/index');
129
- expect.extend({ toMatchImageSnapshot });
130
-
131
- expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot({ noColors: true }))
132
- .toThrowErrorMatchingSnapshot();
133
- });
134
-
135
- it('should use custom pixelmatch configuration if passed in', () => {
136
- const mockTestContext = {
137
- testPath: 'path/to/test.spec.js',
138
- currentTestName: 'test1',
139
- isNot: false,
140
- snapshotState: {
141
- _counters: new Map(),
142
- _updateSnapshot: 'new',
143
- updated: undefined,
144
- added: true,
145
- },
146
- };
147
-
148
- const mockDiffResult = {
149
- pass: false,
150
- diffOutputPath: 'path/to/result.png',
151
- diffRatio: 0.8,
152
- diffPixelCount: 600,
153
- };
154
-
155
- setupMock(mockDiffResult);
156
- const { toMatchImageSnapshot } = require('../src/index');
157
- const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
158
-
159
- const customDiffConfig = { threshold: 0.3 };
160
- matcherAtTest('pretendthisisanimagebuffer', { customDiffConfig });
161
- const { runDiffImageToSnapshot } = require('../src/diff-snapshot');
162
- expect(runDiffImageToSnapshot.mock.calls[0][0].customDiffConfig).toEqual(customDiffConfig);
163
- });
164
-
165
- it('passes diffImageToSnapshot everything it needs to create a snapshot and compare if needed', () => {
166
- const mockTestContext = {
167
- testPath: 'path/to/test.spec.js',
168
- currentTestName: 'test',
169
- isNot: false,
170
- snapshotState: {
171
- _counters: new Map(),
172
- _updateSnapshot: 'new',
173
- updated: undefined,
174
- added: true,
175
- },
176
- };
177
-
178
- const mockDiffResult = {
179
- pass: false,
180
- diffOutputPath: 'path/to/result.png',
181
- diffRatio: 0.8,
182
- diffPixelCount: 600,
183
- };
184
-
185
- setupMock(mockDiffResult);
186
- const { toMatchImageSnapshot } = require('../src/index');
187
- const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
188
-
189
- matcherAtTest('pretendthisisanimagebuffer');
190
- const { runDiffImageToSnapshot } = require('../src/diff-snapshot');
191
-
192
- const dataArg = runDiffImageToSnapshot.mock.calls[0][0];
193
- // This is to make the test work on windows
194
- dataArg.snapshotsDir = dataArg.snapshotsDir.replace(/\\/g, '/');
195
-
196
- expect(dataArg).toMatchSnapshot();
197
- });
198
-
199
- it('passes uses user passed snapshot name if given', () => {
200
- const mockTestContext = {
201
- testPath: 'path/to/test.spec.js',
202
- currentTestName: 'test',
203
- isNot: false,
204
- snapshotState: {
205
- _counters: new Map(),
206
- _updateSnapshot: 'new',
207
- updated: undefined,
208
- added: true,
209
- },
210
- };
211
-
212
- const mockDiffResult = {
213
- pass: false,
214
- diffOutputPath: 'path/to/result.png',
215
- diffRatio: 0.8,
216
- diffPixelCount: 600,
217
- };
218
-
219
- setupMock(mockDiffResult);
220
- const { toMatchImageSnapshot } = require('../src/index');
221
- const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
222
-
223
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: 'custom-name' });
224
- const { runDiffImageToSnapshot } = require('../src/diff-snapshot');
225
-
226
- expect(runDiffImageToSnapshot.mock.calls[0][0].snapshotIdentifier).toBe('custom-name');
227
-
228
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: () => 'functional-name' });
229
- expect(runDiffImageToSnapshot.mock.calls[1][0].snapshotIdentifier).toBe('functional-name');
230
-
231
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: () => '' });
232
- expect(runDiffImageToSnapshot.mock.calls[2][0].snapshotIdentifier).toBe('test-spec-js-test-3');
233
-
234
- const mockCustomSnap = jest.fn();
235
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: mockCustomSnap });
236
-
237
- expect(mockCustomSnap).toHaveBeenCalledWith({
238
- testPath: mockTestContext.testPath,
239
- currentTestName: mockTestContext.currentTestName,
240
- counter: 4,
241
- defaultIdentifier: 'test-spec-js-test-4',
242
- });
243
- });
244
-
245
- it('attempts to update snapshots if snapshotState has updateSnapshot flag set', () => {
246
- const mockTestContext = {
247
- testPath: 'path/to/test.spec.js',
248
- currentTestName: 'test1',
249
- isNot: false,
250
- snapshotState: {
251
- _counters: new Map(),
252
- _updateSnapshot: 'all',
253
- updated: undefined,
254
- added: true,
255
- },
256
- };
257
- const mockDiffResult = { updated: true };
258
-
259
- setupMock(mockDiffResult);
260
- const { toMatchImageSnapshot } = require('../src/index');
261
- const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
262
-
263
- matcherAtTest('pretendthisisanimagebuffer');
264
- const { runDiffImageToSnapshot } = require('../src/diff-snapshot');
265
-
266
- expect(runDiffImageToSnapshot.mock.calls[0][0].updateSnapshot).toBe(true);
267
- });
268
-
269
- it('should work when a new snapshot is added', () => {
270
- const mockTestContext = {
271
- testPath: 'path/to/test.spec.js',
272
- currentTestName: 'test1',
273
- isNot: false,
274
- snapshotState: {
275
- _counters: new Map(),
276
- update: false,
277
- _updateSnapshot: 'new',
278
- updated: undefined,
279
- added: true,
280
- },
281
- };
282
- const mockDiff = jest.fn();
283
- jest.doMock('../src/diff-snapshot', () => ({
284
- runDiffImageToSnapshot: mockDiff,
285
- }));
286
-
287
- const mockFs = Object.assign({}, fs, {
288
- existsSync: jest.fn(),
289
- unlinkSync: jest.fn(),
290
- });
291
-
292
- mockFs.existsSync.mockReturnValueOnce(false);
293
- mockDiff.mockReturnValueOnce({ added: true });
294
-
295
- const { toMatchImageSnapshot } = require('../src/index');
296
- const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
297
- expect(matcherAtTest('pretendthisisanimagebuffer')).toHaveProperty('pass', true);
298
- expect(mockDiff).toHaveBeenCalled();
299
- });
300
-
301
- it('should fail when a new snapshot is added in ci', () => {
302
- const mockTestContext = {
303
- testPath: 'path/to/test.spec.js',
304
- currentTestName: 'test1',
305
- isNot: false,
306
- snapshotState: {
307
- _counters: new Map(),
308
- update: false,
309
- _updateSnapshot: 'none',
310
- updated: undefined,
311
- added: true,
312
- },
313
- };
314
-
315
- const mockDiff = jest.fn();
316
- jest.doMock('../src/diff-snapshot', () => ({
317
- diffImageToSnapshot: mockDiff,
318
- }));
319
-
320
- const mockFs = Object.assign({}, fs, {
321
- existsSync: jest.fn(),
322
- unlinkSync: jest.fn(),
323
- });
324
-
325
- mockFs.existsSync.mockReturnValueOnce(false);
326
-
327
-
328
- const { toMatchImageSnapshot } = require('../src/index');
329
- const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
330
- const result = matcherAtTest('pretendthisisanimagebuffer');
331
- expect(result).toHaveProperty('pass', false);
332
- expect(result).toHaveProperty('message');
333
- expect(result.message()).toContain('continuous integration');
334
- expect(mockDiff).not.toHaveBeenCalled();
335
- });
336
-
337
- it('should work when a snapshot is updated', () => {
338
- const mockTestContext = {
339
- testPath: 'path/to/test.spec.js',
340
- currentTestName: 'test1',
341
- isNot: false,
342
- snapshotState: {
343
- _counters: new Map(),
344
- update: true,
345
- updated: undefined,
346
- added: undefined,
347
- },
348
- };
349
- const mockDiffResult = { updated: true };
350
-
351
- setupMock(mockDiffResult);
352
- const { toMatchImageSnapshot } = require('../src/index');
353
- const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
354
- expect(() => matcherAtTest('pretendthisisanimagebuffer')).not.toThrow();
355
- });
356
-
357
- it('can provide custom defaults', () => {
358
- const mockTestContext = {
359
- testPath: path.join('path', 'to', 'test.spec.js'),
360
- currentTestName: 'test1',
361
- isNot: false,
362
- snapshotState: {
363
- _counters: new Map(),
364
- update: true,
365
- updated: undefined,
366
- added: undefined,
367
- },
368
- };
369
- setupMock({ updated: true });
370
-
371
- const runDiffImageToSnapshot = jest.fn(() => ({}));
372
- jest.doMock('../src/diff-snapshot', () => ({
373
- runDiffImageToSnapshot,
374
- }));
375
-
376
- const Chalk = jest.fn();
377
- jest.doMock('chalk', () => ({
378
- constructor: Chalk,
379
- }));
380
- const { configureToMatchImageSnapshot } = require('../src/index');
381
- const customConfig = { perceptual: true };
382
- const toMatchImageSnapshot = configureToMatchImageSnapshot({
383
- customDiffConfig: customConfig,
384
- customSnapshotsDir: path.join('path', 'to', 'my-custom-snapshots-dir'),
385
- customDiffDir: path.join('path', 'to', 'my-custom-diff-dir'),
386
- noColors: true,
387
- });
388
- expect.extend({ toMatchImageSnapshot });
389
- const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
390
-
391
- matcherAtTest();
392
-
393
- expect(runDiffImageToSnapshot).toHaveBeenCalledWith({
394
- customDiffConfig: {
395
- perceptual: true,
396
- },
397
- snapshotIdentifier: 'test-spec-js-test-1-1',
398
- snapshotsDir: path.join('path', 'to', 'my-custom-snapshots-dir'),
399
- diffDir: path.join('path', 'to', 'my-custom-diff-dir'),
400
- diffDirection: 'horizontal',
401
- updateSnapshot: false,
402
- updatePassedSnapshot: false,
403
- failureThreshold: 0,
404
- failureThresholdType: 'pixel',
405
- });
406
- expect(Chalk).toHaveBeenCalledWith({
407
- enabled: false,
408
- });
409
- });
410
-
411
- it('should only increment matched when test passed', () => {
412
- global.UNSTABLE_SKIP_REPORTING = false;
413
-
414
- const mockTestContext = {
415
- testPath: 'path/to/test.spec.js',
416
- currentTestName: 'test',
417
- isNot: false,
418
- snapshotState: {
419
- _counters: new Map(),
420
- _updateSnapshot: 'new',
421
- updated: undefined,
422
- added: true,
423
- unmatched: 0,
424
- matched: 0,
425
- },
426
- };
427
-
428
- const mockDiffResult = {
429
- pass: true,
430
- diffOutputPath: 'path/to/result.png',
431
- diffRatio: 0,
432
- diffPixelCount: 0,
433
- };
434
-
435
- setupMock(mockDiffResult);
436
- const { toMatchImageSnapshot } = require('../src/index');
437
- const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
438
-
439
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: 'custom-name' });
440
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: 'custom-name' });
441
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: 'custom-name' });
442
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: 'custom-name' });
443
- expect(mockTestContext.snapshotState.matched).toBe(4);
444
- });
445
-
446
- describe('when retryTimes is set', () => {
447
- beforeEach(() => { global[Symbol.for('RETRY_TIMES')] = 3; });
448
- afterEach(() => { global[Symbol.for('RETRY_TIMES')] = undefined; });
449
-
450
- it('should throw an error when called without customSnapshotIdentifier', () => {
451
- const mockDiffResult = {
452
- pass: true,
453
- diffOutputPath: 'path/to/result.png',
454
- diffRatio: 0,
455
- diffPixelCount: 0,
456
- };
457
-
458
- setupMock(mockDiffResult);
459
- const { toMatchImageSnapshot } = require('../src/index');
460
- expect.extend({ toMatchImageSnapshot });
461
-
462
- expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot())
463
- .toThrowErrorMatchingSnapshot();
464
-
465
- expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot({ customSnapshotIdentifier: () => '' }))
466
- .toThrowErrorMatchingSnapshot();
467
- });
468
-
469
- it('should only increment unmatched when test fails in excess of retryTimes', () => {
470
- global.UNSTABLE_SKIP_REPORTING = false;
471
-
472
- const mockTestContext = {
473
- testPath: 'path/to/test.spec.js',
474
- currentTestName: 'test',
475
- isNot: false,
476
- snapshotState: {
477
- _counters: new Map(),
478
- _updateSnapshot: 'new',
479
- updated: undefined,
480
- added: true,
481
- unmatched: 0,
482
- },
483
- };
484
-
485
- const mockDiffResult = {
486
- pass: false,
487
- diffOutputPath: 'path/to/result.png',
488
- diffRatio: 0.8,
489
- diffPixelCount: 600,
490
- };
491
-
492
- setupMock(mockDiffResult);
493
- const { toMatchImageSnapshot } = require('../src/index');
494
- const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
495
-
496
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: 'custom-name' });
497
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: 'custom-name' });
498
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: 'custom-name' });
499
- matcherAtTest('pretendthisisanimagebuffer', { customSnapshotIdentifier: 'custom-name' });
500
- expect(mockTestContext.snapshotState.unmatched).toBe(1);
501
- });
502
- });
503
- });
504
-
505
- describe('updateSnapshotState', () => {
506
- it('mutates original state', () => {
507
- const { updateSnapshotState } = require('../src/index');
508
- global.UNSTABLE_SKIP_REPORTING = false;
509
- const originalState = { some: 'value' };
510
- updateSnapshotState(originalState, { another: 'val' });
511
-
512
- expect(originalState).toEqual({ some: 'value', another: 'val' });
513
- });
514
- });