@jsenv/snapshot 1.2.7 → 1.3.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.
- package/package.json +5 -5
- package/src/file_snapshots.js +32 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/snapshot",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Snapshot testing",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"test": "node --conditions=development ./scripts/test.mjs"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@jsenv/filesystem": "4.6.
|
|
39
|
-
"@jsenv/urls": "2.2.
|
|
38
|
+
"@jsenv/filesystem": "4.6.6",
|
|
39
|
+
"@jsenv/urls": "2.2.4",
|
|
40
40
|
"@jsenv/utils": "2.1.1",
|
|
41
|
-
"@jsenv/assert": "
|
|
42
|
-
"prettier": "3.2
|
|
41
|
+
"@jsenv/assert": "4.0.0",
|
|
42
|
+
"prettier": "3.3.2"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"strip-ansi": "7.1.0"
|
package/src/file_snapshots.js
CHANGED
|
@@ -13,7 +13,6 @@ import { urlToFilename, urlToRelativeUrl } from "@jsenv/urls";
|
|
|
13
13
|
import { CONTENT_TYPE } from "@jsenv/utils/src/content_type/content_type.js";
|
|
14
14
|
|
|
15
15
|
import { assert } from "@jsenv/assert";
|
|
16
|
-
import { formatStringAssertionErrorMessage } from "@jsenv/assert/src/error_info/strings.js";
|
|
17
16
|
|
|
18
17
|
export const takeFileSnapshot = (fileUrl) => {
|
|
19
18
|
fileUrl = assertAndNormalizeFileUrl(fileUrl);
|
|
@@ -53,7 +52,7 @@ const createFileSnapshot = (fileUrl) => {
|
|
|
53
52
|
throw e;
|
|
54
53
|
}
|
|
55
54
|
if (!fileSnapshot.stat.isFile()) {
|
|
56
|
-
throw new Error(`file
|
|
55
|
+
throw new Error(`file expect at ${fileUrl}`);
|
|
57
56
|
}
|
|
58
57
|
|
|
59
58
|
const isTextual = CONTENT_TYPE.isTextual(
|
|
@@ -83,11 +82,14 @@ const compareFileSnapshots = (actualFileSnapshot, expectedFileSnapshot) => {
|
|
|
83
82
|
const failureMessage = `snapshot comparison failed for "${filename}"`;
|
|
84
83
|
|
|
85
84
|
if (!actualFileSnapshot.stat) {
|
|
86
|
-
|
|
85
|
+
const fileNotFoundAssertionError =
|
|
86
|
+
assert.createAssertionError(`${failureMessage}
|
|
87
87
|
--- reason ---
|
|
88
88
|
file not found
|
|
89
89
|
--- file ---
|
|
90
90
|
${fileUrl}`);
|
|
91
|
+
fileNotFoundAssertionError.name = "FileNotFoundAssertionError";
|
|
92
|
+
throw fileNotFoundAssertionError;
|
|
91
93
|
}
|
|
92
94
|
if (!expectedFileSnapshot.stat) {
|
|
93
95
|
return;
|
|
@@ -98,26 +100,25 @@ ${fileUrl}`);
|
|
|
98
100
|
if (actualFileContent.equals(expectedFileContent)) {
|
|
99
101
|
return;
|
|
100
102
|
}
|
|
101
|
-
|
|
103
|
+
const fileContentAssertionError =
|
|
104
|
+
assert.createAssertionError(`${failureMessage}
|
|
102
105
|
--- reason ---
|
|
103
106
|
content has changed
|
|
104
107
|
--- file ---
|
|
105
108
|
${fileUrl}`);
|
|
109
|
+
fileContentAssertionError.name = "FileContentAssertionError";
|
|
110
|
+
throw fileContentAssertionError;
|
|
106
111
|
}
|
|
107
112
|
if (actualFileContent === expectedFileContent) {
|
|
108
113
|
return;
|
|
109
114
|
}
|
|
110
|
-
|
|
115
|
+
assert({
|
|
116
|
+
message: failureMessage,
|
|
117
|
+
details: fileUrl,
|
|
111
118
|
actual: actualFileContent,
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
format: assert.format,
|
|
119
|
+
expect: expectedFileContent,
|
|
120
|
+
forceMultilineDiff: true,
|
|
115
121
|
});
|
|
116
|
-
throw assert.createAssertionError(`${failureMessage}
|
|
117
|
-
--- reason ---
|
|
118
|
-
${message}
|
|
119
|
-
--- file ---
|
|
120
|
-
${fileUrl}`);
|
|
121
122
|
};
|
|
122
123
|
|
|
123
124
|
export const takeDirectorySnapshot = (directoryUrl) => {
|
|
@@ -159,17 +160,23 @@ export const takeDirectorySnapshot = (directoryUrl) => {
|
|
|
159
160
|
(relativeUrl) => new URL(relativeUrl, directoryUrl).href,
|
|
160
161
|
);
|
|
161
162
|
if (missingFileCount === 1) {
|
|
162
|
-
|
|
163
|
+
const fileMissingAssertionError =
|
|
164
|
+
assert.createAssertionError(`${failureMessage}
|
|
163
165
|
--- reason ---
|
|
164
166
|
"${missingRelativeUrls[0]}" is missing
|
|
165
167
|
--- file missing ---
|
|
166
168
|
${missingUrls[0]}`);
|
|
169
|
+
fileMissingAssertionError.name = "FileMissingAssertionError";
|
|
170
|
+
throw fileMissingAssertionError;
|
|
167
171
|
}
|
|
168
|
-
|
|
172
|
+
const fileMissingAssertionError =
|
|
173
|
+
assert.createAssertionError(`${failureMessage}
|
|
169
174
|
--- reason ---
|
|
170
175
|
${missingFileCount} files are missing
|
|
171
176
|
--- files missing ---
|
|
172
177
|
${missingUrls.join("\n")}`);
|
|
178
|
+
fileMissingAssertionError.name = "FileMissingAssertionError";
|
|
179
|
+
throw fileMissingAssertionError;
|
|
173
180
|
}
|
|
174
181
|
}
|
|
175
182
|
|
|
@@ -185,17 +192,23 @@ ${missingUrls.join("\n")}`);
|
|
|
185
192
|
(relativeUrl) => new URL(relativeUrl, directoryUrl).href,
|
|
186
193
|
);
|
|
187
194
|
if (extraFileCount === 1) {
|
|
188
|
-
|
|
195
|
+
const extraFileAssertionError =
|
|
196
|
+
assert.createAssertionError(`${failureMessage}
|
|
189
197
|
--- reason ---
|
|
190
198
|
"${extraRelativeUrls[0]}" is unexpected
|
|
191
199
|
--- file unexpected ---
|
|
192
200
|
${extraUrls[0]}`);
|
|
201
|
+
extraFileAssertionError.name = "ExtraFileAssertionError";
|
|
202
|
+
throw extraFileAssertionError;
|
|
193
203
|
}
|
|
194
|
-
|
|
204
|
+
const extraFileAssertionError =
|
|
205
|
+
assert.createAssertionError(`${failureMessage}
|
|
195
206
|
--- reason ---
|
|
196
207
|
${extraFileCount} files are unexpected
|
|
197
208
|
--- files unexpected ---
|
|
198
209
|
${extraUrls.join("\n")}`);
|
|
210
|
+
extraFileAssertionError.name = "ExtraFileAssertionError";
|
|
211
|
+
throw extraFileAssertionError;
|
|
199
212
|
}
|
|
200
213
|
}
|
|
201
214
|
|
|
@@ -259,12 +272,12 @@ const createDirectorySnapshot = (directoryUrl) => {
|
|
|
259
272
|
if (e.code === "ENOTDIR") {
|
|
260
273
|
// trailing slash is forced on directoryUrl
|
|
261
274
|
// as a result Node.js throw ENOTDIR when doing "stat" operation
|
|
262
|
-
throw new Error(`directory
|
|
275
|
+
throw new Error(`directory expect at ${directoryUrl}`);
|
|
263
276
|
}
|
|
264
277
|
throw e;
|
|
265
278
|
}
|
|
266
279
|
if (!directorySnapshot.stat.isDirectory()) {
|
|
267
|
-
throw new Error(`directory
|
|
280
|
+
throw new Error(`directory expect at ${directoryUrl}`);
|
|
268
281
|
}
|
|
269
282
|
|
|
270
283
|
const entryNames = readdirSync(directoryUrl);
|