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,584 +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('diff-snapshot', () => {
20
- beforeEach(() => {
21
- jest.resetModules();
22
- jest.resetAllMocks();
23
- });
24
-
25
- describe('runDiffImageToSnapshot', () => {
26
- const mockSpawnSync = jest.fn();
27
- const fakeRequest = {
28
- receivedImageBuffer: Buffer.from('abcdefg'),
29
- snapshotIdentifier: 'foo',
30
- snapshotsDir: 'bar',
31
- updateSnapshot: false,
32
- failureThreshold: 0,
33
- failureThresholdType: 'pixel',
34
- };
35
-
36
- function setupTest(spawnReturn) {
37
- mockSpawnSync.mockReturnValue(spawnReturn);
38
- jest.mock('child_process', () => ({ spawnSync: mockSpawnSync }));
39
- const { runDiffImageToSnapshot } = require('../src/diff-snapshot');
40
- return runDiffImageToSnapshot;
41
- }
42
-
43
- it('runs external process and returns result', () => {
44
- const runDiffImageToSnapshot = setupTest({
45
- status: 0, output: [null, null, null, JSON.stringify({ add: true, updated: false })],
46
- });
47
-
48
- expect(runDiffImageToSnapshot(fakeRequest)).toEqual({ add: true, updated: false });
49
-
50
- expect(mockSpawnSync).toBeCalled();
51
- });
52
-
53
- it('throws when process returns a non-zero status', () => {
54
- const runDiffImageToSnapshot = setupTest({ status: 1 });
55
- expect(() => runDiffImageToSnapshot(fakeRequest)).toThrow();
56
- });
57
- });
58
-
59
- describe('diffImageToSnapshot', () => {
60
- const mockSnapshotsDir = path.normalize('/path/to/snapshots');
61
- const mockDiffDir = path.normalize('/path/to/snapshots/__diff_output__');
62
- const mockSnapshotIdentifier = 'id1';
63
- const mockImagePath = './__tests__/stubs/TestImage.png';
64
- const mockImageBuffer = fs.readFileSync(mockImagePath);
65
- const mockBigImagePath = './__tests__/stubs/TestImage150x150.png';
66
- const mockBigImageBuffer = fs.readFileSync(mockBigImagePath);
67
- const mockFailImagePath = './__tests__/stubs/TestImageFailure.png';
68
- const mockFailImageBuffer = fs.readFileSync(mockFailImagePath);
69
- const mockMkdirSync = jest.fn();
70
- const mockMkdirpSync = jest.fn();
71
- const mockWriteFileSync = jest.fn();
72
- const mockPixelMatch = jest.fn();
73
-
74
- function setupTest({
75
- snapshotDirExists,
76
- snapshotExists,
77
- outputDirExists,
78
- defaultExists = true,
79
- pixelmatchResult = 0,
80
- }) {
81
- const mockFs = Object.assign({}, fs, {
82
- existsSync: jest.fn(),
83
- mkdirSync: mockMkdirSync,
84
- writeFileSync: mockWriteFileSync,
85
- readFileSync: jest.fn(),
86
- });
87
-
88
- jest.mock('fs', () => mockFs);
89
- jest.mock('mkdirp', () => ({ sync: mockMkdirpSync }));
90
- const { diffImageToSnapshot } = require('../src/diff-snapshot');
91
-
92
- mockFs.existsSync.mockImplementation((p) => {
93
- switch (p) {
94
- case path.join(mockSnapshotsDir, `${mockSnapshotIdentifier}-snap.png`):
95
- return snapshotExists;
96
- case mockDiffDir:
97
- return !!outputDirExists;
98
- case mockSnapshotsDir:
99
- return !!snapshotDirExists;
100
- default:
101
- return !!defaultExists;
102
- }
103
- });
104
- mockFs.readFileSync.mockImplementation((p) => {
105
- const bn = path.basename(p);
106
-
107
- if (bn === 'id1-snap.png' && snapshotExists) {
108
- return mockImageBuffer;
109
- }
110
-
111
- return null;
112
- });
113
-
114
- jest.mock('pixelmatch', () => mockPixelMatch);
115
- mockPixelMatch.mockImplementation(() => pixelmatchResult);
116
-
117
- return diffImageToSnapshot;
118
- }
119
-
120
- it('should run comparison if there is already a snapshot stored and updateSnapshot flag is not set', () => {
121
- const diffImageToSnapshot = setupTest({ snapshotExists: true });
122
- const result = diffImageToSnapshot({
123
- receivedImageBuffer: mockImageBuffer,
124
- snapshotIdentifier: mockSnapshotIdentifier,
125
- snapshotsDir: mockSnapshotsDir,
126
- diffDir: mockDiffDir,
127
- updateSnapshot: false,
128
- failureThreshold: 0,
129
- failureThresholdType: 'pixel',
130
- });
131
-
132
- expect(result).toMatchObject({
133
- diffOutputPath: path.join(mockSnapshotsDir, '__diff_output__', 'id1-diff.png'),
134
- diffRatio: 0,
135
- diffPixelCount: 0,
136
- pass: true,
137
- });
138
- // Check that pixelmatch was not called
139
- expect(mockPixelMatch).not.toHaveBeenCalled();
140
- });
141
-
142
- it('it should not write a diff if a test passes', () => {
143
- const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 0 });
144
- const result = diffImageToSnapshot({
145
- receivedImageBuffer: mockImageBuffer,
146
- snapshotIdentifier: mockSnapshotIdentifier,
147
- snapshotsDir: mockSnapshotsDir,
148
- diffDir: mockDiffDir,
149
- updateSnapshot: false,
150
- failureThreshold: 0,
151
- failureThresholdType: 'pixel',
152
- });
153
-
154
- expect(result).toMatchObject({
155
- diffOutputPath: path.join(mockSnapshotsDir, '__diff_output__', 'id1-diff.png'),
156
- diffRatio: 0,
157
- diffPixelCount: 0,
158
- pass: true,
159
- });
160
- // Check that pixelmatch was not called
161
- expect(mockPixelMatch).not.toHaveBeenCalled();
162
-
163
- // Check that that it did not attempt to write a diff
164
- expect(mockWriteFileSync.mock.calls).toEqual([]);
165
- });
166
-
167
- it('should write a diff image if the test fails', () => {
168
- const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 5000 });
169
- const result = diffImageToSnapshot({
170
- receivedImageBuffer: mockFailImageBuffer,
171
- snapshotIdentifier: mockSnapshotIdentifier,
172
- snapshotsDir: mockSnapshotsDir,
173
- diffDir: mockDiffDir,
174
- updateSnapshot: false,
175
- failureThreshold: 0,
176
- failureThresholdType: 'pixel',
177
- });
178
-
179
- expect(result).toMatchObject({
180
- diffOutputPath: path.join(mockSnapshotsDir, '__diff_output__', 'id1-diff.png'),
181
- diffRatio: 0.5,
182
- diffPixelCount: 5000,
183
- pass: false,
184
- });
185
- expect(mockPixelMatch).toHaveBeenCalledTimes(1);
186
- expect(mockPixelMatch).toHaveBeenCalledWith(
187
- expect.any(Buffer),
188
- expect.any(Buffer),
189
- expect.any(Buffer),
190
- 100,
191
- 100,
192
- { threshold: 0.01 }
193
- );
194
-
195
- expect(mockWriteFileSync).toHaveBeenCalledTimes(1);
196
- });
197
-
198
- it('should fail if image passed is a different size', () => {
199
- const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 5000 });
200
- const result = diffImageToSnapshot({
201
- receivedImageBuffer: mockBigImageBuffer,
202
- snapshotIdentifier: mockSnapshotIdentifier,
203
- snapshotsDir: mockSnapshotsDir,
204
- diffDir: mockDiffDir,
205
- updateSnapshot: false,
206
- failureThreshold: 0,
207
- failureThresholdType: 'pixel',
208
- });
209
-
210
- expect(result).toMatchObject({
211
- diffOutputPath: path.join(mockSnapshotsDir, '__diff_output__', 'id1-diff.png'),
212
- pass: false,
213
- });
214
- expect(mockPixelMatch).toHaveBeenCalledTimes(1);
215
- expect(mockPixelMatch).toHaveBeenCalledWith(
216
- expect.any(Buffer),
217
- expect.any(Buffer),
218
- expect.any(Buffer),
219
- 150,
220
- 150,
221
- { threshold: 0.01 }
222
- );
223
-
224
- expect(mockWriteFileSync).toHaveBeenCalledTimes(1);
225
- });
226
-
227
- it('should pass <= failureThreshold pixel', () => {
228
- const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 250 });
229
- const result = diffImageToSnapshot({
230
- receivedImageBuffer: mockFailImageBuffer,
231
- snapshotIdentifier: mockSnapshotIdentifier,
232
- snapshotsDir: mockSnapshotsDir,
233
- diffDir: mockDiffDir,
234
- updateSnapshot: false,
235
- failureThreshold: 250,
236
- failureThresholdType: 'pixel',
237
- });
238
-
239
- expect(result.pass).toBe(true);
240
- expect(result.diffPixelCount).toBe(250);
241
- expect(result.diffRatio).toBe(0.025);
242
- });
243
-
244
- it('should pass = image checksums', () => {
245
- const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 0 });
246
- const result = diffImageToSnapshot({
247
- receivedImageBuffer: mockImageBuffer,
248
- snapshotIdentifier: mockSnapshotIdentifier,
249
- snapshotsDir: mockSnapshotsDir,
250
- diffDir: mockDiffDir,
251
- updateSnapshot: false,
252
- failureThreshold: 0,
253
- failureThresholdType: 'pixel',
254
- });
255
-
256
- expect(result.pass).toBe(true);
257
- expect(result.diffPixelCount).toBe(0);
258
- expect(result.diffRatio).toBe(0);
259
- });
260
-
261
- it('should run pixelmatch != image checksums', () => {
262
- const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 250 });
263
- const result = diffImageToSnapshot({
264
- receivedImageBuffer: mockFailImageBuffer,
265
- snapshotIdentifier: mockSnapshotIdentifier,
266
- snapshotsDir: mockSnapshotsDir,
267
- diffDir: mockDiffDir,
268
- updateSnapshot: false,
269
- failureThreshold: 250,
270
- failureThresholdType: 'pixel',
271
- });
272
-
273
- expect(mockPixelMatch).toHaveBeenCalledTimes(1);
274
- expect(result.pass).toBe(true);
275
- expect(result.diffPixelCount).toBe(250);
276
- expect(result.diffRatio).toBe(0.025);
277
- });
278
-
279
- it('should fail > failureThreshold pixel', () => {
280
- const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 251 });
281
- const result = diffImageToSnapshot({
282
- receivedImageBuffer: mockFailImageBuffer,
283
- snapshotIdentifier: mockSnapshotIdentifier,
284
- snapshotsDir: mockSnapshotsDir,
285
- diffDir: mockDiffDir,
286
- updateSnapshot: false,
287
- failureThreshold: 250,
288
- failureThresholdType: 'pixel',
289
- });
290
-
291
- expect(result.pass).toBe(false);
292
- expect(result.diffPixelCount).toBe(251);
293
- expect(result.diffRatio).toBe(0.0251);
294
- });
295
-
296
- it('should pass <= failureThreshold percent', () => {
297
- const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 250 });
298
- const result = diffImageToSnapshot({
299
- receivedImageBuffer: mockFailImageBuffer,
300
- snapshotIdentifier: mockSnapshotIdentifier,
301
- snapshotsDir: mockSnapshotsDir,
302
- diffDir: mockDiffDir,
303
- updateSnapshot: false,
304
- failureThreshold: 0.025,
305
- failureThresholdType: 'percent',
306
- });
307
-
308
- expect(result.pass).toBe(true);
309
- expect(result.diffPixelCount).toBe(250);
310
- expect(result.diffRatio).toBe(0.025);
311
- });
312
-
313
- it('should fail > failureThreshold percent', () => {
314
- const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 251 });
315
- const result = diffImageToSnapshot({
316
- receivedImageBuffer: mockFailImageBuffer,
317
- snapshotIdentifier: mockSnapshotIdentifier,
318
- snapshotsDir: mockSnapshotsDir,
319
- diffDir: mockDiffDir,
320
- updateSnapshot: false,
321
- failureThreshold: 0.025,
322
- failureThresholdType: 'percent',
323
- });
324
-
325
- expect(result.pass).toBe(false);
326
- expect(result.diffPixelCount).toBe(251);
327
- expect(result.diffRatio).toBe(0.0251);
328
- });
329
-
330
- it('should take the default diff config', () => {
331
- const diffImageToSnapshot = setupTest({ snapshotExists: true });
332
-
333
- diffImageToSnapshot({
334
- receivedImageBuffer: mockImageBuffer,
335
- snapshotIdentifier: mockSnapshotIdentifier,
336
- snapshotsDir: mockSnapshotsDir,
337
- diffDir: mockDiffDir,
338
- updateSnapshot: false,
339
- failureThreshold: 0,
340
- failureThresholdType: 'pixel',
341
- });
342
-
343
- // Check that pixelmatch was not called
344
- expect(mockPixelMatch).not.toHaveBeenCalled();
345
- });
346
-
347
- it('should merge custom configuration with default configuration if custom config is passed', () => {
348
- const diffImageToSnapshot = setupTest({ snapshotExists: true });
349
-
350
- diffImageToSnapshot({
351
- receivedImageBuffer: mockImageBuffer,
352
- snapshotIdentifier: mockSnapshotIdentifier,
353
- snapshotsDir: mockSnapshotsDir,
354
- diffDir: mockDiffDir,
355
- updateSnapshot: false,
356
- customDiffConfig: {
357
- threshold: 0.1,
358
- foo: 'bar',
359
- },
360
- failureThreshold: 0,
361
- failureThresholdType: 'pixel',
362
- });
363
-
364
- // Check that pixelmatch was not called
365
- expect(mockPixelMatch).not.toHaveBeenCalled();
366
- });
367
-
368
- it('should create diff output directory if there is not one already and test is failing', () => {
369
- const diffImageToSnapshot = setupTest({
370
- snapshotExists: true,
371
- outputDirExists: false,
372
- pixelmatchResult: 100,
373
- });
374
- diffImageToSnapshot({
375
- receivedImageBuffer: mockFailImageBuffer,
376
- snapshotIdentifier: mockSnapshotIdentifier,
377
- snapshotsDir: mockSnapshotsDir,
378
- diffDir: mockDiffDir,
379
- failureThreshold: 0,
380
- failureThresholdType: 'pixel',
381
- });
382
-
383
- expect(mockMkdirpSync).toHaveBeenCalledWith(path.join(mockSnapshotsDir, '__diff_output__'));
384
- });
385
-
386
- it('should not create diff output directory if test passed', () => {
387
- const diffImageToSnapshot = setupTest({ snapshotExists: true, outputDirExists: false });
388
- diffImageToSnapshot({
389
- receivedImageBuffer: mockImageBuffer,
390
- snapshotIdentifier: mockSnapshotIdentifier,
391
- snapshotsDir: mockSnapshotsDir,
392
- diffDir: mockDiffDir,
393
- updateSnapshot: false,
394
- failureThreshold: 0,
395
- failureThresholdType: 'pixel',
396
- });
397
-
398
- expect(mockMkdirSync).not.toHaveBeenCalled();
399
- });
400
-
401
- it('should not create diff output directory if there is one there already', () => {
402
- const diffImageToSnapshot = setupTest({ snapshotExists: true, outputDirExists: true });
403
- diffImageToSnapshot({
404
- receivedImageBuffer: mockImageBuffer,
405
- snapshotIdentifier: mockSnapshotIdentifier,
406
- snapshotsDir: mockSnapshotsDir,
407
- diffDir: mockDiffDir,
408
- updateSnapshot: false,
409
- failureThreshold: 0,
410
- failureThresholdType: 'pixel',
411
- });
412
-
413
- expect(mockMkdirSync).not.toHaveBeenCalledWith(path.join(mockSnapshotsDir, '__diff_output__'));
414
- });
415
-
416
- it('should create snapshots directory if there is not one already', () => {
417
- const diffImageToSnapshot = setupTest({ snapshotExists: true, snapshotDirExists: false });
418
- diffImageToSnapshot({
419
- receivedImageBuffer: mockImageBuffer,
420
- snapshotIdentifier: mockSnapshotIdentifier,
421
- snapshotsDir: mockSnapshotsDir,
422
- diffDir: mockDiffDir,
423
- updateSnapshot: true,
424
- updatePassedSnapshot: true,
425
- failureThreshold: 0,
426
- failureThresholdType: 'pixel',
427
- });
428
-
429
- expect(mockMkdirpSync).toHaveBeenCalledWith(mockSnapshotsDir);
430
- });
431
-
432
- it('should not create snapshots directory if there already is one', () => {
433
- const diffImageToSnapshot = setupTest({ snapshotExists: true, snapshotDirExists: true });
434
- diffImageToSnapshot({
435
- receivedImageBuffer: mockImageBuffer,
436
- snapshotIdentifier: mockSnapshotIdentifier,
437
- snapshotsDir: mockSnapshotsDir,
438
- diffDir: mockDiffDir,
439
- updateSnapshot: true,
440
- failureThreshold: 0,
441
- failureThresholdType: 'pixel',
442
- });
443
-
444
- expect(mockMkdirSync).not.toHaveBeenCalledWith(mockSnapshotsDir);
445
- });
446
-
447
- it('should create snapshot in __image_snapshots__ directory if there is not a snapshot created yet', () => {
448
- const diffImageToSnapshot = setupTest({ snapshotExists: false, snapshotDirExists: false });
449
- diffImageToSnapshot({
450
- receivedImageBuffer: mockImageBuffer,
451
- snapshotIdentifier: mockSnapshotIdentifier,
452
- snapshotsDir: mockSnapshotsDir,
453
- diffDir: mockDiffDir,
454
- updateSnapshot: false,
455
- failureThreshold: 0,
456
- failureThresholdType: 'pixel',
457
- });
458
-
459
- expect(mockWriteFileSync).toHaveBeenCalledTimes(1);
460
- expect(mockWriteFileSync).toHaveBeenCalledWith(path.join(mockSnapshotsDir, `${mockSnapshotIdentifier}-snap.png`), mockImageBuffer);
461
- });
462
-
463
- it('should return updated flag if snapshot was updated', () => {
464
- const diffImageToSnapshot = setupTest({ snapshotExists: true });
465
- const diffResult = diffImageToSnapshot({
466
- receivedImageBuffer: mockImageBuffer,
467
- snapshotIdentifier: mockSnapshotIdentifier,
468
- snapshotsDir: mockSnapshotsDir,
469
- diffDir: mockDiffDir,
470
- updateSnapshot: true,
471
- updatePassedSnapshot: true,
472
- failureThreshold: 0,
473
- failureThresholdType: 'pixel',
474
- });
475
-
476
- expect(diffResult).toHaveProperty('updated', true);
477
- });
478
-
479
- it('should return added flag if snapshot was added', () => {
480
- const diffImageToSnapshot = setupTest({ snapshotExists: false });
481
- const diffResult = diffImageToSnapshot({
482
- receivedImageBuffer: mockImageBuffer,
483
- snapshotIdentifier: mockSnapshotIdentifier,
484
- snapshotsDir: mockSnapshotsDir,
485
- diffDir: mockDiffDir,
486
- diffDirection: 'vertical',
487
- updateSnapshot: false,
488
- failureThreshold: 0,
489
- failureThresholdType: 'pixel',
490
- });
491
-
492
- expect(diffResult).toHaveProperty('added', true);
493
- expect(mockWriteFileSync).toHaveBeenCalledWith(
494
- path.join(mockSnapshotsDir, 'id1-snap.png'),
495
- expect.any(Buffer)
496
- );
497
- });
498
-
499
- it('should return path to comparison output image if a comparison was performed', () => {
500
- const diffImageToSnapshot = setupTest({ snapshotExists: true });
501
- const diffResult = diffImageToSnapshot({
502
- receivedImageBuffer: mockImageBuffer,
503
- snapshotIdentifier: mockSnapshotIdentifier,
504
- snapshotsDir: mockSnapshotsDir,
505
- diffDir: mockDiffDir,
506
- updateSnapshot: false,
507
- failureThreshold: 0,
508
- failureThresholdType: 'pixel',
509
- });
510
-
511
- expect(diffResult).toHaveProperty('diffOutputPath', path.join(mockSnapshotsDir, '__diff_output__', `${mockSnapshotIdentifier}-diff.png`));
512
- });
513
-
514
- it('should throw an error if an unknown threshold type is supplied', () => {
515
- const diffImageToSnapshot = setupTest({ snapshotExists: true });
516
-
517
- expect(() => {
518
- diffImageToSnapshot({
519
- receivedImageBuffer: mockFailImageBuffer,
520
- snapshotIdentifier: mockSnapshotIdentifier,
521
- snapshotsDir: mockSnapshotsDir,
522
- diffDir: mockDiffDir,
523
- updateSnapshot: false,
524
- failureThreshold: 0,
525
- failureThresholdType: 'banana',
526
- });
527
- }).toThrowErrorMatchingSnapshot();
528
- });
529
-
530
- it('should not write a file if updatePassedSnapshot is false', () => {
531
- const diffImageToSnapshot = setupTest({ snapshotExists: true });
532
-
533
- const diffResult = diffImageToSnapshot({
534
- receivedImageBuffer: mockImageBuffer,
535
- snapshotIdentifier: mockSnapshotIdentifier,
536
- snapshotsDir: mockSnapshotsDir,
537
- diffDir: mockDiffDir,
538
- updateSnapshot: true,
539
- updatePassedSnapshot: false,
540
- failureThreshold: 0,
541
- failureThresholdType: 'pixel',
542
- });
543
-
544
- expect(mockWriteFileSync).not.toHaveBeenCalled();
545
- expect(diffResult).toHaveProperty('pass', true);
546
- });
547
-
548
- it('should write a file if updatePassedSnapshot is true on passing test', () => {
549
- const diffImageToSnapshot = setupTest({ snapshotExists: true });
550
-
551
- const diffResult = diffImageToSnapshot({
552
- receivedImageBuffer: mockImageBuffer,
553
- snapshotIdentifier: mockSnapshotIdentifier,
554
- snapshotsDir: mockSnapshotsDir,
555
- diffDir: mockDiffDir,
556
- updateSnapshot: true,
557
- updatePassedSnapshot: true,
558
- failureThreshold: 0,
559
- failureThresholdType: 'pixel',
560
- });
561
-
562
- expect(mockWriteFileSync).toHaveBeenCalledTimes(1);
563
- expect(diffResult).toHaveProperty('updated', true);
564
- });
565
-
566
- it('should update snapshot on failure if updatePassedSnapshot is false', () => {
567
- const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 500 });
568
-
569
- const diffResult = diffImageToSnapshot({
570
- receivedImageBuffer: mockFailImageBuffer,
571
- snapshotIdentifier: mockSnapshotIdentifier,
572
- snapshotsDir: mockSnapshotsDir,
573
- diffDir: mockDiffDir,
574
- updateSnapshot: true,
575
- updatePassedSnapshot: false,
576
- failureThreshold: 0,
577
- failureThresholdType: 'pixel',
578
- });
579
-
580
- expect(mockWriteFileSync).toHaveBeenCalledTimes(1);
581
- expect(diffResult).toHaveProperty('updated', true);
582
- });
583
- });
584
- });
@@ -1,81 +0,0 @@
1
- /*
2
- * Copyright (c) 2019 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 ImageComposer = require('../src/image-composer');
17
-
18
- describe('image-composer', () => {
19
- const imageOneRaw = {};
20
- const imageOneWidth = 100;
21
- const imageOneHeight = 100;
22
-
23
- const imageTwoRaw = {};
24
- const imageTwoWidth = 100;
25
- const imageTwoHeight = 100;
26
-
27
- it('it should use default direction horizontal', () => {
28
- const composer = new ImageComposer();
29
- const params = composer.getParams();
30
-
31
- expect(params).toHaveProperty('direction', 'horizontal');
32
- });
33
-
34
- it('it should change direction to vertical', () => {
35
- const composer = new ImageComposer({ direction: 'vertical' });
36
- const params = composer.getParams();
37
-
38
- expect(params).toHaveProperty('direction', 'vertical');
39
- });
40
-
41
- it('it should add one image', () => {
42
- const composer = new ImageComposer();
43
- composer.addImage(imageOneRaw, imageOneWidth, imageOneHeight);
44
- const params = composer.getParams();
45
-
46
- expect(params).toHaveProperty('imagesCount', 1);
47
- });
48
-
49
- it('it should add two image', () => {
50
- const composer = new ImageComposer();
51
- composer.addImage(imageOneRaw, imageOneWidth, imageOneHeight);
52
- composer.addImage(imageTwoRaw, imageTwoWidth, imageTwoHeight);
53
- const params = composer.getParams();
54
-
55
- expect(params).toHaveProperty('imagesCount', 2);
56
- });
57
-
58
- it('it should align images horizontally', () => {
59
- const composer = new ImageComposer();
60
- composer.addImage(imageOneRaw, imageOneWidth, imageOneHeight);
61
- composer.addImage(imageTwoRaw, imageTwoWidth, imageTwoHeight);
62
- const params = composer.getParams();
63
-
64
- expect(params).toHaveProperty('compositeWidth', 200);
65
- expect(params).toHaveProperty('compositeHeight', 100);
66
- expect(params).toHaveProperty('offsetX', 100);
67
- expect(params).toHaveProperty('offsetY', 0);
68
- });
69
-
70
- it('it should align images vertically', () => {
71
- const composer = new ImageComposer({ direction: 'vertical' });
72
- composer.addImage(imageOneRaw, imageOneWidth, imageOneHeight);
73
- composer.addImage(imageTwoRaw, imageTwoWidth, imageTwoHeight);
74
- const params = composer.getParams();
75
-
76
- expect(params).toHaveProperty('compositeWidth', 100);
77
- expect(params).toHaveProperty('compositeHeight', 200);
78
- expect(params).toHaveProperty('offsetX', 0);
79
- expect(params).toHaveProperty('offsetY', 100);
80
- });
81
- });