@jsenv/snapshot 2.3.0 → 2.3.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,17 +1,16 @@
|
|
|
1
1
|
import { createException } from "@jsenv/exception";
|
|
2
2
|
import { replaceFluctuatingValues } from "../replace_fluctuating_values.js";
|
|
3
|
+
import { wrapIntoMarkdownBlock } from "./function_side_effects_renderer.js";
|
|
3
4
|
|
|
4
5
|
const RETURN_PROMISE = {};
|
|
5
6
|
|
|
6
|
-
let
|
|
7
|
+
let functionExecutingCount = 0;
|
|
8
|
+
|
|
7
9
|
export const collectFunctionSideEffects = (
|
|
8
10
|
fn,
|
|
9
11
|
sideEffectDetectors,
|
|
10
12
|
{ rootDirectoryUrl },
|
|
11
13
|
) => {
|
|
12
|
-
if (executing) {
|
|
13
|
-
throw new Error("collectFunctionSideEffects already running");
|
|
14
|
-
}
|
|
15
14
|
const sideEffects = [];
|
|
16
15
|
const addSideEffect = (sideEffect) => {
|
|
17
16
|
sideEffects.push(sideEffect);
|
|
@@ -23,14 +22,32 @@ export const collectFunctionSideEffects = (
|
|
|
23
22
|
uninstall();
|
|
24
23
|
});
|
|
25
24
|
}
|
|
25
|
+
if (functionExecutingCount) {
|
|
26
|
+
// The reason for this warning:
|
|
27
|
+
// 1. fs side effect detectors is not yet fully compatible with that because
|
|
28
|
+
// callback.oncomplete redefinition might be wrong for open, mkdir etc
|
|
29
|
+
// (at least this is to be tested)
|
|
30
|
+
// 2. It's usually a sign code forgets to put await in front of
|
|
31
|
+
// collectFunctionSideEffects or snapshotFunctionSideEffects
|
|
32
|
+
// 3. collectFunctionSideEffects is meant to collect a function side effect
|
|
33
|
+
// during unit test. So in unit test the function being tested should be analyized
|
|
34
|
+
// and should not in turn analyze an other one
|
|
35
|
+
console.warn(
|
|
36
|
+
`collectFunctionSideEffects called while other function(s) side effects are collected`,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
functionExecutingCount++;
|
|
40
|
+
|
|
26
41
|
const onCatch = (valueThrow) => {
|
|
27
42
|
sideEffects.push({
|
|
28
43
|
type: "throw",
|
|
29
44
|
value: valueThrow,
|
|
30
45
|
label: "throw",
|
|
31
|
-
text:
|
|
32
|
-
|
|
33
|
-
|
|
46
|
+
text: wrapIntoMarkdownBlock(
|
|
47
|
+
renderValueThrownOrRejected(
|
|
48
|
+
createException(valueThrow, { rootDirectoryUrl }),
|
|
49
|
+
{ rootDirectoryUrl },
|
|
50
|
+
),
|
|
34
51
|
),
|
|
35
52
|
});
|
|
36
53
|
};
|
|
@@ -47,9 +64,12 @@ export const collectFunctionSideEffects = (
|
|
|
47
64
|
type: "return",
|
|
48
65
|
value: valueReturned,
|
|
49
66
|
label: "return",
|
|
50
|
-
text:
|
|
51
|
-
|
|
52
|
-
|
|
67
|
+
text: wrapIntoMarkdownBlock(
|
|
68
|
+
renderReturnValueOrResolveValue(valueReturned, {
|
|
69
|
+
rootDirectoryUrl,
|
|
70
|
+
}),
|
|
71
|
+
"js",
|
|
72
|
+
),
|
|
53
73
|
});
|
|
54
74
|
}
|
|
55
75
|
};
|
|
@@ -58,7 +78,10 @@ export const collectFunctionSideEffects = (
|
|
|
58
78
|
type: "resolve",
|
|
59
79
|
value,
|
|
60
80
|
label: "resolve",
|
|
61
|
-
text:
|
|
81
|
+
text: wrapIntoMarkdownBlock(
|
|
82
|
+
renderReturnValueOrResolveValue(value, { rootDirectoryUrl }),
|
|
83
|
+
"js",
|
|
84
|
+
),
|
|
62
85
|
});
|
|
63
86
|
};
|
|
64
87
|
const onReject = (reason) => {
|
|
@@ -66,14 +89,16 @@ export const collectFunctionSideEffects = (
|
|
|
66
89
|
type: "reject",
|
|
67
90
|
value: reason,
|
|
68
91
|
label: "reject",
|
|
69
|
-
text:
|
|
70
|
-
|
|
71
|
-
|
|
92
|
+
text: wrapIntoMarkdownBlock(
|
|
93
|
+
renderValueThrownOrRejected(
|
|
94
|
+
createException(reason, { rootDirectoryUrl }),
|
|
95
|
+
{ rootDirectoryUrl },
|
|
96
|
+
),
|
|
72
97
|
),
|
|
73
98
|
});
|
|
74
99
|
};
|
|
75
100
|
const onFinally = () => {
|
|
76
|
-
|
|
101
|
+
functionExecutingCount--;
|
|
77
102
|
for (const finallyCallback of finallyCallbackSet) {
|
|
78
103
|
finallyCallback();
|
|
79
104
|
}
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { writeFileSync } from "@jsenv/filesystem";
|
|
2
2
|
import {
|
|
3
3
|
ensurePathnameTrailingSlash,
|
|
4
|
+
urlToExtension,
|
|
4
5
|
urlToFilename,
|
|
5
6
|
urlToRelativeUrl,
|
|
6
7
|
} from "@jsenv/urls";
|
|
7
8
|
import { takeDirectorySnapshot } from "../filesystem_snapshot.js";
|
|
8
9
|
import { replaceFluctuatingValues } from "../replace_fluctuating_values.js";
|
|
9
10
|
import { collectFunctionSideEffects } from "./function_side_effects_collector.js";
|
|
10
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
renderSideEffects,
|
|
13
|
+
wrapIntoMarkdownBlock,
|
|
14
|
+
} from "./function_side_effects_renderer.js";
|
|
11
15
|
import { spyConsoleCalls } from "./spy_console_calls.js";
|
|
12
16
|
import { spyFilesystemCalls } from "./spy_filesystem_calls.js";
|
|
13
17
|
|
|
14
|
-
let filesystemSideEffectInstalled;
|
|
15
|
-
|
|
16
18
|
export const snapshotFunctionSideEffects = (
|
|
17
19
|
fn,
|
|
18
20
|
fnFileUrl,
|
|
@@ -34,7 +36,7 @@ export const snapshotFunctionSideEffects = (
|
|
|
34
36
|
const sideEffectDirectorySnapshot = takeDirectorySnapshot(
|
|
35
37
|
sideEffectDirectoryUrl,
|
|
36
38
|
);
|
|
37
|
-
const sideEffectFilename = `${urlToFilename(sideEffectDirectoryUrl)}_side_effects.
|
|
39
|
+
const sideEffectFilename = `${urlToFilename(sideEffectDirectoryUrl)}_side_effects.md`;
|
|
38
40
|
const sideEffectFileUrl = new URL(sideEffectFilename, sideEffectDirectoryUrl);
|
|
39
41
|
const callbackSet = new Set();
|
|
40
42
|
const sideEffectDetectors = [
|
|
@@ -46,10 +48,13 @@ export const snapshotFunctionSideEffects = (
|
|
|
46
48
|
type: `console:${methodName}`,
|
|
47
49
|
value: message,
|
|
48
50
|
label: `console.${methodName}`,
|
|
49
|
-
text:
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
text: wrapIntoMarkdownBlock(
|
|
52
|
+
replaceFluctuatingValues(message, {
|
|
53
|
+
stringType: "console",
|
|
54
|
+
rootDirectoryUrl,
|
|
55
|
+
}),
|
|
56
|
+
"console",
|
|
57
|
+
),
|
|
53
58
|
});
|
|
54
59
|
};
|
|
55
60
|
const consoleSpy = spyConsoleCalls(
|
|
@@ -79,12 +84,6 @@ export const snapshotFunctionSideEffects = (
|
|
|
79
84
|
{
|
|
80
85
|
name: "filesystem",
|
|
81
86
|
install: (addSideEffect) => {
|
|
82
|
-
if (filesystemSideEffectInstalled) {
|
|
83
|
-
throw new Error(
|
|
84
|
-
"cannot collect filesystem side effects concurrently (already collecting side effect for an other function)",
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
filesystemSideEffectInstalled = true;
|
|
88
87
|
const fsSideEffectDirectoryUrl = ensurePathnameTrailingSlash(
|
|
89
88
|
new URL(filesystemEffectsDirectory, sideEffectDirectoryUrl),
|
|
90
89
|
);
|
|
@@ -112,7 +111,10 @@ export const snapshotFunctionSideEffects = (
|
|
|
112
111
|
type: "fs:write_file",
|
|
113
112
|
value: { relativeUrl, content },
|
|
114
113
|
label: `write file "${relativeUrl}"`,
|
|
115
|
-
text:
|
|
114
|
+
text: wrapIntoMarkdownBlock(
|
|
115
|
+
content,
|
|
116
|
+
urlToExtension(url).slice(1),
|
|
117
|
+
),
|
|
116
118
|
});
|
|
117
119
|
}
|
|
118
120
|
},
|
|
@@ -132,7 +134,6 @@ export const snapshotFunctionSideEffects = (
|
|
|
132
134
|
);
|
|
133
135
|
return () => {
|
|
134
136
|
filesystemSpy.restore();
|
|
135
|
-
filesystemSideEffectInstalled = false;
|
|
136
137
|
};
|
|
137
138
|
},
|
|
138
139
|
},
|