@jsenv/snapshot 2.6.8 → 2.6.10

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.6.8",
3
+ "version": "2.6.10",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -35,11 +35,11 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@jsenv/assert": "4.1.13",
38
- "@jsenv/ast": "6.2.11",
38
+ "@jsenv/ast": "6.2.12",
39
39
  "@jsenv/exception": "1.0.1",
40
- "@jsenv/filesystem": "4.9.6",
40
+ "@jsenv/filesystem": "4.9.7",
41
41
  "@jsenv/terminal-recorder": "1.4.2",
42
- "@jsenv/urls": "2.5.0",
42
+ "@jsenv/urls": "2.5.1",
43
43
  "@jsenv/utils": "2.1.2",
44
44
  "ansi-regex": "6.0.1",
45
45
  "pixelmatch": "6.0.0",
@@ -170,27 +170,35 @@ export const takeDirectorySnapshot = (
170
170
  directoryUrl = new URL(directoryUrl);
171
171
  const associations = URL_META.resolveAssociations(
172
172
  {
173
- included: pattern,
173
+ action: pattern,
174
174
  },
175
175
  directoryUrl,
176
176
  );
177
- const predicate = ({ included }) => included;
178
- const shouldVisitDirectory = (url) =>
179
- URL_META.urlChildMayMatch({
177
+ const shouldVisitDirectory = (url) => {
178
+ return URL_META.urlChildMayMatch({
180
179
  url,
181
180
  associations,
182
- predicate,
181
+ predicate: (meta) => Boolean(meta.action),
183
182
  });
184
- const shouldIncludeFile = (url) =>
185
- predicate(
186
- URL_META.applyAssociations({
187
- url,
188
- associations,
189
- }),
190
- );
183
+ };
184
+ const shouldIncludeFile = (url) => {
185
+ const meta = URL_META.applyAssociations({
186
+ url,
187
+ associations,
188
+ });
189
+ return meta.action === true || meta.action === "presence_only";
190
+ };
191
+ const shouldCompareFile = (url) => {
192
+ const meta = URL_META.applyAssociations({
193
+ url,
194
+ associations,
195
+ });
196
+ return meta.action === true || meta.action === "compare";
197
+ };
191
198
  const directorySnapshot = createDirectorySnapshot(directoryUrl, {
192
199
  shouldVisitDirectory,
193
200
  shouldIncludeFile,
201
+ shouldCompareFile,
194
202
  clean: true,
195
203
  });
196
204
  return {
@@ -199,6 +207,7 @@ export const takeDirectorySnapshot = (
199
207
  const nextDirectorySnapshot = createDirectorySnapshot(directoryUrl, {
200
208
  shouldVisitDirectory,
201
209
  shouldIncludeFile,
210
+ shouldCompareFile,
202
211
  });
203
212
  directorySnapshot.compare(nextDirectorySnapshot, { throwWhenDiff });
204
213
  },
@@ -226,7 +235,7 @@ export const takeDirectorySnapshot = (
226
235
  };
227
236
  const createDirectorySnapshot = (
228
237
  directoryUrl,
229
- { shouldVisitDirectory, shouldIncludeFile, clean },
238
+ { shouldVisitDirectory, shouldIncludeFile, shouldCompareFile, clean },
230
239
  ) => {
231
240
  const directorySnapshot = {
232
241
  type: "directory",
@@ -311,6 +320,9 @@ ${extraUrls.join("\n")}`);
311
320
  // content
312
321
  {
313
322
  for (const relativeUrl of relativeUrls) {
323
+ if (!shouldCompareFile(new URL(relativeUrl, directoryUrl))) {
324
+ continue;
325
+ }
314
326
  const snapshot = directoryContentSnapshot[relativeUrl];
315
327
  const nextSnapshot = nextDirectoryContentSnapshot[relativeUrl];
316
328
  if (nextSnapshot) {
@@ -370,6 +382,7 @@ ${extraUrls.join("\n")}`);
370
382
  {
371
383
  shouldVisitDirectory,
372
384
  shouldIncludeFile,
385
+ shouldCompareFile,
373
386
  clean,
374
387
  },
375
388
  );
@@ -9,6 +9,9 @@ export const getCallerLocation = (callIndex = 1) => {
9
9
  const { stack } = new Error();
10
10
  Error.prepareStackTrace = prepareStackTrace;
11
11
  const callerCallsite = stack[callIndex];
12
+ if (!callerCallsite) {
13
+ return null;
14
+ }
12
15
  const fileName = callerCallsite.getFileName();
13
16
  const testCallSite = {
14
17
  url:
@@ -7,10 +7,27 @@ import { createCaptureSideEffects } from "./create_capture_side_effects.js";
7
7
  import { renderSideEffects } from "./render_side_effects.js";
8
8
 
9
9
  export const snapshotSideEffects = (
10
+ sourceFileUrl,
10
11
  fn,
11
- sideEffectFileUrl,
12
- { outDirectoryUrl, errorStackHidden, ...captureOptions } = {},
12
+ {
13
+ sideEffectFileUrl,
14
+ sideEffectFilePattern = "./output/<basename>.md",
15
+ outDirectoryUrl,
16
+ errorStackHidden,
17
+ ...captureOptions
18
+ } = {},
13
19
  ) => {
20
+ if (sideEffectFileUrl === undefined) {
21
+ const basename = urlToBasename(sourceFileUrl, true);
22
+ const sideEffectFileRelativeUrl = sideEffectFilePattern.replaceAll(
23
+ "<basename>",
24
+ basename,
25
+ );
26
+ sideEffectFileUrl = new URL(sideEffectFileRelativeUrl, sourceFileUrl);
27
+ } else {
28
+ sideEffectFileUrl = new URL(sideEffectFileUrl, sourceFileUrl);
29
+ }
30
+
14
31
  const captureSideEffects = createCaptureSideEffects(captureOptions);
15
32
  if (outDirectoryUrl === undefined) {
16
33
  outDirectoryUrl = new URL(
@@ -1,4 +1,4 @@
1
- import { urlToFilename, urlToRelativeUrl } from "@jsenv/urls";
1
+ import { urlToBasename, urlToFilename, urlToRelativeUrl } from "@jsenv/urls";
2
2
  import {
3
3
  takeDirectorySnapshot,
4
4
  takeFileSnapshot,
@@ -9,20 +9,22 @@ import { renderSideEffects, renderSmallLink } from "./render_side_effects.js";
9
9
 
10
10
  /**
11
11
  * Generate a markdown file describing all test side effects. When executed in CI throw if there is a diff.
12
+ * @param {URL} sourceFileUrl
12
13
  * @param {Function} fnRegisteringTest
13
- * @param {URL} snapshotFileUrl
14
14
  * @param {Object} snapshotTestsOptions
15
- * @param {string|url} snapshotTestsOptions.sourceDirectoryUrl
15
+ * @param {string|url} snapshotTestsOptions.sideEffectFileUrl
16
+ * @param {string|url} snapshotTestsOptions.rootDirectoryUrl
16
17
  * @return {Array.<Object>} sideEffects
17
18
  */
18
19
  export const snapshotTests = async (
20
+ sourceFileUrl,
19
21
  fnRegisteringTest,
20
- snapshotFileUrl,
21
22
  {
23
+ sideEffectFileUrl,
24
+ sideEffectFilePattern = "./<basename>/<basename>.md",
22
25
  rootDirectoryUrl,
23
26
  generatedBy = true,
24
27
  linkToSource = true,
25
- sourceFileUrl,
26
28
  linkToEachSource,
27
29
  errorStackHidden,
28
30
  logEffects,
@@ -30,6 +32,17 @@ export const snapshotTests = async (
30
32
  throwWhenDiff = process.env.CI,
31
33
  } = {},
32
34
  ) => {
35
+ if (sideEffectFileUrl === undefined) {
36
+ const basename = urlToBasename(sourceFileUrl, true);
37
+ const sideEffectFileRelativeUrl = sideEffectFilePattern.replaceAll(
38
+ "<basename>",
39
+ basename,
40
+ );
41
+ sideEffectFileUrl = new URL(sideEffectFileRelativeUrl, sourceFileUrl);
42
+ } else {
43
+ sideEffectFileUrl = new URL(sideEffectFileUrl, sourceFileUrl);
44
+ }
45
+
33
46
  const testMap = new Map();
34
47
  const onlyTestMap = new Map();
35
48
  const test = (scenario, fn, options) => {
@@ -47,7 +60,7 @@ export const snapshotTests = async (
47
60
  filesystemEffects,
48
61
  });
49
62
  let markdown = "";
50
- markdown += `# ${urlToFilename(snapshotFileUrl)}`;
63
+ markdown += `# ${urlToFilename(sideEffectFileUrl)}`;
51
64
  if (generatedBy) {
52
65
  let generatedByLink = renderSmallLink(
53
66
  {
@@ -58,7 +71,7 @@ export const snapshotTests = async (
58
71
  prefix: "Generated by ",
59
72
  suffix:
60
73
  linkToSource && sourceFileUrl
61
- ? generateExecutingLink(sourceFileUrl, snapshotFileUrl)
74
+ ? generateExecutingLink(sourceFileUrl, sideEffectFileUrl)
62
75
  : "",
63
76
  },
64
77
  );
@@ -76,11 +89,16 @@ export const snapshotTests = async (
76
89
  });
77
90
  const outDirectoryUrl = new URL(
78
91
  `./${asValidFilename(scenario)}/`,
79
- snapshotFileUrl,
92
+ sideEffectFileUrl,
80
93
  );
81
- const outDirectorySnapshot = takeDirectorySnapshot(outDirectoryUrl);
94
+ const outDirectorySnapshot = takeDirectorySnapshot(outDirectoryUrl, {
95
+ pattern: {
96
+ "**/*": true,
97
+ "**/*.svg": "presence_only",
98
+ },
99
+ });
82
100
  const sideEffectsMarkdown = renderSideEffects(sideEffects, {
83
- sideEffectFileUrl: snapshotFileUrl,
101
+ sideEffectFileUrl,
84
102
  outDirectoryUrl,
85
103
  generatedBy: false,
86
104
  titleLevel: 3,
@@ -89,15 +107,15 @@ export const snapshotTests = async (
89
107
  outDirectorySnapshot.compare(throwWhenDiff);
90
108
  markdown += sideEffectsMarkdown;
91
109
  }
92
- const sideEffectFileSnapshot = takeFileSnapshot(snapshotFileUrl);
110
+ const sideEffectFileSnapshot = takeFileSnapshot(sideEffectFileUrl);
93
111
  sideEffectFileSnapshot.update(markdown, {
94
112
  mockFluctuatingValues: false,
95
113
  throwWhenDiff,
96
114
  });
97
115
  };
98
116
 
99
- const generateExecutingLink = (sourceFileUrl, snapshotFileUrl) => {
100
- const relativeUrl = urlToRelativeUrl(sourceFileUrl, snapshotFileUrl, {
117
+ const generateExecutingLink = (sourceFileUrl, sideEffectFileUrl) => {
118
+ const relativeUrl = urlToRelativeUrl(sourceFileUrl, sideEffectFileUrl, {
101
119
  preferRelativeNotation: true,
102
120
  });
103
121
  const href = `${relativeUrl}`;