@jsenv/snapshot 2.8.3 → 2.8.4

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.8.3",
3
+ "version": "2.8.4",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -9,7 +9,7 @@ import {
9
9
  writeFileSync,
10
10
  } from "@jsenv/filesystem";
11
11
  import { URL_META } from "@jsenv/url-meta";
12
- import { yieldAncestorUrls } from "@jsenv/urls";
12
+ import { ensurePathnameTrailingSlash, yieldAncestorUrls } from "@jsenv/urls";
13
13
  import { readFileSync, statSync } from "node:fs";
14
14
  import { pathToFileURL } from "node:url";
15
15
  import {
@@ -101,7 +101,9 @@ export const spyFilesystemCalls = (
101
101
  _internalFs,
102
102
  "mkdir",
103
103
  (directoryPath, mode, recursive) => {
104
- const directoryUrl = pathToFileURL(directoryPath);
104
+ const directoryUrl = ensurePathnameTrailingSlash(
105
+ pathToFileURL(directoryPath),
106
+ );
105
107
  const stateBefore = getDirectoryState(directoryPath);
106
108
  if (!stateBefore.found && recursive) {
107
109
  const ancestorNotFoundArray = [];
@@ -110,7 +112,9 @@ export const spyFilesystemCalls = (
110
112
  if (ancestorState.found) {
111
113
  break;
112
114
  }
113
- ancestorNotFoundArray.unshift(ancestorUrl);
115
+ ancestorNotFoundArray.unshift(
116
+ ensurePathnameTrailingSlash(ancestorUrl),
117
+ );
114
118
  }
115
119
  return {
116
120
  return: (fd) => {
@@ -151,6 +155,13 @@ export const spyFilesystemCalls = (
151
155
  },
152
156
  { execute: METHOD_EXECUTION_NODE_CALLBACK },
153
157
  );
158
+ /*
159
+ * Relying on open/close to detect writes is done to be able to catch
160
+ * write done async, not sure how to distinguish open/close done to write
161
+ * from open/close done to read file stat
162
+ * open/close for file stat are excluded because we compare stateBefore/stateAfter
163
+ * but ideally we would early return by detecting open/close is not for write operations
164
+ */
154
165
  const closeHook = hookIntoMethod(
155
166
  _internalFs,
156
167
  "close",
@@ -4,7 +4,7 @@ import { groupLogSideEffects } from "./group_log_side_effects.js";
4
4
 
5
5
  const logSideEffectsOptionsDefault = {
6
6
  prevent: true,
7
- group: false,
7
+ group: true,
8
8
  };
9
9
 
10
10
  export const logSideEffects = (logSideEffectsOptions) => {
@@ -50,6 +50,7 @@ export const renderSideEffects = (
50
50
  dedicatedFile: { line: 50, length: 5000 },
51
51
  }),
52
52
  errorStackHidden,
53
+ errorMessageTransform,
53
54
  } = {},
54
55
  ) => {
55
56
  const { rootDirectoryUrl, replaceFilesystemWellKnownValues } =
@@ -93,6 +94,7 @@ export const renderSideEffects = (
93
94
  getBigSizeEffect,
94
95
  replace,
95
96
  errorStackHidden,
97
+ errorMessageTransform,
96
98
  lastSideEffectNumber,
97
99
  });
98
100
  }
@@ -141,6 +143,7 @@ const renderOneSideEffect = (
141
143
  getBigSizeEffect,
142
144
  replace,
143
145
  errorStackHidden,
146
+ errorMessageTransform,
144
147
  lastSideEffectNumber,
145
148
  },
146
149
  ) => {
@@ -177,6 +180,7 @@ const renderOneSideEffect = (
177
180
  replace,
178
181
  rootDirectoryUrl,
179
182
  errorStackHidden,
183
+ errorMessageTransform,
180
184
  });
181
185
  }
182
186
  if (sideEffect.code === "source_code") {
@@ -213,6 +217,7 @@ const renderText = (
213
217
  replace,
214
218
  rootDirectoryUrl,
215
219
  errorStackHidden,
220
+ errorMessageTransform,
216
221
  },
217
222
  ) => {
218
223
  if (text && typeof text === "object") {
@@ -250,7 +255,10 @@ const renderText = (
250
255
  typeof jsValue.stack === "string")
251
256
  ) {
252
257
  // return renderMarkdownBlock(text.value.stack);
253
- const exception = createException(jsValue, { rootDirectoryUrl });
258
+ const exception = createException(jsValue, {
259
+ rootDirectoryUrl,
260
+ errorMessageTransform,
261
+ });
254
262
  const exceptionText = errorStackHidden
255
263
  ? `${exception.name}: ${exception.message}`
256
264
  : exception.stack || exception.message || exception;
@@ -30,6 +30,7 @@ export const snapshotTests = async (
30
30
  linkToSource = true,
31
31
  linkToEachSource,
32
32
  errorStackHidden,
33
+ errorMessageTransform,
33
34
  logEffects,
34
35
  filesystemEffects,
35
36
  throwWhenDiff = process.env.CI,
@@ -89,6 +90,15 @@ export const snapshotTests = async (
89
90
  markdown += generatedByLink;
90
91
  }
91
92
 
93
+ const outDirectoryRelativeUrl = outDirectoryPattern.replaceAll(
94
+ "[test_name]",
95
+ testName,
96
+ );
97
+ const outDirectoryUrl = new URL(outDirectoryRelativeUrl, testFileUrl);
98
+ const outDirectorySnapshot = takeDirectorySnapshot(outDirectoryUrl, {
99
+ "**/*": true,
100
+ "**/*.svg": "presence_only",
101
+ });
92
102
  for (const [scenario, { fn, callSite }] of activeTestMap) {
93
103
  markdown += "\n\n";
94
104
  markdown += `## ${scenario}`;
@@ -99,16 +109,6 @@ export const snapshotTests = async (
99
109
  });
100
110
  sideEffectsMap.set(scenario, sideEffects);
101
111
  const testScenario = asValidFilename(scenario);
102
- let outDirectoryRelativeUrl = outDirectoryPattern
103
- .replaceAll("[test_name]", testName)
104
- .replaceAll("[test_scenario]", testScenario);
105
- const outDirectoryUrl = new URL(outDirectoryRelativeUrl, testFileUrl);
106
- const outDirectorySnapshot = takeDirectorySnapshot(outDirectoryUrl, {
107
- pattern: {
108
- "**/*": true,
109
- "**/*.svg": "presence_only",
110
- },
111
- });
112
112
  const generateOutFileUrl = (filename) => {
113
113
  const outFileRelativeUrl = outFilePattern
114
114
  .replaceAll("[test_name]", testName)
@@ -127,8 +127,8 @@ export const snapshotTests = async (
127
127
  generatedBy: false,
128
128
  titleLevel: 3,
129
129
  errorStackHidden,
130
+ errorMessageTransform,
130
131
  });
131
- outDirectorySnapshot.compare(throwWhenDiff);
132
132
  markdown += sideEffectsMarkdown;
133
133
  }
134
134
  const sideEffectFileSnapshot = takeFileSnapshot(sideEffectFileUrl);
@@ -136,6 +136,7 @@ export const snapshotTests = async (
136
136
  mockFluctuatingValues: false,
137
137
  throwWhenDiff,
138
138
  });
139
+ outDirectorySnapshot.compare(throwWhenDiff);
139
140
 
140
141
  return { dirUrlMap, sideEffectsMap };
141
142
  };