@jsenv/snapshot 2.16.0 → 2.16.2

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": "2.16.0",
3
+ "version": "2.16.2",
4
4
  "description": "Snapshot testing",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,13 +26,13 @@
26
26
  ],
27
27
  "dependencies": {
28
28
  "@jsenv/assert": "4.5.5",
29
- "@jsenv/ast": "6.7.17",
29
+ "@jsenv/ast": "6.7.18",
30
30
  "@jsenv/exception": "1.2.1",
31
- "@jsenv/filesystem": "4.15.10",
32
- "@jsenv/humanize": "1.7.2",
33
- "@jsenv/terminal-recorder": "1.5.31",
31
+ "@jsenv/filesystem": "4.15.11",
32
+ "@jsenv/humanize": "1.7.3",
33
+ "@jsenv/terminal-recorder": "1.5.32",
34
34
  "@jsenv/url-meta": "8.5.7",
35
- "@jsenv/urls": "2.9.4",
35
+ "@jsenv/urls": "2.9.5",
36
36
  "@jsenv/utils": "2.3.1",
37
37
  "ansi-regex": "6.2.2",
38
38
  "pixelmatch": "7.1.0",
@@ -11,6 +11,7 @@ import {
11
11
  stringifyHtmlAst,
12
12
  visitHtmlNodes,
13
13
  } from "@jsenv/ast";
14
+ import { humanize } from "@jsenv/humanize";
14
15
  import { urlToExtension } from "@jsenv/urls";
15
16
  import { CONTENT_TYPE } from "@jsenv/utils/src/content_type/content_type.js";
16
17
  import stripAnsi from "strip-ansi";
@@ -109,7 +110,7 @@ export const replaceFluctuatingValues = (
109
110
  if (stringType === "json") {
110
111
  const jsValue = JSON.parse(value);
111
112
  const replaced = replaceInObject(jsValue, { replace: replaceThings });
112
- return JSON.stringify(replaced, null, " ");
113
+ return jsonStringifyWithUndefined(replaced);
113
114
  }
114
115
  if (stringType === "html" || stringType === "xml") {
115
116
  // do parse html
@@ -165,11 +166,19 @@ export const replaceFluctuatingValues = (
165
166
  return regexpSource;
166
167
  }
167
168
  const jsValueReplaced = replaceInObject(value, { replace: replaceThings });
168
- return JSON.stringify(jsValueReplaced, null, " ");
169
+ return jsonStringifyWithUndefined(jsValueReplaced);
169
170
  }
170
171
  return value;
171
172
  };
172
173
 
174
+ // Use humanize for better JavaScript value representation
175
+ const jsonStringifyWithUndefined = (obj) => {
176
+ return humanize(obj, {
177
+ quote: "auto", // Let humanize decide the best quotes
178
+ indentSize: 2,
179
+ });
180
+ };
181
+
173
182
  const replaceInObject = (object, { replace }) => {
174
183
  const deepCopy = (
175
184
  value,
@@ -50,6 +50,7 @@ export const renderSideEffects = (
50
50
  // and in that case we might want to move it to an other file
51
51
  dedicatedFile: { line: 50, length: 5000 },
52
52
  }),
53
+ sourceLocation = false,
53
54
  errorTransform,
54
55
  } = {},
55
56
  ) => {
@@ -97,6 +98,7 @@ export const renderSideEffects = (
97
98
  replace,
98
99
  errorTransform,
99
100
  lastSideEffectNumber,
101
+ sourceLocation,
100
102
  });
101
103
  }
102
104
  markdown += sideEffectMd;
@@ -163,6 +165,7 @@ const renderOneSideEffect = (
163
165
  replace,
164
166
  errorTransform,
165
167
  lastSideEffectNumber,
168
+ sourceLocation,
166
169
  },
167
170
  ) => {
168
171
  const { render } = sideEffect;
@@ -197,6 +200,7 @@ const renderOneSideEffect = (
197
200
  replace,
198
201
  rootDirectoryUrl,
199
202
  errorTransform,
203
+ sourceLocation,
200
204
  });
201
205
  }
202
206
  if (sideEffect.code === "source_code") {
@@ -233,6 +237,7 @@ const renderText = (
233
237
  replace,
234
238
  rootDirectoryUrl,
235
239
  errorTransform,
240
+ sourceLocation,
236
241
  },
237
242
  ) => {
238
243
  if (text && typeof text === "object") {
@@ -247,8 +252,15 @@ const renderText = (
247
252
  sideEffectMdFileUrl,
248
253
  { preferRelativeNotation: true },
249
254
  );
250
- const sourceCodeLinkText = `${callSiteRelativeUrl}:${callSite.line}:${callSite.column}`;
251
- const sourceCodeLinkHref = `${callSiteRelativeUrl}#L${callSite.line}`;
255
+ let sourceCodeLinkText;
256
+ let sourceCodeLinkHref;
257
+ if (sourceLocation) {
258
+ sourceCodeLinkText = `${callSiteRelativeUrl}:${callSite.line}:${callSite.column}`;
259
+ sourceCodeLinkHref = `${callSiteRelativeUrl}#L${callSite.line}`;
260
+ } else {
261
+ sourceCodeLinkText = `${callSiteRelativeUrl}`;
262
+ sourceCodeLinkHref = `${callSiteRelativeUrl}`;
263
+ }
252
264
  sourceMd += "\n";
253
265
  sourceMd += renderSmallLink({
254
266
  text: sourceCodeLinkText,
@@ -53,6 +53,7 @@ export const snapshotTests = async (
53
53
  logEffects,
54
54
  filesystemEffects,
55
55
  throwWhenDiff = process.env.CI,
56
+ sourceLocation = false,
56
57
  } = options;
57
58
  filesystemActions = {
58
59
  "**": "compare",
@@ -184,10 +185,13 @@ export const snapshotTests = async (
184
185
  });
185
186
  sideEffectsMap.set(scenario, sideEffects);
186
187
  const sideEffectsMarkdown = renderSideEffects(sideEffects, {
187
- sourceFileUrl: `${callSite.url}#L${callSite.line}`,
188
+ sourceFileUrl: sourceLocation
189
+ ? `${callSite.url}#L${callSite.line}`
190
+ : callSite.url,
188
191
  sideEffectMdFileUrl: scenarioMdFileUrl,
189
192
  generateOutFileUrl: generateScenarioOutFileUrl,
190
193
  title: scenario,
194
+ sourceLocation,
191
195
  });
192
196
  writeFileSync(scenarioMdFileUrl, sideEffectsMarkdown);
193
197
  }