jest-image-snapshot 6.0.0 → 6.1.1
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.
- package/CHANGELOG.md +14 -0
- package/README.md +1 -0
- package/package.json +6 -2
- package/src/diff-snapshot.js +28 -14
- package/src/index.js +3 -0
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
## [6.1.1](https://github.com/americanexpress/jest-image-snapshot/compare/v6.1.0...v6.1.1) (2023-07-25)
|
2
|
+
|
3
|
+
|
4
|
+
### Bug Fixes
|
5
|
+
|
6
|
+
* only updatePassedSnapshot if updateSnapshot is also true ([#327](https://github.com/americanexpress/jest-image-snapshot/issues/327)) ([b9d9c3f](https://github.com/americanexpress/jest-image-snapshot/commit/b9d9c3f16ab0e10a3e1320d03efb52e81675d2aa)), closes [#320](https://github.com/americanexpress/jest-image-snapshot/issues/320) [#322](https://github.com/americanexpress/jest-image-snapshot/issues/322) [#324](https://github.com/americanexpress/jest-image-snapshot/issues/324)
|
7
|
+
|
8
|
+
# [6.1.0](https://github.com/americanexpress/jest-image-snapshot/compare/v6.0.0...v6.1.0) (2022-12-02)
|
9
|
+
|
10
|
+
|
11
|
+
### Features
|
12
|
+
|
13
|
+
* add onlyDiff in options ([#317](https://github.com/americanexpress/jest-image-snapshot/issues/317)) ([4bad752](https://github.com/americanexpress/jest-image-snapshot/commit/4bad752571bb567861ddfa2cc9073f33c4239352))
|
14
|
+
|
1
15
|
# [6.0.0](https://github.com/americanexpress/jest-image-snapshot/compare/v5.2.0...v6.0.0) (2022-11-03)
|
2
16
|
|
3
17
|
|
package/README.md
CHANGED
@@ -113,6 +113,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
|
|
113
113
|
* `customReceivedDir`: A custom absolute path of a directory to keep this received image in
|
114
114
|
* `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.
|
115
115
|
* `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`).
|
116
117
|
* `noColors`: Removes coloring from console output, useful if storing the results in a file
|
117
118
|
* `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.
|
118
119
|
* `failureThresholdType`: (default `pixel`) (options `percent` or `pixel`) Sets the type of threshold that would trigger a failure.
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "jest-image-snapshot",
|
3
|
-
"version": "6.
|
3
|
+
"version": "6.1.1",
|
4
4
|
"description": "Jest matcher for image comparisons. Most commonly used for visual regression testing.",
|
5
5
|
"main": "src/index.js",
|
6
6
|
"engines": {
|
@@ -64,7 +64,6 @@
|
|
64
64
|
"get-stdin": "^5.0.1",
|
65
65
|
"glur": "^1.1.2",
|
66
66
|
"lodash": "^4.17.4",
|
67
|
-
"mkdirp": "^0.5.1",
|
68
67
|
"pixelmatch": "^5.1.0",
|
69
68
|
"pngjs": "^3.4.0",
|
70
69
|
"rimraf": "^2.6.2",
|
@@ -73,6 +72,11 @@
|
|
73
72
|
"peerDependencies": {
|
74
73
|
"jest": ">=20 <=29"
|
75
74
|
},
|
75
|
+
"peerDependenciesMeta": {
|
76
|
+
"jest": {
|
77
|
+
"optional": true
|
78
|
+
}
|
79
|
+
},
|
76
80
|
"husky": {
|
77
81
|
"hooks": {
|
78
82
|
"pre-commit": "npm test",
|
package/src/diff-snapshot.js
CHANGED
@@ -15,7 +15,6 @@
|
|
15
15
|
const childProcess = require('child_process');
|
16
16
|
const fs = require('fs');
|
17
17
|
const path = require('path');
|
18
|
-
const mkdirp = require('mkdirp');
|
19
18
|
const pixelmatch = require('pixelmatch');
|
20
19
|
const ssim = require('ssim.js');
|
21
20
|
const { PNG } = require('pngjs');
|
@@ -148,9 +147,8 @@ const alignImagesToSameSize = (firstImage, secondImage) => {
|
|
148
147
|
|
149
148
|
const isFailure = ({ pass, updateSnapshot }) => !pass && !updateSnapshot;
|
150
149
|
|
151
|
-
const shouldUpdate = ({ pass, updateSnapshot, updatePassedSnapshot }) =>
|
152
|
-
(!pass
|
153
|
-
);
|
150
|
+
const shouldUpdate = ({ pass, updateSnapshot, updatePassedSnapshot }) =>
|
151
|
+
updateSnapshot && (!pass || (pass && updatePassedSnapshot));
|
154
152
|
|
155
153
|
const shouldFail = ({
|
156
154
|
totalPixels,
|
@@ -184,6 +182,24 @@ const shouldFail = ({
|
|
184
182
|
};
|
185
183
|
};
|
186
184
|
|
185
|
+
function composeDiff(options) {
|
186
|
+
const {
|
187
|
+
diffDirection, baselineImage, diffImage, receivedImage, imageWidth, imageHeight, onlyDiff,
|
188
|
+
} = options;
|
189
|
+
const composer = new ImageComposer({
|
190
|
+
direction: diffDirection,
|
191
|
+
});
|
192
|
+
|
193
|
+
if (onlyDiff) {
|
194
|
+
composer.addImage(diffImage, imageWidth, imageHeight);
|
195
|
+
} else {
|
196
|
+
composer.addImage(baselineImage, imageWidth, imageHeight);
|
197
|
+
composer.addImage(diffImage, imageWidth, imageHeight);
|
198
|
+
composer.addImage(receivedImage, imageWidth, imageHeight);
|
199
|
+
}
|
200
|
+
return composer;
|
201
|
+
}
|
202
|
+
|
187
203
|
function diffImageToSnapshot(options) {
|
188
204
|
const {
|
189
205
|
receivedImageBuffer,
|
@@ -193,6 +209,7 @@ function diffImageToSnapshot(options) {
|
|
193
209
|
receivedDir = path.join(options.snapshotsDir, '__received_output__'),
|
194
210
|
diffDir = path.join(options.snapshotsDir, '__diff_output__'),
|
195
211
|
diffDirection,
|
212
|
+
onlyDiff = false,
|
196
213
|
updateSnapshot = false,
|
197
214
|
updatePassedSnapshot = false,
|
198
215
|
customDiffConfig = {},
|
@@ -207,7 +224,7 @@ function diffImageToSnapshot(options) {
|
|
207
224
|
let result = {};
|
208
225
|
const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}.png`);
|
209
226
|
if (!fs.existsSync(baselineSnapshotPath)) {
|
210
|
-
|
227
|
+
fs.mkdirSync(path.dirname(baselineSnapshotPath), { recursive: true });
|
211
228
|
fs.writeFileSync(baselineSnapshotPath, receivedImageBuffer);
|
212
229
|
result = { added: true };
|
213
230
|
} else {
|
@@ -275,20 +292,16 @@ function diffImageToSnapshot(options) {
|
|
275
292
|
|
276
293
|
if (isFailure({ pass, updateSnapshot })) {
|
277
294
|
if (storeReceivedOnFailure) {
|
278
|
-
|
295
|
+
fs.mkdirSync(path.dirname(receivedSnapshotPath), { recursive: true });
|
279
296
|
fs.writeFileSync(receivedSnapshotPath, receivedImageBuffer);
|
280
297
|
result = { receivedSnapshotPath };
|
281
298
|
}
|
282
299
|
|
283
|
-
|
284
|
-
const composer =
|
285
|
-
|
300
|
+
fs.mkdirSync(path.dirname(diffOutputPath), { recursive: true });
|
301
|
+
const composer = composeDiff({
|
302
|
+
diffDirection, baselineImage, diffImage, receivedImage, imageWidth, imageHeight, onlyDiff,
|
286
303
|
});
|
287
304
|
|
288
|
-
composer.addImage(baselineImage, imageWidth, imageHeight);
|
289
|
-
composer.addImage(diffImage, imageWidth, imageHeight);
|
290
|
-
composer.addImage(receivedImage, imageWidth, imageHeight);
|
291
|
-
|
292
305
|
const composerParams = composer.getParams();
|
293
306
|
|
294
307
|
const compositeResultImage = new PNG({
|
@@ -319,7 +332,7 @@ function diffImageToSnapshot(options) {
|
|
319
332
|
imgSrcString: `data:image/png;base64,${pngBuffer.toString('base64')}`,
|
320
333
|
};
|
321
334
|
} else if (shouldUpdate({ pass, updateSnapshot, updatePassedSnapshot })) {
|
322
|
-
|
335
|
+
fs.mkdirSync(path.dirname(baselineSnapshotPath), { recursive: true });
|
323
336
|
fs.writeFileSync(baselineSnapshotPath, receivedImageBuffer);
|
324
337
|
result = { updated: true };
|
325
338
|
} else {
|
@@ -335,6 +348,7 @@ function diffImageToSnapshot(options) {
|
|
335
348
|
return result;
|
336
349
|
}
|
337
350
|
|
351
|
+
|
338
352
|
function runDiffImageToSnapshot(options) {
|
339
353
|
options.receivedImageBuffer = options.receivedImageBuffer.toString('base64');
|
340
354
|
|
package/src/index.js
CHANGED
@@ -138,6 +138,7 @@ function configureToMatchImageSnapshot({
|
|
138
138
|
storeReceivedOnFailure: commonStoreReceivedOnFailure = false,
|
139
139
|
customReceivedDir: commonCustomReceivedDir,
|
140
140
|
customDiffDir: commonCustomDiffDir,
|
141
|
+
onlyDiff: commonOnlyDiff = false,
|
141
142
|
diffDirection: commonDiffDirection = 'horizontal',
|
142
143
|
noColors: commonNoColors,
|
143
144
|
failureThreshold: commonFailureThreshold = 0,
|
@@ -156,6 +157,7 @@ function configureToMatchImageSnapshot({
|
|
156
157
|
storeReceivedOnFailure = commonStoreReceivedOnFailure,
|
157
158
|
customReceivedDir = commonCustomReceivedDir,
|
158
159
|
customDiffDir = commonCustomDiffDir,
|
160
|
+
onlyDiff = commonOnlyDiff,
|
159
161
|
diffDirection = commonDiffDirection,
|
160
162
|
customDiffConfig = {},
|
161
163
|
noColors = commonNoColors,
|
@@ -218,6 +220,7 @@ function configureToMatchImageSnapshot({
|
|
218
220
|
receivedDir,
|
219
221
|
diffDir,
|
220
222
|
diffDirection,
|
223
|
+
onlyDiff,
|
221
224
|
snapshotIdentifier,
|
222
225
|
updateSnapshot: snapshotState._updateSnapshot === 'all',
|
223
226
|
customDiffConfig: Object.assign({}, commonCustomDiffConfig, customDiffConfig),
|