@jsenv/snapshot 2.2.2 → 2.2.4

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.2.2",
3
+ "version": "2.2.4",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -1,20 +1,36 @@
1
- import { readEntryStatSync, readFileSync } from "@jsenv/filesystem";
1
+ import {
2
+ readEntryStatSync,
3
+ readFileSync,
4
+ removeFileSync,
5
+ writeFileSync,
6
+ } from "@jsenv/filesystem";
2
7
  import { urlToRelativeUrl } from "@jsenv/urls/src/url_to_relative_url.js";
3
- import { takeFileSnapshot } from "./filesystem_snapshot.js";
8
+ import { takeDirectorySnapshot } from "./filesystem_snapshot.js";
4
9
  import { replaceFluctuatingValues } from "./replace_fluctuating_values.js";
5
10
 
6
11
  export const snapshotFunctionSideEffects = (
7
12
  fn,
8
13
  fnFileUrl,
9
- sideEffectFileRelativeUrl,
14
+ sideEffectDirectoryRelativeUrl,
10
15
  {
11
16
  rootDirectoryUrl = new URL("./", fnFileUrl),
12
17
  captureConsole = true,
13
18
  filesystemEffects,
19
+ filesystemEffectsInline,
20
+ restoreFilesystem,
14
21
  } = {},
15
22
  ) => {
16
- const sideEffectFileUrl = new URL(sideEffectFileRelativeUrl, fnFileUrl);
17
- const sideEffectFileSnapshot = takeFileSnapshot(sideEffectFileUrl);
23
+ const sideEffectDirectoryUrl = new URL(
24
+ sideEffectDirectoryRelativeUrl,
25
+ fnFileUrl,
26
+ );
27
+ const sideEffectDirectorySnapshot = takeDirectorySnapshot(
28
+ sideEffectDirectoryUrl,
29
+ );
30
+ const sideEffectFileUrl = new URL(
31
+ "./side_effect.txt",
32
+ sideEffectDirectoryUrl,
33
+ );
18
34
  const sideEffects = [];
19
35
  const finallyCallbackSet = new Set();
20
36
  const onError = (e) => {
@@ -33,10 +49,14 @@ export const snapshotFunctionSideEffects = (
33
49
  for (const finallyCallback of finallyCallbackSet) {
34
50
  finallyCallback();
35
51
  }
36
- sideEffectFileSnapshot.update(
37
- stringifySideEffects(sideEffects, { rootDirectoryUrl }),
52
+ writeFileSync(
53
+ sideEffectFileUrl,
54
+ stringifySideEffects(sideEffects, {
55
+ rootDirectoryUrl,
56
+ filesystemEffectsInline,
57
+ }),
38
58
  );
39
- sideEffectFileSnapshot.compare();
59
+ sideEffectDirectorySnapshot.compare();
40
60
  };
41
61
  if (captureConsole) {
42
62
  const installConsoleSpy = (methodName) => {
@@ -57,9 +77,11 @@ export const snapshotFunctionSideEffects = (
57
77
  installConsoleSpy("log");
58
78
  }
59
79
  if (filesystemEffects) {
80
+ const fsSideEffectDirectoryUrl = new URL("./fs/", sideEffectDirectoryUrl);
60
81
  for (const filesystemEffect of filesystemEffects) {
61
82
  const from = new URL(filesystemEffect, fnFileUrl);
62
83
  const relativeUrl = urlToRelativeUrl(from, fnFileUrl);
84
+ const toUrl = new URL(relativeUrl, fsSideEffectDirectoryUrl);
63
85
  const atStartState = getFileState(from);
64
86
  const onFileSystemSideEffect = (fsSideEffect) => {
65
87
  const last = sideEffects.pop();
@@ -73,6 +95,9 @@ export const snapshotFunctionSideEffects = (
73
95
  type: `remove file "${relativeUrl}"`,
74
96
  value: atStartState.content,
75
97
  });
98
+ if (restoreFilesystem) {
99
+ writeFileSync(from, atStartState.content);
100
+ }
76
101
  return;
77
102
  }
78
103
  // we use same type because we don't want to differentiate between
@@ -84,10 +109,27 @@ export const snapshotFunctionSideEffects = (
84
109
  atStartState.content !== nowState.content ||
85
110
  atStartState.mtimeMs !== nowState.mtimeMs
86
111
  ) {
87
- onFileSystemSideEffect({
88
- type: `write file "${relativeUrl}"`,
89
- value: nowState.content,
90
- });
112
+ if (filesystemEffectsInline) {
113
+ onFileSystemSideEffect({
114
+ type: `write file "${relativeUrl}"`,
115
+ value: nowState.content,
116
+ });
117
+ } else {
118
+ writeFileSync(toUrl, nowState.content);
119
+ onFileSystemSideEffect({
120
+ type: `write file "${relativeUrl}" (see ./fs/${relativeUrl})`,
121
+ value: nowState.content,
122
+ });
123
+ }
124
+ if (restoreFilesystem) {
125
+ if (atStartState.found) {
126
+ if (atStartState.content !== nowState.content) {
127
+ writeFileSync(from, atStartState.content);
128
+ }
129
+ } else {
130
+ removeFileSync(from);
131
+ }
132
+ }
91
133
  return;
92
134
  }
93
135
  // file is exactly the same
@@ -123,7 +165,10 @@ export const snapshotFunctionSideEffects = (
123
165
  }
124
166
  };
125
167
 
126
- const stringifySideEffects = (sideEffects, { rootDirectoryUrl }) => {
168
+ const stringifySideEffects = (
169
+ sideEffects,
170
+ { rootDirectoryUrl, filesystemEffectsInline },
171
+ ) => {
127
172
  let string = "";
128
173
  let index = 0;
129
174
  for (const sideEffect of sideEffects) {
@@ -131,12 +176,28 @@ const stringifySideEffects = (sideEffects, { rootDirectoryUrl }) => {
131
176
  string += "\n\n";
132
177
  }
133
178
  string += `${index + 1}. ${sideEffect.type}`;
134
- string += "\n";
135
179
  let value = sideEffect.value;
136
- if (sideEffect.type === "throw") {
180
+ if (sideEffect.type.startsWith("console.")) {
181
+ value = replaceFluctuatingValues(value, {
182
+ stringType: "console",
183
+ rootDirectoryUrl,
184
+ });
185
+ string += "\n";
186
+ string += value;
187
+ } else if (
188
+ sideEffect.type.startsWith("remove file") ||
189
+ sideEffect.type.startsWith("write file")
190
+ ) {
191
+ if (filesystemEffectsInline) {
192
+ string += "\n";
193
+ string += value;
194
+ }
195
+ } else if (sideEffect.type === "throw") {
137
196
  value = replaceFluctuatingValues(value.stack, {
138
197
  stringType: "error",
139
198
  });
199
+ string += "\n";
200
+ string += value;
140
201
  } else if (sideEffect.type === "return") {
141
202
  value =
142
203
  value === undefined
@@ -145,13 +206,12 @@ const stringifySideEffects = (sideEffects, { rootDirectoryUrl }) => {
145
206
  stringType: "json",
146
207
  rootDirectoryUrl,
147
208
  });
148
- } else if (sideEffect.type.startsWith("console.")) {
149
- value = replaceFluctuatingValues(value, {
150
- stringType: "console",
151
- rootDirectoryUrl,
152
- });
209
+ string += "\n";
210
+ string += value;
211
+ } else {
212
+ string += "\n";
213
+ string += value;
153
214
  }
154
- string += value;
155
215
  index++;
156
216
  }
157
217
  return string;