@jsenv/snapshot 2.2.5 → 2.2.7

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.2.5",
3
+ "version": "2.2.7",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -35,9 +35,9 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@jsenv/assert": "4.1.5",
38
- "@jsenv/ast": "6.2.5",
39
- "@jsenv/filesystem": "4.9.1",
40
- "@jsenv/urls": "2.3.1",
38
+ "@jsenv/ast": "6.2.6",
39
+ "@jsenv/filesystem": "4.9.2",
40
+ "@jsenv/urls": "2.3.2",
41
41
  "@jsenv/utils": "2.1.1",
42
42
  "pixelmatch": "6.0.0",
43
43
  "prettier": "3.3.3",
@@ -4,10 +4,12 @@ import {
4
4
  removeFileSync,
5
5
  writeFileSync,
6
6
  } from "@jsenv/filesystem";
7
- import { urlToRelativeUrl } from "@jsenv/urls/src/url_to_relative_url.js";
7
+ import { urlToFilename, urlToRelativeUrl } from "@jsenv/urls";
8
8
  import { takeDirectorySnapshot } from "./filesystem_snapshot.js";
9
9
  import { replaceFluctuatingValues } from "./replace_fluctuating_values.js";
10
10
 
11
+ const consoleSpySymbol = Symbol.for("console_spy_for_jsenv_snapshot");
12
+
11
13
  export const snapshotFunctionSideEffects = (
12
14
  fn,
13
15
  fnFileUrl,
@@ -17,7 +19,7 @@ export const snapshotFunctionSideEffects = (
17
19
  captureConsole = true,
18
20
  filesystemEffects,
19
21
  filesystemEffectsInline,
20
- restoreFilesystem,
22
+ restoreFilesystem = true,
21
23
  } = {},
22
24
  ) => {
23
25
  const sideEffectDirectoryUrl = new URL(
@@ -27,10 +29,8 @@ export const snapshotFunctionSideEffects = (
27
29
  const sideEffectDirectorySnapshot = takeDirectorySnapshot(
28
30
  sideEffectDirectoryUrl,
29
31
  );
30
- const sideEffectFileUrl = new URL(
31
- "./side_effect.txt",
32
- sideEffectDirectoryUrl,
33
- );
32
+ const sideEffectFilename = `${urlToFilename(sideEffectDirectoryUrl)}_side_effects.txt`;
33
+ const sideEffectFileUrl = new URL(sideEffectFilename, sideEffectDirectoryUrl);
34
34
  const sideEffects = [];
35
35
  const finallyCallbackSet = new Set();
36
36
  const onError = (e) => {
@@ -61,12 +61,17 @@ export const snapshotFunctionSideEffects = (
61
61
  if (captureConsole) {
62
62
  const installConsoleSpy = (methodName) => {
63
63
  const methodSpied = console[methodName];
64
- console[methodName] = (message) => {
64
+ if (consoleSpySymbol in methodSpied) {
65
+ throw new Error("snapshotFunctionSideEffects already running");
66
+ }
67
+ const methodSpy = (message) => {
65
68
  sideEffects.push({
66
69
  type: `console.${methodName}`,
67
70
  value: message,
68
71
  });
69
72
  };
73
+ methodSpy[consoleSpySymbol] = true;
74
+ console[methodName] = methodSpy;
70
75
  finallyCallbackSet.add(() => {
71
76
  console[methodName] = methodSpied;
72
77
  });
@@ -141,8 +146,7 @@ export const snapshotFunctionSideEffects = (
141
146
  try {
142
147
  const returnValue = fn();
143
148
  if (returnValue && returnValue.then) {
144
- returnedPromise = true;
145
- returnValue.then(
149
+ returnedPromise = returnValue.then(
146
150
  (value) => {
147
151
  onResult(value);
148
152
  onFinally();
@@ -152,14 +156,16 @@ export const snapshotFunctionSideEffects = (
152
156
  onFinally();
153
157
  },
154
158
  );
155
- } else {
156
- onResult(returnValue);
159
+ return returnedPromise;
157
160
  }
161
+ onResult(returnValue);
162
+ return null;
158
163
  } catch (e) {
159
164
  onError(e);
165
+ return null;
160
166
  } finally {
161
167
  if (returnedPromise) {
162
- return;
168
+ return returnedPromise;
163
169
  }
164
170
  onFinally();
165
171
  }