@jsenv/snapshot 2.7.0 → 2.7.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.7.0",
3
+ "version": "2.7.2",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -12,7 +12,6 @@
12
12
  "url": "https://github.com/jsenv/core",
13
13
  "directory": "packages/independent/snapshot"
14
14
  },
15
- "bin": "./src/cli.mjs",
16
15
  "engines": {
17
16
  "node": ">=20.0.0"
18
17
  },
@@ -37,7 +36,7 @@
37
36
  "@jsenv/assert": "4.1.13",
38
37
  "@jsenv/ast": "6.2.12",
39
38
  "@jsenv/exception": "1.0.1",
40
- "@jsenv/filesystem": "4.9.7",
39
+ "@jsenv/filesystem": "4.9.8",
41
40
  "@jsenv/terminal-recorder": "1.4.2",
42
41
  "@jsenv/urls": "2.5.1",
43
42
  "@jsenv/utils": "2.1.2",
@@ -38,7 +38,7 @@ export const spyFilesystemCalls = (
38
38
  const fileRestoreMap = new Map();
39
39
  const onWriteFileDone = (fileUrl, stateBefore, stateAfter) => {
40
40
  if (!stateAfter.found) {
41
- // not suppoed to happen but could apparently
41
+ // seems to be possible somehow
42
42
  return;
43
43
  }
44
44
  // we use same type because we don't want to differentiate between
@@ -29,6 +29,9 @@ export const hookIntoMethod = (
29
29
  const callHooks = (hookCallbackSet, ...args) => {
30
30
  hookExecuting = true;
31
31
  for (const hookCallback of hookCallbackSet) {
32
+ if (!hookCallback.initCallback.installed) {
33
+ continue;
34
+ }
32
35
  hookCallback(...args);
33
36
  }
34
37
  hookExecuting = false;
@@ -62,15 +65,19 @@ export const hookIntoMethod = (
62
65
  allWantsToPreventOriginalCall = false;
63
66
  }
64
67
  if (hooks.returnPromise) {
68
+ hooks.returnPromise.initCallback = initCallback;
65
69
  returnPromiseCallbackSet.add(hooks.returnPromise);
66
70
  }
67
71
  if (hooks.return) {
72
+ hooks.return.initCallback = initCallback;
68
73
  returnCallbackSet.add(hooks.return);
69
74
  }
70
75
  if (hooks.catch) {
76
+ hooks.catch.initCallback = initCallback;
71
77
  catchCallbackSet.add(hooks.catch);
72
78
  }
73
79
  if (hooks.finally) {
80
+ hooks.finally.initCallback = initCallback;
74
81
  finallyCallbackSet.add(hooks.catch);
75
82
  }
76
83
  }
@@ -124,6 +131,7 @@ export const hookIntoMethod = (
124
131
  };
125
132
  object[method] = proxy;
126
133
  }
134
+ initCallback.installed = true;
127
135
  addInitCallback(initCallback);
128
136
  const hook = {
129
137
  disable: () => {
@@ -133,6 +141,7 @@ export const hookIntoMethod = (
133
141
  initCallback.disabled = false;
134
142
  },
135
143
  remove: () => {
144
+ initCallback.installed = false;
136
145
  removeInitCallback(initCallback);
137
146
  },
138
147
  };
@@ -220,7 +229,9 @@ export const METHOD_EXECUTION_NODE_CALLBACK = ({
220
229
  // useless because are a copy of the args
221
230
  // so the mutation we do above ( args[lastArgIndex] =)
222
231
  // cannot be important for the method being proxied
223
- args[lastArgIndex] = lastArg;
232
+ if (args[lastArgIndex] === callbackProxy) {
233
+ args[lastArgIndex] = lastArg;
234
+ }
224
235
  };
225
236
  });
226
237
  }
@@ -230,7 +241,9 @@ export const METHOD_EXECUTION_NODE_CALLBACK = ({
230
241
  return installProxyAndCall(originalCallback, (callbackProxy) => {
231
242
  lastArg.context.callback = callbackProxy;
232
243
  return () => {
233
- lastArg.context.callback = originalCallback;
244
+ if (lastArg.context.callback === callbackProxy) {
245
+ lastArg.context.callback = originalCallback;
246
+ }
234
247
  };
235
248
  });
236
249
  }
@@ -239,7 +252,9 @@ export const METHOD_EXECUTION_NODE_CALLBACK = ({
239
252
  return installProxyAndCall(originalCallback, (callbackProxy) => {
240
253
  lastArg.oncomplete = callbackProxy;
241
254
  return () => {
242
- lastArg.oncomplete = originalCallback;
255
+ if (lastArg.oncomplete === callbackProxy) {
256
+ lastArg.oncomplete = originalCallback;
257
+ }
243
258
  };
244
259
  });
245
260
  }
@@ -11,21 +11,21 @@ export const snapshotSideEffects = (
11
11
  fn,
12
12
  {
13
13
  sideEffectFileUrl,
14
- sideEffectFilePattern = "./output/[basename].md",
15
- outDirectoryPattern = "./output/[basename]/",
16
- outFilePattern = "./output/[basename]/[filename]",
14
+ outDirectoryPattern = "./side_effects/",
15
+ sideEffectFilePattern = "./side_effects/[basename].md",
16
+ outFilePattern = "./side_effects/[name]/[filename]",
17
17
  generateOutFileUrl,
18
18
  outDirectoryUrl,
19
19
  errorStackHidden,
20
20
  ...captureOptions
21
21
  } = {},
22
22
  ) => {
23
- const basename = urlToBasename(sourceFileUrl, true);
23
+ const name = urlToBasename(sourceFileUrl, true);
24
+ const basename = urlToBasename(sourceFileUrl);
24
25
  if (sideEffectFileUrl === undefined) {
25
- const sideEffectFileRelativeUrl = sideEffectFilePattern.replaceAll(
26
- "[basename]",
27
- basename,
28
- );
26
+ const sideEffectFileRelativeUrl = sideEffectFilePattern
27
+ .replaceAll("[basename]", basename)
28
+ .replaceAll("[name]", name);
29
29
  sideEffectFileUrl = new URL(sideEffectFileRelativeUrl, sourceFileUrl);
30
30
  } else {
31
31
  sideEffectFileUrl = new URL(sideEffectFileUrl, sourceFileUrl);
@@ -42,6 +42,7 @@ export const snapshotSideEffects = (
42
42
  if (generateOutFileUrl === undefined) {
43
43
  generateOutFileUrl = (filename) => {
44
44
  const outRelativeUrl = outFilePattern
45
+ .replaceAll("[name]", name)
45
46
  .replaceAll("[basename]", basename)
46
47
  .replaceAll("[filename]", filename);
47
48
  const outFileUrl = new URL(outRelativeUrl, new URL("./", sourceFileUrl))
@@ -1,4 +1,4 @@
1
- import { urlToBasename, urlToFilename, urlToRelativeUrl } from "@jsenv/urls";
1
+ import { urlToBasename, urlToRelativeUrl } from "@jsenv/urls";
2
2
  import {
3
3
  takeDirectorySnapshot,
4
4
  takeFileSnapshot,
@@ -22,9 +22,9 @@ export const snapshotTests = async (
22
22
  {
23
23
  testName = urlToBasename(testFileUrl, true),
24
24
  sideEffectFileUrl,
25
- sideEffectFilePattern = "./output/[test_name].md",
26
- outDirectoryPattern = "./output/[test_name]/",
27
- outFilePattern = "./output/[test_scenario]/[filename]",
25
+ outDirectoryPattern = "./side_effects/",
26
+ sideEffectFilePattern = "./side_effects/[test_basename].md",
27
+ outFilePattern = "./side_effects/[test_name]/[test_scenario]/[filename]",
28
28
  rootDirectoryUrl,
29
29
  generatedBy = true,
30
30
  linkToSource = true,
@@ -35,11 +35,11 @@ export const snapshotTests = async (
35
35
  throwWhenDiff = process.env.CI,
36
36
  } = {},
37
37
  ) => {
38
+ const testBasename = urlToBasename(testFileUrl);
38
39
  if (sideEffectFileUrl === undefined) {
39
- const sideEffectFileRelativeUrl = sideEffectFilePattern.replaceAll(
40
- "[test_name]",
41
- testName,
42
- );
40
+ const sideEffectFileRelativeUrl = sideEffectFilePattern
41
+ .replaceAll("[test_name]", testName)
42
+ .replaceAll("[test_basename]", testBasename);
43
43
  sideEffectFileUrl = new URL(sideEffectFileRelativeUrl, testFileUrl);
44
44
  } else {
45
45
  sideEffectFileUrl = new URL(sideEffectFileUrl, testFileUrl);
@@ -62,7 +62,7 @@ export const snapshotTests = async (
62
62
  filesystemEffects,
63
63
  });
64
64
  let markdown = "";
65
- markdown += `# ${urlToFilename(sideEffectFileUrl)}`;
65
+ markdown += `# ${testName}`;
66
66
  if (generatedBy) {
67
67
  let generatedByLink = renderSmallLink(
68
68
  {
@@ -103,6 +103,7 @@ export const snapshotTests = async (
103
103
  const generateOutFileUrl = (filename) => {
104
104
  const outFileRelativeUrl = outFilePattern
105
105
  .replaceAll("[test_name]", testName)
106
+ .replaceAll("[test_basename]", testBasename)
106
107
  .replaceAll("[test_scenario]", testScenario)
107
108
  .replaceAll("[filename]", filename);
108
109
  const outFileUrl = new URL(outFileRelativeUrl, testFileUrl).href;
package/src/cli.mjs DELETED
@@ -1,54 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import { clearDirectorySync } from "@jsenv/filesystem";
4
- import { pathToFileURL } from "node:url";
5
- import { parseArgs } from "node:util";
6
-
7
- const options = {
8
- help: {
9
- type: "boolean",
10
- },
11
- };
12
- const { values, positionals } = parseArgs({
13
- options,
14
- allowPositionals: true,
15
- });
16
-
17
- if (values.help || positionals.length === 0) {
18
- console.log(`snapshot: Manage snapshot files generated during tests.
19
-
20
- Usage: npx @jsenv/snapshot clear [pattern]
21
-
22
- https://github.com/jsenv/core/tree/main/packages/independent/snapshot
23
-
24
- pattern: files matching this pattern will be removed; can use "*" and "**"
25
- `);
26
- process.exit(0);
27
- }
28
-
29
- const commandHandlers = {
30
- clear: (pattern) => {
31
- const currentDirectoryPath = process.cwd();
32
- const currentDirectoryUrl = pathToFileURL(`${currentDirectoryPath}/`);
33
- console.log(`clear files matching ${pattern} in ${currentDirectoryPath}`);
34
- clearDirectorySync(currentDirectoryUrl, pattern);
35
- },
36
- };
37
-
38
- const [command] = positionals;
39
- const commandHandler = commandHandlers[command];
40
-
41
- if (!commandHandler) {
42
- console.error(`Error: unknown command ${command}.`);
43
- process.exit(1);
44
- }
45
-
46
- if (commandHandler.length) {
47
- const args = positionals.slice(1);
48
- if (args.length === 0) {
49
- console.error(`Error: "${command}" command expect arguments.`);
50
- process.exit(1);
51
- }
52
- }
53
-
54
- await commandHandler(...positionals.slice(1));