@jsenv/snapshot 2.3.3 → 2.4.1

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.3.3",
3
+ "version": "2.4.1",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -15,19 +15,29 @@ import {
15
15
  import { spyConsoleCalls } from "./spy_console_calls.js";
16
16
  import { spyFilesystemCalls } from "./spy_filesystem_calls.js";
17
17
 
18
+ const filesystemEffectsDefault = {
19
+ outDirectory: null,
20
+ preserve: false,
21
+ };
22
+ const consoleEffectsDefault = {
23
+ prevent: true,
24
+ };
25
+
18
26
  export const snapshotFunctionSideEffects = (
19
27
  fn,
20
28
  fnFileUrl,
21
29
  sideEffectDirectoryRelativeUrl = "./",
22
30
  {
23
31
  rootDirectoryUrl = new URL("./", fnFileUrl),
24
- filesystemEffectsDirectory,
25
- preventConsoleSideEffects = true,
26
- undoFilesystemSideEffects = true,
32
+ consoleEffects = true,
33
+ filesystemEffects = true,
27
34
  } = {},
28
35
  ) => {
29
- if (filesystemEffectsDirectory === true) {
30
- filesystemEffectsDirectory = "./fs/";
36
+ if (consoleEffects === true) {
37
+ consoleEffects = {};
38
+ }
39
+ if (filesystemEffects === true) {
40
+ filesystemEffects = {};
31
41
  }
32
42
  const sideEffectDirectoryUrl = new URL(
33
43
  sideEffectDirectoryRelativeUrl,
@@ -40,103 +50,123 @@ export const snapshotFunctionSideEffects = (
40
50
  const sideEffectFileUrl = new URL(sideEffectFilename, sideEffectDirectoryUrl);
41
51
  const callbackSet = new Set();
42
52
  const sideEffectDetectors = [
43
- {
44
- name: "console",
45
- install: (addSideEffect) => {
46
- const onConsole = (methodName, message) => {
47
- addSideEffect({
48
- type: `console:${methodName}`,
49
- value: message,
50
- label: `console.${methodName}`,
51
- text: wrapIntoMarkdownBlock(
52
- replaceFluctuatingValues(message, {
53
- stringType: "console",
54
- rootDirectoryUrl,
55
- }),
56
- "console",
57
- ),
58
- });
59
- };
60
- const consoleSpy = spyConsoleCalls(
53
+ ...(consoleEffects
54
+ ? [
61
55
  {
62
- error: (message) => {
63
- onConsole("error", message);
64
- },
65
- warn: (message) => {
66
- onConsole("warn", message);
67
- },
68
- info: (message) => {
69
- onConsole("info", message);
70
- },
71
- log: (message) => {
72
- onConsole("log", message);
73
- },
74
- },
75
- {
76
- preventConsoleSideEffects,
77
- },
78
- );
79
- return () => {
80
- consoleSpy.restore();
81
- };
82
- },
83
- },
84
- {
85
- name: "filesystem",
86
- install: (addSideEffect) => {
87
- const fsSideEffectDirectoryUrl = ensurePathnameTrailingSlash(
88
- new URL(filesystemEffectsDirectory, sideEffectDirectoryUrl),
89
- );
90
- const fsSideEffectsDirectoryRelativeUrl = urlToRelativeUrl(
91
- fsSideEffectDirectoryUrl,
92
- sideEffectFileUrl,
93
- );
94
- const filesystemSpy = spyFilesystemCalls(
95
- {
96
- writeFile: (url, content) => {
97
- const relativeUrl = urlToRelativeUrl(url, fnFileUrl);
98
- const toUrl = new URL(relativeUrl, fsSideEffectDirectoryUrl);
99
- if (filesystemEffectsDirectory) {
100
- callbackSet.add(() => {
101
- writeFileSync(toUrl, content);
102
- });
103
- addSideEffect({
104
- type: "fs:write_file",
105
- value: { relativeUrl, content },
106
- label: `write file "${relativeUrl}" (see ./${fsSideEffectsDirectoryRelativeUrl}${relativeUrl})`,
107
- text: null,
108
- });
109
- } else {
56
+ name: "console",
57
+ install: (addSideEffect) => {
58
+ consoleEffects = { ...consoleEffectsDefault, ...consoleEffects };
59
+ const { prevent } = consoleEffects;
60
+ const onConsole = (methodName, message) => {
110
61
  addSideEffect({
111
- type: "fs:write_file",
112
- value: { relativeUrl, content },
113
- label: `write file "${relativeUrl}"`,
62
+ type: `console:${methodName}`,
63
+ value: message,
64
+ label: `console.${methodName}`,
114
65
  text: wrapIntoMarkdownBlock(
115
- content,
116
- urlToExtension(url).slice(1),
66
+ replaceFluctuatingValues(message, {
67
+ stringType: "console",
68
+ rootDirectoryUrl,
69
+ }),
70
+ "console",
117
71
  ),
118
72
  });
119
- }
120
- },
121
- writeDirectory: (url) => {
122
- const relativeUrl = urlToRelativeUrl(url, fnFileUrl);
123
- addSideEffect({
124
- type: "fs:write_directory",
125
- value: { relativeUrl },
126
- label: `write directory "${relativeUrl}"`,
127
- text: null,
128
- });
73
+ };
74
+ const consoleSpy = spyConsoleCalls(
75
+ {
76
+ error: (message) => {
77
+ onConsole("error", message);
78
+ },
79
+ warn: (message) => {
80
+ onConsole("warn", message);
81
+ },
82
+ info: (message) => {
83
+ onConsole("info", message);
84
+ },
85
+ log: (message) => {
86
+ onConsole("log", message);
87
+ },
88
+ },
89
+ {
90
+ preventConsoleSideEffects: prevent,
91
+ },
92
+ );
93
+ return () => {
94
+ consoleSpy.restore();
95
+ };
129
96
  },
130
97
  },
98
+ ]
99
+ : []),
100
+ ...(filesystemEffects
101
+ ? [
131
102
  {
132
- undoFilesystemSideEffects,
103
+ name: "filesystem",
104
+ install: (addSideEffect) => {
105
+ filesystemEffects = {
106
+ ...filesystemEffectsDefault,
107
+ ...filesystemEffects,
108
+ };
109
+ let writeFile;
110
+ const { preserve, outDirectory } = filesystemEffects;
111
+ if (outDirectory) {
112
+ const fsEffectsOutDirectoryUrl = ensurePathnameTrailingSlash(
113
+ new URL(outDirectory, sideEffectDirectoryUrl),
114
+ );
115
+ const fsEffectsOutDirectoryRelativeUrl = urlToRelativeUrl(
116
+ fsEffectsOutDirectoryUrl,
117
+ sideEffectFileUrl,
118
+ );
119
+ writeFile = (url, content) => {
120
+ const relativeUrl = urlToRelativeUrl(url, fnFileUrl);
121
+ const toUrl = new URL(relativeUrl, fsEffectsOutDirectoryUrl);
122
+ callbackSet.add(() => {
123
+ writeFileSync(toUrl, content);
124
+ });
125
+ addSideEffect({
126
+ type: "fs:write_file",
127
+ value: { relativeUrl, content },
128
+ label: `write file "${relativeUrl}" (see ./${fsEffectsOutDirectoryRelativeUrl}${relativeUrl})`,
129
+ text: null,
130
+ });
131
+ };
132
+ } else {
133
+ writeFile = (url, content) => {
134
+ const relativeUrl = urlToRelativeUrl(url, fnFileUrl);
135
+ addSideEffect({
136
+ type: "fs:write_file",
137
+ value: { relativeUrl, content },
138
+ label: `write file "${relativeUrl}"`,
139
+ text: wrapIntoMarkdownBlock(
140
+ content,
141
+ urlToExtension(url).slice(1),
142
+ ),
143
+ });
144
+ };
145
+ }
146
+ const filesystemSpy = spyFilesystemCalls(
147
+ {
148
+ writeFile,
149
+ writeDirectory: (url) => {
150
+ const relativeUrl = urlToRelativeUrl(url, fnFileUrl);
151
+ addSideEffect({
152
+ type: "fs:write_directory",
153
+ value: { relativeUrl },
154
+ label: `write directory "${relativeUrl}"`,
155
+ text: null,
156
+ });
157
+ },
158
+ },
159
+ {
160
+ undoFilesystemSideEffects: !preserve,
161
+ },
162
+ );
163
+ return () => {
164
+ filesystemSpy.restore();
165
+ };
166
+ },
133
167
  },
134
- );
135
- return () => {
136
- filesystemSpy.restore();
137
- };
138
- },
139
- },
168
+ ]
169
+ : []),
140
170
  ];
141
171
 
142
172
  const onSideEffectsCollected = (sideEffects) => {
@@ -1,16 +1,19 @@
1
1
  // https://github.com/antfu/fs-spy/blob/main/src/index.ts
2
2
  // https://github.com/tschaub/mock-fs/tree/main
3
+ // https://github.com/tschaub/mock-fs/blob/6e84d5bb320022624c7d770432e3322323ce043e/lib/binding.js#L353
4
+ // https://github.com/tschaub/mock-fs/issues/348
3
5
 
4
6
  import { removeDirectorySync, removeFileSync } from "@jsenv/filesystem";
5
7
  import { readFileSync, statSync, writeFileSync } from "node:fs";
6
8
  import { pathToFileURL } from "node:url";
7
- import { spyMethod } from "./spy_method.js";
9
+ import { disableSpiesWhileCalling, spyMethod } from "./spy_method.js";
8
10
 
9
11
  export const spyFilesystemCalls = (
10
12
  {
13
+ readFile = () => {}, // TODO
11
14
  writeFile = () => {},
12
15
  writeDirectory = () => {},
13
- removeFile = () => {},
16
+ removeFile = () => {}, // TODO
14
17
  // removeDirectory = () => {},
15
18
  },
16
19
  { undoFilesystemSideEffects } = {},
@@ -61,6 +64,14 @@ export const spyFilesystemCalls = (
61
64
  writeDirectory(directoryUrl);
62
65
  };
63
66
  const restoreCallbackSet = new Set();
67
+
68
+ const getFileStateWithinSpy = (fileUrl) => {
69
+ return disableSpiesWhileCalling(
70
+ () => getFileState(fileUrl),
71
+ [openSpy, closeSpy],
72
+ );
73
+ };
74
+
64
75
  const mkdirSpy = spyMethod(
65
76
  _internalFs,
66
77
  "mkdir",
@@ -90,54 +101,118 @@ export const spyFilesystemCalls = (
90
101
  _internalFs,
91
102
  "open",
92
103
  (filePath, flags, mode, callback) => {
104
+ const stateBefore = getFileStateWithinSpy(filePath);
105
+ filesystemStateInfoMap.set(filePath, stateBefore);
93
106
  if (callback) {
94
- const stateBefore = getFileState(filePath);
95
- filesystemStateInfoMap.set(filePath, stateBefore);
96
- const oncomplete = callback.oncomplete;
97
- callback.oncomplete = (error, fd) => {
107
+ if (callback.context) {
108
+ const original = callback.context.callback;
109
+ callback.context.callback = function (...args) {
110
+ callback.context.callback = original;
111
+ const [error, fd] = args;
112
+ if (error) {
113
+ original.call(this, ...args);
114
+ } else {
115
+ if (typeof fd === "number") {
116
+ fileDescriptorPathMap.set(fd, filePath);
117
+ } else {
118
+ // it's a buffer (happens for readFile)
119
+ }
120
+ original.call(this, ...args);
121
+ }
122
+ };
123
+ openSpy.callOriginal();
124
+ return;
125
+ }
126
+ const original = callback.oncomplete;
127
+ callback.oncomplete = function (...args) {
128
+ callback.oncomplete = original;
129
+ const [error, fd] = args;
98
130
  if (error) {
99
- oncomplete.call(callback, error);
131
+ original.call(this, ...args);
100
132
  } else {
101
133
  fileDescriptorPathMap.set(fd, filePath);
102
- oncomplete.call(callback, error, fd);
134
+ original.call(this, ...args);
103
135
  }
104
136
  };
105
- return openSpy.callOriginal();
137
+ openSpy.callOriginal();
138
+ return;
106
139
  }
107
- const stateBefore = getFileState(filePath);
108
- filesystemStateInfoMap.set(filePath, stateBefore);
109
140
  const fd = openSpy.callOriginal();
110
- fileDescriptorPathMap.set(fd, filePath);
111
- return fd;
141
+ if (typeof fd === "number") {
142
+ fileDescriptorPathMap.set(fd, filePath);
143
+ } else {
144
+ // it's a buffer (happens for readFile)
145
+ }
112
146
  },
113
147
  );
114
- const closeSpy = spyMethod(_internalFs, "close", (fileDescriptor) => {
115
- const filePath = fileDescriptorPathMap.get(fileDescriptor);
116
- if (!filePath) {
117
- return closeSpy.callOriginal();
118
- }
119
- const openInfo = filesystemStateInfoMap.get(filePath);
120
- if (!openInfo) {
121
- const returnValue = closeSpy.callOriginal();
148
+ const closeSpy = spyMethod(
149
+ _internalFs,
150
+ "close",
151
+ (fileDescriptor, callback) => {
152
+ const filePath = fileDescriptorPathMap.get(fileDescriptor);
153
+ if (!filePath) {
154
+ closeSpy.callOriginal();
155
+ return;
156
+ }
157
+ const stateBefore = filesystemStateInfoMap.get(filePath);
158
+ if (!stateBefore) {
159
+ closeSpy.callOriginal();
160
+ fileDescriptorPathMap.delete(fileDescriptor);
161
+ return;
162
+ }
163
+ const fileUrl = pathToFileURL(filePath);
164
+ if (callback) {
165
+ if (callback.context) {
166
+ const original = callback.context.callback;
167
+ callback.context.callback = function (...args) {
168
+ callback.context.callback = original;
169
+ const [error] = args;
170
+ if (error) {
171
+ original.call(this, ...args);
172
+ } else {
173
+ original.call(this, ...args);
174
+ fileDescriptorPathMap.delete(fileDescriptor);
175
+ filesystemStateInfoMap.delete(filePath);
176
+ const stateAfter = getFileStateWithinSpy(fileUrl);
177
+ readFile(fileUrl);
178
+ onWriteFileDone(fileUrl, stateBefore, stateAfter);
179
+ }
180
+ };
181
+ closeSpy.callOriginal();
182
+ return;
183
+ }
184
+ const original = callback.oncomplete;
185
+ callback.oncomplete = function (...args) {
186
+ callback.oncomplete = original;
187
+ const [error] = args;
188
+ if (error) {
189
+ original.call(this, ...args);
190
+ } else {
191
+ original.call(this, ...args);
192
+ fileDescriptorPathMap.delete(fileDescriptor);
193
+ filesystemStateInfoMap.delete(filePath);
194
+ const stateAfter = getFileStateWithinSpy(fileUrl);
195
+ onWriteFileDone(fileUrl, stateBefore, stateAfter);
196
+ }
197
+ };
198
+ closeSpy.callOriginal();
199
+ return;
200
+ }
201
+ closeSpy.callOriginal();
122
202
  fileDescriptorPathMap.delete(fileDescriptor);
123
- return returnValue;
124
- }
125
- const fileUrl = pathToFileURL(filePath);
126
- const nowState = getFileState(fileUrl);
127
- onWriteFileDone(fileUrl, openInfo, nowState);
128
- const returnValue = closeSpy.callOriginal();
129
- fileDescriptorPathMap.delete(fileDescriptor);
130
- filesystemStateInfoMap.delete(filePath);
131
- return returnValue;
132
- });
203
+ filesystemStateInfoMap.delete(filePath);
204
+ const stateAfter = getFileStateWithinSpy(fileUrl);
205
+ onWriteFileDone(fileUrl, stateBefore, stateAfter);
206
+ },
207
+ );
133
208
  const writeFileUtf8Spy = spyMethod(
134
209
  _internalFs,
135
210
  "writeFileUtf8",
136
211
  (filePath) => {
137
212
  const fileUrl = pathToFileURL(filePath);
138
- const stateBefore = getFileState(fileUrl);
213
+ const stateBefore = getFileStateWithinSpy(fileUrl);
139
214
  writeFileUtf8Spy.callOriginal();
140
- const stateAfter = getFileState(fileUrl);
215
+ const stateAfter = getFileStateWithinSpy(fileUrl);
141
216
  onWriteFileDone(fileUrl, stateBefore, stateAfter);
142
217
  },
143
218
  );
@@ -16,6 +16,7 @@ export const spyMethod = (object, method, spyCallback) => {
16
16
  preventOriginalCall = jsenvSpySymbolValue.preventOriginalCall;
17
17
  } else {
18
18
  const original = current;
19
+ let currentThis;
19
20
  let currentArgs;
20
21
  let originalCalled = false;
21
22
  let originalReturnValue;
@@ -32,33 +33,40 @@ export const spyMethod = (object, method, spyCallback) => {
32
33
  return originalReturnValue;
33
34
  }
34
35
  someSpyUsedCallOriginal = true;
35
- onOriginalCall(original(...currentArgs));
36
+ onOriginalCall(original.call(currentThis, ...currentArgs));
36
37
  return originalReturnValue;
37
38
  };
38
39
  preventOriginalCall = () => {
39
40
  preventOriginalCallCalled = true;
40
41
  };
41
42
  const spyCallbackSet = new Set();
42
- const spy = (...args) => {
43
+ const spy = function (...args) {
43
44
  if (spyExecuting) {
44
45
  // when a spy is executing
45
46
  // if it calls the method himself
46
47
  // then we want this call to go trough
47
48
  // and others spy should not know about it
48
- onOriginalCall(original(...args));
49
+ onOriginalCall(original.call(this, ...args));
49
50
  return originalReturnValue;
50
51
  }
51
52
  spyExecuting = true;
52
53
  originalCalled = false;
54
+ currentThis = this;
53
55
  currentArgs = args;
54
56
  someSpyUsedCallOriginal = false;
55
- allSpyUsedPreventOriginalCall = true;
57
+ allSpyUsedPreventOriginalCall = undefined;
56
58
  for (const spyCallback of spyCallbackSet) {
59
+ if (spyCallback.disabled) {
60
+ continue;
61
+ }
57
62
  try {
58
63
  spyCallback(...args);
59
64
  } finally {
60
65
  if (preventOriginalCallCalled) {
61
66
  preventOriginalCallCalled = false;
67
+ if (allSpyUsedPreventOriginalCall === undefined) {
68
+ allSpyUsedPreventOriginalCall = true;
69
+ }
62
70
  } else {
63
71
  allSpyUsedPreventOriginalCall = false;
64
72
  }
@@ -68,6 +76,7 @@ export const spyMethod = (object, method, spyCallback) => {
68
76
  if (!someSpyUsedCallOriginal && !allSpyUsedPreventOriginalCall) {
69
77
  callOriginal();
70
78
  }
79
+ currentThis = null;
71
80
  currentArgs = null;
72
81
  if (originalCalled) {
73
82
  originalCalled = false;
@@ -103,9 +112,28 @@ export const spyMethod = (object, method, spyCallback) => {
103
112
  const spyHooks = {
104
113
  callOriginal,
105
114
  preventOriginalCall,
115
+ disable: () => {
116
+ spyCallback.disabled = true;
117
+ },
118
+ enable: () => {
119
+ spyCallback.disabled = false;
120
+ },
106
121
  remove: () => {
107
122
  removeCallback(spyCallback);
108
123
  },
109
124
  };
110
125
  return spyHooks;
111
126
  };
127
+
128
+ export const disableSpiesWhileCalling = (fn, spyToDisableArray) => {
129
+ for (const spyToDisable of spyToDisableArray) {
130
+ spyToDisable.disable();
131
+ }
132
+ try {
133
+ return fn();
134
+ } finally {
135
+ for (const spyToEnable of spyToDisableArray) {
136
+ spyToEnable.enable();
137
+ }
138
+ }
139
+ };