@jsenv/snapshot 2.8.0 → 2.8.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.8.0",
3
+ "version": "2.8.2",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@jsenv/assert": "4.1.14",
37
- "@jsenv/ast": "6.2.13",
37
+ "@jsenv/ast": "6.2.14",
38
38
  "@jsenv/exception": "1.0.1",
39
39
  "@jsenv/filesystem": "4.9.9",
40
40
  "@jsenv/terminal-recorder": "1.4.3",
@@ -58,6 +58,7 @@ export const createWellKnown = (name, replacement = name) => {
58
58
 
59
59
  export const createReplaceFilesystemWellKnownValues = ({
60
60
  rootDirectoryUrl,
61
+ localhostUrl,
61
62
  // for unit tests
62
63
  isWindows = process.platform === "win32",
63
64
  ancestorPackagesDisabled,
@@ -126,9 +127,32 @@ export const createReplaceFilesystemWellKnownValues = ({
126
127
  }
127
128
  };
128
129
  };
130
+ const addWellKnownUrl = (url, replacement) => {
131
+ const wellKnownUrl = {
132
+ url,
133
+ replace: (string) => {
134
+ return string.replaceAll(url, replacement);
135
+ },
136
+ };
137
+ wellKownUrlArray.push(wellKnownUrl);
138
+ return () => {
139
+ const urlIndex = wellKownUrlArray.indexOf(wellKnownUrl);
140
+ if (urlIndex > -1) {
141
+ wellKownUrlArray.splice(urlIndex, 1);
142
+ }
143
+ };
144
+ };
129
145
  if (rootDirectoryUrl) {
130
146
  addWellKnownFileUrl(rootDirectoryUrl, WELL_KNOWN_ROOT);
131
147
  }
148
+ if (localhostUrl) {
149
+ addWellKnownUrl(
150
+ localhostUrl,
151
+ localhostUrl.startsWith("https")
152
+ ? "https://localhost"
153
+ : "http://localhost",
154
+ );
155
+ }
132
156
  /*
133
157
  * When running code inside a node project ancestor packages
134
158
  * should make things super predictible because
@@ -21,11 +21,13 @@ export const replaceFluctuatingValues = (
21
21
  {
22
22
  stringType,
23
23
  rootDirectoryUrl,
24
+ localhostUrl,
24
25
  fileUrl,
25
26
  preserveAnsi,
26
27
  // for unit test
27
28
  replaceFilesystemWellKnownValues = createReplaceFilesystemWellKnownValues({
28
29
  rootDirectoryUrl,
30
+ localhostUrl,
29
31
  }),
30
32
  } = {},
31
33
  ) => {
@@ -53,27 +53,31 @@ export const spyFilesystemCalls = (
53
53
  : stateBefore.mtimeMs === stateAfter.mtimeMs
54
54
  ? ""
55
55
  : "mtime_modified";
56
- if (reason) {
57
- if (undoFilesystemSideEffects && !fileRestoreMap.has(fileUrl)) {
58
- if (stateBefore.found) {
59
- fileRestoreMap.set(fileUrl, () => {
60
- writeFileSync(fileUrl, stateBefore.buffer);
61
- });
62
- } else {
63
- fileRestoreMap.set(fileUrl, () => {
64
- removeFileSync(fileUrl, { allowUseless: true });
65
- });
66
- }
67
- }
68
- if (shouldReport(fileUrl)) {
69
- onWriteFile(fileUrl, stateAfter.buffer, reason);
70
- return;
56
+ if (!reason) {
57
+ // file is exactly the same
58
+ // function did not have any effect on the file
59
+ return;
60
+ }
61
+ if (!shouldReport(fileUrl)) {
62
+ return;
63
+ }
64
+ if (undoFilesystemSideEffects && !fileRestoreMap.has(fileUrl)) {
65
+ if (stateBefore.found) {
66
+ fileRestoreMap.set(fileUrl, () => {
67
+ writeFileSync(fileUrl, stateBefore.buffer);
68
+ });
69
+ } else {
70
+ fileRestoreMap.set(fileUrl, () => {
71
+ removeFileSync(fileUrl, { allowUseless: true });
72
+ });
71
73
  }
72
74
  }
73
- // file is exactly the same
74
- // function did not have any effect on the file
75
+ onWriteFile(fileUrl, stateAfter.buffer, reason);
75
76
  };
76
77
  const onWriteDirectoryDone = (directoryUrl) => {
78
+ if (!shouldReport(directoryUrl)) {
79
+ return;
80
+ }
77
81
  if (undoFilesystemSideEffects && !fileRestoreMap.has(directoryUrl)) {
78
82
  fileRestoreMap.set(directoryUrl, () => {
79
83
  removeDirectorySync(directoryUrl, {
@@ -82,9 +86,7 @@ export const spyFilesystemCalls = (
82
86
  });
83
87
  });
84
88
  }
85
- if (shouldReport(directoryUrl)) {
86
- onWriteDirectory(directoryUrl);
87
- }
89
+ onWriteDirectory(directoryUrl);
88
90
  };
89
91
  const restoreCallbackSet = new Set();
90
92