@jsenv/snapshot 1.4.3 → 1.5.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/package.json +4 -4
- package/src/compare_png_files.js +1 -1
- package/src/file_snapshots.js +29 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/snapshot",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "Snapshot testing",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
"test": "node --conditions=development ./scripts/test.mjs"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@jsenv/assert": "4.0.
|
|
39
|
-
"@jsenv/filesystem": "4.7.
|
|
40
|
-
"@jsenv/urls": "2.2.
|
|
38
|
+
"@jsenv/assert": "4.0.14",
|
|
39
|
+
"@jsenv/filesystem": "4.7.3",
|
|
40
|
+
"@jsenv/urls": "2.2.8",
|
|
41
41
|
"@jsenv/utils": "2.1.1",
|
|
42
42
|
"pixelmatch": "6.0.0",
|
|
43
43
|
"prettier": "3.3.2"
|
package/src/compare_png_files.js
CHANGED
|
@@ -20,7 +20,7 @@ export const comparePngFiles = (actualData, expectData) => {
|
|
|
20
20
|
);
|
|
21
21
|
const diffRatio = numberOfPixelsConsideredAsDiff / numberOfPixels;
|
|
22
22
|
const diffPercentage = diffRatio * 100;
|
|
23
|
-
return diffPercentage <=
|
|
23
|
+
return diffPercentage <= 0.5;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
const getPngDimensions = (buffer) => {
|
package/src/file_snapshots.js
CHANGED
|
@@ -26,11 +26,8 @@ export const takeFileSnapshot = (fileUrl) => {
|
|
|
26
26
|
const fileSnapshot = createFileSnapshot(fileUrl);
|
|
27
27
|
removeFileSync(fileUrl, { allowUseless: true });
|
|
28
28
|
return {
|
|
29
|
-
compare: (
|
|
30
|
-
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
fileSnapshot.compare(createFileSnapshot(fileUrl));
|
|
29
|
+
compare: (throwWhenDiff = process.env.CI) => {
|
|
30
|
+
fileSnapshot.compare(createFileSnapshot(fileUrl), { throwWhenDiff });
|
|
34
31
|
},
|
|
35
32
|
writeContent: (content) => {
|
|
36
33
|
writeFileSync(fileUrl, content);
|
|
@@ -51,11 +48,14 @@ const createFileSnapshot = (fileUrl) => {
|
|
|
51
48
|
stat: null,
|
|
52
49
|
contentType: CONTENT_TYPE.fromUrlExtension(fileUrl),
|
|
53
50
|
content: "",
|
|
54
|
-
compare: (nextFileSnapshot) => {
|
|
51
|
+
compare: (nextFileSnapshot, { throwWhenDiff }) => {
|
|
55
52
|
const filename = urlToFilename(fileUrl);
|
|
56
53
|
const failureMessage = `snapshot comparison failed for "${filename}"`;
|
|
57
54
|
|
|
58
55
|
if (!nextFileSnapshot.stat) {
|
|
56
|
+
if (!throwWhenDiff) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
59
|
const fileNotFoundAssertionError =
|
|
60
60
|
new FileContentNotFoundAssertionError(`${failureMessage}
|
|
61
61
|
--- reason ---
|
|
@@ -75,9 +75,14 @@ ${fileUrl}`);
|
|
|
75
75
|
}
|
|
76
76
|
if (fileSnapshot.contentType === "image/png") {
|
|
77
77
|
if (comparePngFiles(fileContent, nextFileContent)) {
|
|
78
|
+
// restore old version to prevent git diff
|
|
79
|
+
writeFileSync(fileUrl, fileContent);
|
|
78
80
|
return;
|
|
79
81
|
}
|
|
80
82
|
}
|
|
83
|
+
if (!throwWhenDiff) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
81
86
|
const fileContentAssertionError =
|
|
82
87
|
new FileContentAssertionError(`${failureMessage}
|
|
83
88
|
--- reason ---
|
|
@@ -89,6 +94,9 @@ ${fileUrl}`);
|
|
|
89
94
|
if (nextFileContent === fileContent) {
|
|
90
95
|
return;
|
|
91
96
|
}
|
|
97
|
+
if (!throwWhenDiff) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
92
100
|
assert({
|
|
93
101
|
message: failureMessage,
|
|
94
102
|
actual: nextFileContent,
|
|
@@ -133,7 +141,10 @@ ${fileUrl}`);
|
|
|
133
141
|
|
|
134
142
|
export const takeDirectorySnapshot = (
|
|
135
143
|
directoryUrl,
|
|
136
|
-
pattern = {
|
|
144
|
+
pattern = {
|
|
145
|
+
"**/*": true,
|
|
146
|
+
"**/.*/": false,
|
|
147
|
+
},
|
|
137
148
|
) => {
|
|
138
149
|
directoryUrl = assertAndNormalizeDirectoryUrl(directoryUrl);
|
|
139
150
|
directoryUrl = new URL(directoryUrl);
|
|
@@ -164,15 +175,12 @@ export const takeDirectorySnapshot = (
|
|
|
164
175
|
});
|
|
165
176
|
return {
|
|
166
177
|
__snapshot: directorySnapshot,
|
|
167
|
-
compare: (
|
|
168
|
-
if (!doIt) {
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
178
|
+
compare: (throwWhenDiff = process.env.CI) => {
|
|
171
179
|
const nextDirectorySnapshot = createDirectorySnapshot(directoryUrl, {
|
|
172
180
|
shouldVisitDirectory,
|
|
173
181
|
shouldIncludeFile,
|
|
174
182
|
});
|
|
175
|
-
directorySnapshot.compare(nextDirectorySnapshot);
|
|
183
|
+
directorySnapshot.compare(nextDirectorySnapshot, { throwWhenDiff });
|
|
176
184
|
},
|
|
177
185
|
addFile: (relativeUrl, content) => {
|
|
178
186
|
writeFileSync(new URL(relativeUrl, directoryUrl), content);
|
|
@@ -206,7 +214,7 @@ const createDirectorySnapshot = (
|
|
|
206
214
|
stat: null,
|
|
207
215
|
empty: false,
|
|
208
216
|
contentSnapshot: {},
|
|
209
|
-
compare: (nextDirectorySnapshot) => {
|
|
217
|
+
compare: (nextDirectorySnapshot, { throwWhenDiff }) => {
|
|
210
218
|
const dirname = `${urlToFilename(directoryUrl)}/`;
|
|
211
219
|
const failureMessage = `snapshot comparison failed for "${dirname}"`;
|
|
212
220
|
if (!directorySnapshot.stat || directorySnapshot.empty) {
|
|
@@ -225,7 +233,7 @@ const createDirectorySnapshot = (
|
|
|
225
233
|
nextDirectorySnapshot.contentSnapshot;
|
|
226
234
|
const nextRelativeUrls = Object.keys(nextDirectoryContentSnapshot);
|
|
227
235
|
// missing content
|
|
228
|
-
{
|
|
236
|
+
if (throwWhenDiff) {
|
|
229
237
|
const missingRelativeUrls = relativeUrls.filter(
|
|
230
238
|
(relativeUrl) => !nextRelativeUrls.includes(relativeUrl),
|
|
231
239
|
);
|
|
@@ -253,7 +261,7 @@ ${missingUrls.join("\n")}`);
|
|
|
253
261
|
}
|
|
254
262
|
}
|
|
255
263
|
// unexpected content
|
|
256
|
-
{
|
|
264
|
+
if (throwWhenDiff) {
|
|
257
265
|
const extraRelativeUrls = nextRelativeUrls.filter(
|
|
258
266
|
(nextRelativeUrl) => !relativeUrls.includes(nextRelativeUrl),
|
|
259
267
|
);
|
|
@@ -285,7 +293,9 @@ ${extraUrls.join("\n")}`);
|
|
|
285
293
|
for (const relativeUrl of nextRelativeUrls) {
|
|
286
294
|
const snapshot = directoryContentSnapshot[relativeUrl];
|
|
287
295
|
const nextSnapshot = nextDirectoryContentSnapshot[relativeUrl];
|
|
288
|
-
snapshot.compare(nextSnapshot
|
|
296
|
+
snapshot.compare(nextSnapshot, {
|
|
297
|
+
throwWhenDiff: relativeUrl.endsWith(".gif") ? false : throwWhenDiff,
|
|
298
|
+
});
|
|
289
299
|
}
|
|
290
300
|
}
|
|
291
301
|
},
|
|
@@ -327,6 +337,9 @@ ${extraUrls.join("\n")}`);
|
|
|
327
337
|
}
|
|
328
338
|
const relativeUrl = urlToRelativeUrl(directoryItemUrl, directoryUrl);
|
|
329
339
|
if (directoryItemStat.isDirectory()) {
|
|
340
|
+
if (!shouldIncludeFile(directoryUrl)) {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
330
343
|
if (!shouldVisitDirectory(directoryUrl)) {
|
|
331
344
|
continue;
|
|
332
345
|
}
|