@jsenv/snapshot 2.1.2 → 2.2.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.1.2",
3
+ "version": "2.2.1",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -36,7 +36,7 @@
36
36
  "dependencies": {
37
37
  "@jsenv/assert": "4.1.5",
38
38
  "@jsenv/ast": "6.2.5",
39
- "@jsenv/filesystem": "4.9.0",
39
+ "@jsenv/filesystem": "4.9.1",
40
40
  "@jsenv/urls": "2.3.1",
41
41
  "@jsenv/utils": "2.1.1",
42
42
  "pixelmatch": "6.0.0",
@@ -0,0 +1,138 @@
1
+ import {
2
+ moveEntrySync,
3
+ writeDirectorySync,
4
+ writeFileSync,
5
+ } from "@jsenv/filesystem";
6
+ import { urlToRelativeUrl } from "@jsenv/urls/src/url_to_relative_url.js";
7
+ import { takeDirectorySnapshot } from "./filesystem_snapshot.js";
8
+ import { replaceFluctuatingValues } from "./replace_fluctuating_values.js";
9
+
10
+ export const snapshotFunctionSideEffects = (
11
+ fn,
12
+ fnFileUrl,
13
+ {
14
+ sideEffectDirectoryName = "output",
15
+ rootDirectoryUrl = new URL("./", fnFileUrl),
16
+ captureConsole = true,
17
+ filesystemEffects,
18
+ } = {},
19
+ ) => {
20
+ const sideEffectDirectoryUrl = new URL(sideEffectDirectoryName, fnFileUrl);
21
+ writeDirectorySync(sideEffectDirectoryUrl, { allowUseless: true });
22
+ const sideEffectDirectorySnapshot = takeDirectorySnapshot(
23
+ sideEffectDirectoryUrl,
24
+ );
25
+ const finallyCallbackSet = new Set();
26
+ const errorFileUrl = new URL("./error.txt", sideEffectDirectoryUrl);
27
+ const resultFileUrl = new URL("./result.json", sideEffectDirectoryUrl);
28
+ const onError = (e) => {
29
+ writeFileSync(
30
+ errorFileUrl,
31
+ replaceFluctuatingValues(e.stack, {
32
+ fileUrl: errorFileUrl,
33
+ }),
34
+ );
35
+ };
36
+ const onResult = (result) => {
37
+ if (result === undefined) {
38
+ return;
39
+ }
40
+ writeFileSync(
41
+ resultFileUrl,
42
+ replaceFluctuatingValues(JSON.stringify(result, null, " "), {
43
+ fileUrl: resultFileUrl,
44
+ rootDirectoryUrl,
45
+ }),
46
+ );
47
+ };
48
+ const onFinally = () => {
49
+ for (const finallyCallback of finallyCallbackSet) {
50
+ finallyCallback();
51
+ }
52
+ sideEffectDirectorySnapshot.compare();
53
+ };
54
+ if (captureConsole) {
55
+ const installConsoleSpy = (methodName, consoleOutputFileUrl) => {
56
+ const methodSpied = console[methodName];
57
+ let output = "";
58
+ console[methodName] = (message) => {
59
+ if (output) {
60
+ output += "\n";
61
+ }
62
+ output += message;
63
+ };
64
+ finallyCallbackSet.add(() => {
65
+ console[methodName] = methodSpied;
66
+ if (output) {
67
+ writeFileSync(
68
+ consoleOutputFileUrl,
69
+ replaceFluctuatingValues(output, {
70
+ fileUrl: consoleOutputFileUrl,
71
+ rootDirectoryUrl,
72
+ }),
73
+ );
74
+ }
75
+ });
76
+ };
77
+ installConsoleSpy(
78
+ "error",
79
+ new URL("./console_errors.txt", sideEffectDirectoryUrl),
80
+ );
81
+ installConsoleSpy(
82
+ "warn",
83
+ new URL("./console_warnings.txt", sideEffectDirectoryUrl),
84
+ );
85
+ installConsoleSpy(
86
+ "info",
87
+ new URL("./console_infos.txt", sideEffectDirectoryUrl),
88
+ );
89
+ installConsoleSpy(
90
+ "log",
91
+ new URL("./console_logs.txt", sideEffectDirectoryUrl),
92
+ );
93
+ }
94
+ if (filesystemEffects) {
95
+ const filesystemEffectDirectoryUrl = new URL(
96
+ "./fs/",
97
+ sideEffectDirectoryUrl,
98
+ );
99
+ for (const filesystemEffect of filesystemEffects) {
100
+ finallyCallbackSet.add(() => {
101
+ const from = new URL(filesystemEffect, fnFileUrl);
102
+ const relativeUrl = urlToRelativeUrl(from, fnFileUrl);
103
+ moveEntrySync({
104
+ from,
105
+ to: new URL(relativeUrl, filesystemEffectDirectoryUrl),
106
+ noEntryEffect: "none",
107
+ overwrite: true,
108
+ });
109
+ });
110
+ }
111
+ }
112
+ let returnedPromise = false;
113
+ try {
114
+ const returnValue = fn();
115
+ if (returnValue && returnValue.then) {
116
+ returnedPromise = true;
117
+ returnValue.then(
118
+ (value) => {
119
+ onResult(value);
120
+ onFinally();
121
+ },
122
+ (e) => {
123
+ onError(e);
124
+ onFinally();
125
+ },
126
+ );
127
+ } else {
128
+ onResult(returnValue);
129
+ }
130
+ } catch (e) {
131
+ onError(e);
132
+ } finally {
133
+ if (returnedPromise) {
134
+ return;
135
+ }
136
+ onFinally();
137
+ }
138
+ };
package/src/main.js CHANGED
@@ -2,4 +2,5 @@ export {
2
2
  takeDirectorySnapshot,
3
3
  takeFileSnapshot,
4
4
  } from "./filesystem_snapshot.js";
5
+ export { snapshotFunctionSideEffects } from "./function_snapshot.js";
5
6
  export { replaceFluctuatingValues } from "./replace_fluctuating_values.js";