@jsenv/snapshot 1.2.6 → 1.2.8

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/snapshot",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -38,7 +38,7 @@
38
38
  "@jsenv/filesystem": "4.6.5",
39
39
  "@jsenv/urls": "2.2.3",
40
40
  "@jsenv/utils": "2.1.1",
41
- "@jsenv/assert": "3.0.2",
41
+ "@jsenv/assert": "3.0.3",
42
42
  "prettier": "3.2.4"
43
43
  },
44
44
  "devDependencies": {
package/src/cli.mjs CHANGED
@@ -5,14 +5,14 @@ import { pathToFileURL } from "node:url";
5
5
  import { clearDirectorySync } from "@jsenv/filesystem";
6
6
 
7
7
  const options = {
8
- "help": {
9
- type: "boolean",
10
- },
11
- "include-dev": {
8
+ help: {
12
9
  type: "boolean",
13
10
  },
14
11
  };
15
- const { values, positionals } = parseArgs({ options, allowPositionals: true });
12
+ const { values, positionals } = parseArgs({
13
+ options,
14
+ allowPositionals: true,
15
+ });
16
16
 
17
17
  if (values.help || positionals.length === 0) {
18
18
  console.log(`snapshot: Manage snapshot files generated during tests.
@@ -27,9 +27,10 @@ pattern: files matching this pattern will be removed; can use "*" and "**"
27
27
  }
28
28
 
29
29
  const commandHandlers = {
30
- clear: async (pattern) => {
30
+ clear: (pattern) => {
31
31
  const currentDirectoryPath = process.cwd();
32
32
  const currentDirectoryUrl = pathToFileURL(`${currentDirectoryPath}/`);
33
+ console.log(`clear files matching ${pattern} in ${currentDirectoryPath}`);
33
34
  clearDirectorySync(currentDirectoryUrl, pattern);
34
35
  },
35
36
  };
@@ -42,4 +43,12 @@ if (!commandHandler) {
42
43
  process.exit(1);
43
44
  }
44
45
 
46
+ if (commandHandler.length) {
47
+ const args = positionals.slice(1);
48
+ if (args.length === 0) {
49
+ console.error(`Error: "${command}" command expect arguments.`);
50
+ process.exit(1);
51
+ }
52
+ }
53
+
45
54
  await commandHandler(...positionals.slice(1));
@@ -13,7 +13,7 @@ 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";
16
+ import { getStringAssertionErrorInfo } from "@jsenv/assert/src/error_info/strings.js";
17
17
 
18
18
  export const takeFileSnapshot = (fileUrl) => {
19
19
  fileUrl = assertAndNormalizeFileUrl(fileUrl);
@@ -83,11 +83,14 @@ const compareFileSnapshots = (actualFileSnapshot, expectedFileSnapshot) => {
83
83
  const failureMessage = `snapshot comparison failed for "${filename}"`;
84
84
 
85
85
  if (!actualFileSnapshot.stat) {
86
- throw assert.createAssertionError(`${failureMessage}
86
+ const fileNotFoundAssertionError =
87
+ assert.createAssertionError(`${failureMessage}
87
88
  --- reason ---
88
89
  file not found
89
90
  --- file ---
90
91
  ${fileUrl}`);
92
+ fileNotFoundAssertionError.name = "FileNotFoundAssertionError";
93
+ throw fileNotFoundAssertionError;
91
94
  }
92
95
  if (!expectedFileSnapshot.stat) {
93
96
  return;
@@ -98,26 +101,32 @@ ${fileUrl}`);
98
101
  if (actualFileContent.equals(expectedFileContent)) {
99
102
  return;
100
103
  }
101
- throw assert.createAssertionError(`${failureMessage}
104
+ const fileContentAssertionError =
105
+ assert.createAssertionError(`${failureMessage}
102
106
  --- reason ---
103
107
  content has changed
104
108
  --- file ---
105
109
  ${fileUrl}`);
110
+ fileContentAssertionError.name = "FileContentAssertionError";
111
+ throw fileContentAssertionError;
106
112
  }
107
113
  if (actualFileContent === expectedFileContent) {
108
114
  return;
109
115
  }
110
- const message = formatStringAssertionErrorMessage({
116
+ const errorInfo = getStringAssertionErrorInfo({
111
117
  actual: actualFileContent,
112
118
  expected: expectedFileContent,
113
119
  name: `file content`,
114
120
  format: assert.format,
115
121
  });
116
- throw assert.createAssertionError(`${failureMessage}
122
+ const fileContentStringAssertionError =
123
+ assert.createAssertionError(`${failureMessage}
117
124
  --- reason ---
118
- ${message}
125
+ ${errorInfo.message}
119
126
  --- file ---
120
127
  ${fileUrl}`);
128
+ fileContentStringAssertionError.name = errorInfo.type;
129
+ throw fileContentStringAssertionError;
121
130
  };
122
131
 
123
132
  export const takeDirectorySnapshot = (directoryUrl) => {
@@ -159,17 +168,23 @@ export const takeDirectorySnapshot = (directoryUrl) => {
159
168
  (relativeUrl) => new URL(relativeUrl, directoryUrl).href,
160
169
  );
161
170
  if (missingFileCount === 1) {
162
- throw assert.createAssertionError(`${failureMessage}
163
- --- reason ---
164
- "${missingRelativeUrls[0]}" is missing
165
- --- file missing ---
166
- ${missingUrls[0]}`);
171
+ const fileMissingAssertionError =
172
+ assert.createAssertionError(`${failureMessage}
173
+ --- reason ---
174
+ "${missingRelativeUrls[0]}" is missing
175
+ --- file missing ---
176
+ ${missingUrls[0]}`);
177
+ fileMissingAssertionError.name = "FileMissingAssertionError";
178
+ throw fileMissingAssertionError;
167
179
  }
168
- throw assert.createAssertionError(`${failureMessage}
180
+ const fileMissingAssertionError =
181
+ assert.createAssertionError(`${failureMessage}
169
182
  --- reason ---
170
183
  ${missingFileCount} files are missing
171
184
  --- files missing ---
172
185
  ${missingUrls.join("\n")}`);
186
+ fileMissingAssertionError.name = "FileMissingAssertionError";
187
+ throw fileMissingAssertionError;
173
188
  }
174
189
  }
175
190
 
@@ -185,17 +200,23 @@ ${missingUrls.join("\n")}`);
185
200
  (relativeUrl) => new URL(relativeUrl, directoryUrl).href,
186
201
  );
187
202
  if (extraFileCount === 1) {
188
- throw assert.createAssertionError(`${failureMessage}
203
+ const extraFileAssertionError =
204
+ assert.createAssertionError(`${failureMessage}
189
205
  --- reason ---
190
206
  "${extraRelativeUrls[0]}" is unexpected
191
207
  --- file unexpected ---
192
208
  ${extraUrls[0]}`);
209
+ extraFileAssertionError.name = "ExtraFileAssertionError";
210
+ throw extraFileAssertionError;
193
211
  }
194
- throw assert.createAssertionError(`${failureMessage}
212
+ const extraFileAssertionError =
213
+ assert.createAssertionError(`${failureMessage}
195
214
  --- reason ---
196
215
  ${extraFileCount} files are unexpected
197
216
  --- files unexpected ---
198
217
  ${extraUrls.join("\n")}`);
218
+ extraFileAssertionError.name = "ExtraFileAssertionError";
219
+ throw extraFileAssertionError;
199
220
  }
200
221
  }
201
222