@jsenv/snapshot 2.2.3 → 2.2.5

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.3",
3
+ "version": "2.2.5",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -8,7 +8,11 @@ import {
8
8
  writeFileSync,
9
9
  } from "@jsenv/filesystem";
10
10
  import { URL_META } from "@jsenv/url-meta";
11
- import { urlToFilename, urlToRelativeUrl } from "@jsenv/urls";
11
+ import {
12
+ ensurePathnameTrailingSlash,
13
+ urlToFilename,
14
+ urlToRelativeUrl,
15
+ } from "@jsenv/urls";
12
16
  import { CONTENT_TYPE } from "@jsenv/utils/src/content_type/content_type.js";
13
17
  import { readdirSync, readFileSync, statSync } from "node:fs";
14
18
  import { fileURLToPath } from "node:url";
@@ -357,20 +361,21 @@ ${extraUrls.join("\n")}`);
357
361
  }
358
362
  const relativeUrl = urlToRelativeUrl(directoryItemUrl, directoryUrl);
359
363
  if (directoryItemStat.isDirectory()) {
360
- if (!shouldIncludeFile(directoryUrl)) {
361
- continue;
362
- }
363
- if (!shouldVisitDirectory(directoryUrl)) {
364
+ ensurePathnameTrailingSlash(directoryItemUrl);
365
+ if (!shouldVisitDirectory(directoryItemUrl)) {
364
366
  continue;
365
367
  }
366
368
  contentSnapshotNaturalOrder[relativeUrl] = createDirectorySnapshot(
367
- new URL(`${directoryItemUrl}/`),
369
+ directoryItemUrl,
368
370
  {
369
371
  shouldVisitDirectory,
370
372
  shouldIncludeFile,
371
373
  clean,
372
374
  },
373
375
  );
376
+ if (clean) {
377
+ removeDirectorySync(directoryItemUrl);
378
+ }
374
379
  continue;
375
380
  }
376
381
  if (!shouldIncludeFile(directoryItemUrl)) {
@@ -16,6 +16,7 @@ export const snapshotFunctionSideEffects = (
16
16
  rootDirectoryUrl = new URL("./", fnFileUrl),
17
17
  captureConsole = true,
18
18
  filesystemEffects,
19
+ filesystemEffectsInline,
19
20
  restoreFilesystem,
20
21
  } = {},
21
22
  ) => {
@@ -50,7 +51,10 @@ export const snapshotFunctionSideEffects = (
50
51
  }
51
52
  writeFileSync(
52
53
  sideEffectFileUrl,
53
- stringifySideEffects(sideEffects, { rootDirectoryUrl }),
54
+ stringifySideEffects(sideEffects, {
55
+ rootDirectoryUrl,
56
+ filesystemEffectsInline,
57
+ }),
54
58
  );
55
59
  sideEffectDirectorySnapshot.compare();
56
60
  };
@@ -89,6 +93,7 @@ export const snapshotFunctionSideEffects = (
89
93
  if (atStartState.found && !nowState.found) {
90
94
  onFileSystemSideEffect({
91
95
  type: `remove file "${relativeUrl}"`,
96
+ value: atStartState.content,
92
97
  });
93
98
  if (restoreFilesystem) {
94
99
  writeFileSync(from, atStartState.content);
@@ -104,10 +109,18 @@ export const snapshotFunctionSideEffects = (
104
109
  atStartState.content !== nowState.content ||
105
110
  atStartState.mtimeMs !== nowState.mtimeMs
106
111
  ) {
107
- writeFileSync(toUrl, nowState.content);
108
- onFileSystemSideEffect({
109
- type: `write file "./fs/${relativeUrl}"`,
110
- });
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
+ }
111
124
  if (restoreFilesystem) {
112
125
  if (atStartState.found) {
113
126
  if (atStartState.content !== nowState.content) {
@@ -152,7 +165,10 @@ export const snapshotFunctionSideEffects = (
152
165
  }
153
166
  };
154
167
 
155
- const stringifySideEffects = (sideEffects, { rootDirectoryUrl }) => {
168
+ const stringifySideEffects = (
169
+ sideEffects,
170
+ { rootDirectoryUrl, filesystemEffectsInline },
171
+ ) => {
156
172
  let string = "";
157
173
  let index = 0;
158
174
  for (const sideEffect of sideEffects) {
@@ -172,6 +188,10 @@ const stringifySideEffects = (sideEffects, { rootDirectoryUrl }) => {
172
188
  sideEffect.type.startsWith("remove file") ||
173
189
  sideEffect.type.startsWith("write file")
174
190
  ) {
191
+ if (filesystemEffectsInline) {
192
+ string += "\n";
193
+ string += value;
194
+ }
175
195
  } else if (sideEffect.type === "throw") {
176
196
  value = replaceFluctuatingValues(value.stack, {
177
197
  stringType: "error",