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,310 +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
- const fs = require('fs');
16
- const path = require('path');
17
- const rimraf = require('rimraf');
18
- const uniqueId = require('lodash/uniqueId');
19
- const sizeOf = require('image-size');
20
- const { SnapshotState } = require('jest-snapshot');
21
- const { toMatchImageSnapshot } = require('../src');
22
-
23
- describe('toMatchImageSnapshot', () => {
24
- const fromStubs = file => path.resolve(__dirname, './stubs', file);
25
- const imageData = fs.readFileSync(fromStubs('TestImage.png'));
26
- const diffOutputDir = (snapshotsDir = '__image_snapshots__') => path.join(snapshotsDir, '/__diff_output__/');
27
- const customSnapshotsDir = path.resolve(__dirname, '__custom_snapshots_dir__');
28
- const cleanupRequiredIndicator = 'cleanup-required-';
29
- const getIdentifierIndicatingCleanupIsRequired = () => uniqueId(cleanupRequiredIndicator);
30
- const getSnapshotFilename = identifier => `${identifier}-snap.png`;
31
- const diffExists = identifier => fs.existsSync(path.join(__dirname, diffOutputDir(), `${identifier}-diff.png`));
32
-
33
- beforeAll(() => {
34
- // In tests, skip reporting (skip snapshotState update to not mess with our test report)
35
- global.UNSTABLE_SKIP_REPORTING = true;
36
- expect.extend({ toMatchImageSnapshot });
37
- });
38
-
39
- beforeEach(() => {
40
- rimraf.sync(`**/${cleanupRequiredIndicator}*`);
41
- });
42
-
43
- afterAll(() => {
44
- rimraf.sync(`**/${cleanupRequiredIndicator}*`);
45
- });
46
-
47
- describe('happy path', () => {
48
- it('writes snapshot with no error if there is not one stored already', () => {
49
- const snapshotsDir = path.resolve(__dirname, '__image_snapshots__');
50
- const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
51
-
52
- expect(
53
- () => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
54
- ).not.toThrowError();
55
- expect(
56
- fs.existsSync(path.join(snapshotsDir, getSnapshotFilename(customSnapshotIdentifier)))
57
- ).toBe(true);
58
- });
59
-
60
- it('matches an identical snapshot', () => {
61
- expect(() => expect(imageData).toMatchImageSnapshot()).not.toThrowError();
62
- });
63
-
64
- it('creates a snapshot in a custom directory if such is specified', () => {
65
- const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
66
-
67
- // First we need to write a new snapshot image
68
- expect(
69
- () => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier, customSnapshotsDir }) // eslint-disable-line max-len
70
- ).not.toThrowError();
71
-
72
- expect(
73
- fs.existsSync(path.join(customSnapshotsDir, getSnapshotFilename(customSnapshotIdentifier)))
74
- ).toBe(true);
75
- });
76
-
77
- it('does not write a result image for passing tests', () => {
78
- const customSnapshotIdentifier = 'integration-6';
79
-
80
- // First we need to write a new snapshot image
81
- expect(
82
- () => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
83
- ).not.toThrowError();
84
-
85
- expect(diffExists(customSnapshotIdentifier)).toBe(false);
86
- });
87
- });
88
-
89
- describe('updates', () => {
90
- const customSnapshotIdentifier = 'integration-update';
91
- const updateImageData = fs.readFileSync(fromStubs('TestImageUpdate1pxOff.png'));
92
- const updateImageSnapshotPath = path.join(__dirname, '__image_snapshots__', `${customSnapshotIdentifier}-snap.png`);
93
-
94
- beforeEach(() => {
95
- fs.writeFileSync(updateImageSnapshotPath, imageData);
96
- });
97
-
98
- afterAll(() => {
99
- fs.writeFileSync(updateImageSnapshotPath, imageData);
100
- });
101
-
102
- it('does not write a result image for passing tests in update mode by default', () => {
103
- const updateModeMatcher = toMatchImageSnapshot.bind({
104
- snapshotState: new SnapshotState(__filename, {
105
- updateSnapshot: 'all',
106
- }),
107
- testPath: __filename,
108
- });
109
- updateModeMatcher(updateImageData, {
110
- customSnapshotIdentifier,
111
- failureThreshold: 2,
112
- failureThresholdType: 'pixel',
113
- });
114
- expect(fs.readFileSync(updateImageSnapshotPath)).toEqual(imageData);
115
- });
116
-
117
- it('writes a result image for passing test in update mode with updatePassedSnapshots: true', () => {
118
- const updateModeMatcher = toMatchImageSnapshot.bind({
119
- snapshotState: new SnapshotState(__filename, {
120
- updateSnapshot: 'all',
121
- }),
122
- testPath: __filename,
123
- });
124
- updateModeMatcher(updateImageData, {
125
- customSnapshotIdentifier,
126
- updatePassedSnapshots: true,
127
- failureThreshold: 2,
128
- failureThresholdType: 'pixel',
129
- });
130
- expect(fs.readFileSync(updateImageSnapshotPath)).not.toEqual(updateImageData);
131
- });
132
-
133
- it('writes a result image for failing test in update mode by default', () => {
134
- const updateModeMatcher = toMatchImageSnapshot.bind({
135
- snapshotState: new SnapshotState(__filename, {
136
- updateSnapshot: 'all',
137
- }),
138
- testPath: __filename,
139
- });
140
- updateModeMatcher(updateImageData, {
141
- customSnapshotIdentifier,
142
- failureThreshold: 0,
143
- failureThresholdType: 'pixel',
144
- });
145
- expect(fs.readFileSync(updateImageSnapshotPath)).toEqual(updateImageData);
146
- });
147
-
148
- it('writes a result image for failing test in update mode with updatePassedSnapshots: false', () => {
149
- const updateModeMatcher = toMatchImageSnapshot.bind({
150
- snapshotState: new SnapshotState(__filename, {
151
- updateSnapshot: 'all',
152
- }),
153
- testPath: __filename,
154
- });
155
- updateModeMatcher(updateImageData, {
156
- customSnapshotIdentifier,
157
- updatePassedSnapshots: true,
158
- failureThreshold: 0,
159
- failureThresholdType: 'pixel',
160
- });
161
- expect(fs.readFileSync(updateImageSnapshotPath)).toEqual(updateImageData);
162
- });
163
- });
164
-
165
- describe('failures', () => {
166
- const failImageData = fs.readFileSync(fromStubs('TestImageFailure.png'));
167
- const oversizeImageData = fs.readFileSync(fromStubs('TestImageFailureOversize.png'));
168
- const biggerImageData = fs.readFileSync(fromStubs('TestImage150x150.png'));
169
-
170
- it('fails for a different snapshot', () => {
171
- const expectedError = /^Expected image to match or be a close match to snapshot but was 86\.55000000000001% different from snapshot \(8655 differing pixels\)\./;
172
- const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
173
-
174
- // Write a new snapshot image
175
- expect(
176
- () => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
177
- ).not.toThrowError();
178
-
179
- // Test against a different image
180
- expect(
181
- () => expect(failImageData).toMatchImageSnapshot({ customSnapshotIdentifier })
182
- ).toThrowError(expectedError);
183
- });
184
-
185
- it('fails with differently sized images and outputs diff', () => {
186
- const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
187
-
188
- // First we need to write a new snapshot image
189
- expect(
190
- () => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
191
- ).not.toThrowError();
192
-
193
- // Test against an image much larger than the snapshot.
194
- expect(
195
- () => expect(oversizeImageData).toMatchImageSnapshot({ customSnapshotIdentifier })
196
- ).toThrowError(/Expected image to be the same size as the snapshot \(100x100\), but was different \(153x145\)/);
197
-
198
- expect(diffExists(customSnapshotIdentifier)).toBe(true);
199
- });
200
-
201
- it('fails with images without diff pixels after being resized', () => {
202
- const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
203
-
204
- expect(
205
- () => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
206
- ).not.toThrowError();
207
-
208
- expect(
209
- () => expect(biggerImageData).toMatchImageSnapshot({ customSnapshotIdentifier })
210
- ).toThrowError(/Expected image to be the same size as the snapshot \(100x100\), but was different \(150x150\)/);
211
-
212
- expect(diffExists(customSnapshotIdentifier)).toBe(true);
213
- });
214
-
215
- it('writes a result image for failing tests', () => {
216
- const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
217
- const pathToResultImage = path.join(__dirname, diffOutputDir(), `${customSnapshotIdentifier}-diff.png`);
218
- // First we need to write a new snapshot image
219
- expect(
220
- () => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
221
- ).not.toThrowError();
222
-
223
- // then test against a different image
224
- expect(
225
- () => expect(failImageData).toMatchImageSnapshot({ customSnapshotIdentifier })
226
- ).toThrow();
227
-
228
- expect(fs.existsSync(pathToResultImage)).toBe(true);
229
-
230
- // just because file was written does not mean it is a png image
231
- expect(sizeOf(pathToResultImage)).toHaveProperty('type', 'png');
232
- });
233
-
234
- it('writes a result image for failing tests with horizontal layout', () => {
235
- const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
236
- const pathToResultImage = path.join(__dirname, diffOutputDir(), `${customSnapshotIdentifier}-diff.png`);
237
- // First we need to write a new snapshot image
238
- expect(
239
- () => expect(imageData).toMatchImageSnapshot({
240
- customSnapshotIdentifier,
241
- diffDirection: 'horizontal',
242
- })
243
- ).not.toThrowError();
244
-
245
- // then test against a different image
246
- expect(
247
- () => expect(failImageData).toMatchImageSnapshot({
248
- customSnapshotIdentifier,
249
- diffDirection: 'horizontal',
250
- })
251
- ).toThrow();
252
-
253
- expect(fs.existsSync(pathToResultImage)).toBe(true);
254
-
255
- expect(sizeOf(pathToResultImage)).toMatchObject({
256
- width: 300,
257
- height: 100,
258
- type: 'png',
259
- });
260
- });
261
-
262
- it('writes a result image for failing tests with vertical layout', () => {
263
- const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
264
- const pathToResultImage = path.join(__dirname, diffOutputDir(), `${customSnapshotIdentifier}-diff.png`);
265
- // First we need to write a new snapshot image
266
- expect(
267
- () => expect(imageData).toMatchImageSnapshot({
268
- customSnapshotIdentifier,
269
- diffDirection: 'vertical',
270
- })
271
- ).not.toThrowError();
272
-
273
- // then test against a different image
274
- expect(
275
- () => expect(failImageData).toMatchImageSnapshot({
276
- customSnapshotIdentifier,
277
- diffDirection: 'vertical',
278
- })
279
- ).toThrow();
280
-
281
- expect(fs.existsSync(pathToResultImage)).toBe(true);
282
-
283
- expect(sizeOf(pathToResultImage)).toMatchObject({
284
- width: 100,
285
- height: 300,
286
- type: 'png',
287
- });
288
- });
289
-
290
- it('removes result image from previous test runs for the same snapshot', () => {
291
- const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
292
- // First we need to write a new snapshot image
293
- expect(
294
- () => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
295
- ).not.toThrowError();
296
-
297
- // then test against a different image (to generate a results image)
298
- expect(
299
- () => expect(failImageData).toMatchImageSnapshot({ customSnapshotIdentifier })
300
- ).toThrow();
301
-
302
- // then test against image that should not generate results image (as it is passing test)
303
- expect(
304
- () => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
305
- ).not.toThrowError();
306
-
307
- expect(diffExists(customSnapshotIdentifier)).toBe(false);
308
- });
309
- });
310
- });
Binary file
@@ -1,3 +0,0 @@
1
- {
2
- "extends": "amex/test"
3
- }
@@ -1,9 +0,0 @@
1
- # jest-image-snapshot example usage
2
-
3
- To run the examples:
4
- ```bash
5
- git clone https://github.com/americanexpress/jest-image-snapshot.git
6
- cd jest-image-snapshot/examples
7
- npm install
8
- npm test
9
- ```
@@ -1,24 +0,0 @@
1
- /*
2
- * Copyright (c) 2018 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
- const fs = require('fs');
16
- const path = require('path');
17
-
18
- it('works reading an image from the local file system', () => {
19
- const imageAtTestPath = path.resolve(__dirname, './stubs', 'image.png');
20
- // imageAtTest is a PNG encoded image buffer which is what `toMatchImageSnapshot() expects
21
- const imageAtTest = fs.readFileSync(imageAtTestPath);
22
-
23
- expect(imageAtTest).toMatchImageSnapshot();
24
- });
@@ -1,37 +0,0 @@
1
- /*
2
- * Copyright (c) 2018 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 is looking for `puppeteer` at root level package.json
16
- // eslint-disable-next-line import/no-unresolved
17
- const puppeteer = require('puppeteer');
18
-
19
- describe('jest-image-snapshot usage with an image received from puppeteer', () => {
20
- let browser;
21
-
22
- beforeAll(async () => {
23
- browser = await puppeteer.launch();
24
- });
25
-
26
- it('works', async () => {
27
- const page = await browser.newPage();
28
- await page.goto('https://example.com');
29
- const image = await page.screenshot();
30
-
31
- expect(image).toMatchImageSnapshot();
32
- });
33
-
34
- afterAll(async () => {
35
- await browser.close();
36
- });
37
- });
Binary file
@@ -1,46 +0,0 @@
1
- /* eslint-disable */
2
-
3
- /*
4
- * To enable this image reporter, add it to your `jest.config.js` "reporters" definition:
5
- "reporters": [ "default", "<rootDir>/image-reporter.js" ]
6
- */
7
-
8
- const chalk = require('chalk');
9
- const fs = require('fs');
10
- const AWS = require('aws-sdk/global');
11
- const S3 = require('aws-sdk/clients/s3');
12
-
13
- const UPLOAD_BUCKET = 'YOUR_S3_BUCKET_NAME';
14
-
15
- const s3 = new AWS.S3({ apiVersion: '2006-03-01' });
16
-
17
- class ImageReporter {
18
- constructor(globalConfig, options) {
19
- this._globalConfig = globalConfig;
20
- this._options = options;
21
- }
22
-
23
- onTestResult(test, testResult, aggregateResults) {
24
- if (testResult.numFailingTests && testResult.failureMessage.match(/different from snapshot/)) {
25
- const files = fs.readdirSync('./__tests__/__image_snapshots__/__diff_output__/');
26
- files.forEach((value) => {
27
- const path = `diff_output/${value}`;
28
- const params = {
29
- Body: fs.readFileSync(`./__tests__/__image_snapshots__/__diff_output__/${value}`),
30
- Bucket: UPLOAD_BUCKET,
31
- Key: path,
32
- ContentType: 'image/png',
33
- };
34
- s3.putObject(params, (err) => {
35
- if (err) {
36
- console.log(err, err.stack);
37
- } else {
38
- console.log(chalk.red.bold(`Uploaded image diff file to https://${UPLOAD_BUCKET}.s3.amazonaws.com/${path}`));
39
- }
40
- });
41
- });
42
- }
43
- }
44
- }
45
-
46
- module.exports = ImageReporter;
@@ -1,19 +0,0 @@
1
- /*
2
- * Copyright (c) 2018 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 runs from root and only looks at root package.json
16
- // eslint-disable-next-line import/no-unresolved
17
- const { toMatchImageSnapshot } = require('jest-image-snapshot');
18
-
19
- expect.extend({ toMatchImageSnapshot });
@@ -1,26 +0,0 @@
1
- {
2
- "name": "jest-image-snapshot-examples",
3
- "version": "0.0.0",
4
- "description": "Example jest-image-snapshot usage",
5
- "scripts": {
6
- "test": "jest"
7
- },
8
- "keywords": [
9
- "jest-image-snapshot",
10
- "examples"
11
- ],
12
- "repository": {
13
- "type": "git",
14
- "url": "https://github.com/americanexpress/jest-image-snapshot.git"
15
- },
16
- "author": "Andres Escobar <andres.escobar@aexp.com> (https://github.com/anescobar1991)",
17
- "license": "Apache-2.0",
18
- "dependencies": {
19
- "jest": "*",
20
- "jest-image-snapshot": "*",
21
- "puppeteer": "*"
22
- },
23
- "jest": {
24
- "setupFilesAfterEnv": ["<rootDir>/jest-setup.js"]
25
- }
26
- }