@jsenv/snapshot 2.8.5 → 2.8.7

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.8.5",
3
+ "version": "2.8.7",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@jsenv/assert": "4.1.15",
37
- "@jsenv/ast": "6.2.14",
37
+ "@jsenv/ast": "6.2.15",
38
38
  "@jsenv/exception": "1.0.2",
39
39
  "@jsenv/filesystem": "4.9.10",
40
40
  "@jsenv/terminal-recorder": "1.4.4",
@@ -58,7 +58,6 @@ export const createWellKnown = (name, replacement = name) => {
58
58
 
59
59
  export const createReplaceFilesystemWellKnownValues = ({
60
60
  rootDirectoryUrl,
61
- localhostUrl,
62
61
  // for unit tests
63
62
  isWindows = process.platform === "win32",
64
63
  ancestorPackagesDisabled,
@@ -127,32 +126,9 @@ export const createReplaceFilesystemWellKnownValues = ({
127
126
  }
128
127
  };
129
128
  };
130
- const addWellKnownUrl = (url, replacement) => {
131
- const wellKnownUrl = {
132
- url,
133
- replace: (string) => {
134
- return string.replaceAll(url, replacement);
135
- },
136
- };
137
- wellKownUrlArray.push(wellKnownUrl);
138
- return () => {
139
- const urlIndex = wellKownUrlArray.indexOf(wellKnownUrl);
140
- if (urlIndex > -1) {
141
- wellKownUrlArray.splice(urlIndex, 1);
142
- }
143
- };
144
- };
145
129
  if (rootDirectoryUrl) {
146
130
  addWellKnownFileUrl(rootDirectoryUrl, WELL_KNOWN_ROOT);
147
131
  }
148
- if (localhostUrl) {
149
- addWellKnownUrl(
150
- localhostUrl,
151
- localhostUrl.startsWith("https")
152
- ? "https://localhost"
153
- : "http://localhost",
154
- );
155
- }
156
132
  /*
157
133
  * When running code inside a node project ancestor packages
158
134
  * should make things super predictible because
@@ -21,13 +21,11 @@ export const replaceFluctuatingValues = (
21
21
  {
22
22
  stringType,
23
23
  rootDirectoryUrl,
24
- localhostUrl,
25
24
  fileUrl,
26
25
  preserveAnsi,
27
26
  // for unit test
28
27
  replaceFilesystemWellKnownValues = createReplaceFilesystemWellKnownValues({
29
28
  rootDirectoryUrl,
30
- localhostUrl,
31
29
  }),
32
30
  } = {},
33
31
  ) => {
@@ -191,6 +189,12 @@ const replaceInObject = (object, { replace }) => {
191
189
  };
192
190
 
193
191
  const replaceHttpUrls = (source) => {
192
+ source = source.replace(/(https?):\/\/localhost:\d+/, (match, protocol) => {
193
+ return `${protocol}://localhost`;
194
+ });
195
+ source = source.replace(/(https?):\/\/\[::1\]:\d+/, (match, protocol) => {
196
+ return `${protocol}://[::1]`;
197
+ });
194
198
  return source;
195
199
  // return source.replace(/(?:https?|ftp):\/\/\S+[\w/]/g, (match) => {
196
200
  // const lastChar = match[match.length - 1];
@@ -5,6 +5,7 @@ import { groupLogSideEffects } from "./group_log_side_effects.js";
5
5
  const logSideEffectsOptionsDefault = {
6
6
  prevent: true,
7
7
  group: true,
8
+ ignore: false,
8
9
  };
9
10
 
10
11
  export const logSideEffects = (logSideEffectsOptions) => {
@@ -15,7 +16,7 @@ export const logSideEffects = (logSideEffectsOptions) => {
15
16
  return {
16
17
  name: "console",
17
18
  install: (addSideEffect, { addFinallyCallback }) => {
18
- const { prevent, group } = logSideEffectsOptions;
19
+ const { prevent, group, ignore } = logSideEffectsOptions;
19
20
  if (group) {
20
21
  addFinallyCallback((sideEffects) => {
21
22
  groupLogSideEffects(sideEffects, {
@@ -60,6 +61,9 @@ export const logSideEffects = (logSideEffectsOptions) => {
60
61
  });
61
62
  }
62
63
  const addLogSideEffect = (type, message) => {
64
+ if (ignore) {
65
+ return;
66
+ }
63
67
  addSideEffect({
64
68
  code: type,
65
69
  type,
@@ -12,6 +12,9 @@ import { renderSideEffects, renderSmallLink } from "./render_side_effects.js";
12
12
  * @param {Object} snapshotTestsOptions
13
13
  * @param {string|url} snapshotTestsOptions.outFilePattern
14
14
  * @param {string|url} snapshotTestsOptions.rootDirectoryUrl
15
+ * @param {Object} [snapshotTestsOptions.filesystemActions]
16
+ * Control what to do when there is a file side effect
17
+ * "compare", "compare_presence_only", "undo", "ignore"
15
18
  * @param {Object|boolean} [snapshotTestsOptions.filesystemEffects]
16
19
  * @param {boolean} [snapshotTestsOptions.filesystemEffects.textualFilesInline=false]
17
20
  * Put textual files content in the markdown (instead of separate files).
@@ -43,6 +46,11 @@ export const snapshotTests = async (
43
46
  throwWhenDiff = process.env.CI,
44
47
  } = {},
45
48
  ) => {
49
+ filesystemActions = {
50
+ ...filesystemActions,
51
+ "**/*.svg": "presence_only",
52
+ };
53
+
46
54
  const sourceName = urlToBasename(sourceFileUrl, true);
47
55
  const sourceBasename = urlToBasename(sourceFileUrl);
48
56
  const sourceFilename = urlToFilename(sourceFileUrl);
@@ -56,10 +64,6 @@ export const snapshotTests = async (
56
64
  return outFileUrl;
57
65
  };
58
66
  const outDirectoryUrl = generateOutFileUrl("");
59
- const outDirectorySnapshot = takeDirectorySnapshot(
60
- outDirectoryUrl,
61
- filesystemActions,
62
- );
63
67
  const sideEffectMdFileUrl = generateOutFileUrl(`${sourceFilename}.md`);
64
68
 
65
69
  const dirUrlMap = new Map();
@@ -78,9 +82,28 @@ export const snapshotTests = async (
78
82
  }
79
83
  onlyTestMap.set(scenario, { fn, options, callSite: getCallerLocation(2) });
80
84
  };
81
- fnRegisteringTest({ test });
85
+ const fnReturnValue = await fnRegisteringTest({ test });
82
86
 
83
- const activeTestMap = onlyTestMap.size ? onlyTestMap : testMap;
87
+ let activeTestMap;
88
+ const toIgnoreActions = {};
89
+ if (onlyTestMap.size) {
90
+ activeTestMap = onlyTestMap;
91
+ for (const [scenario] of testMap) {
92
+ const testScenario = asValidFilename(scenario);
93
+ const generateScenarioOutFileUrl = (outfilename) => {
94
+ return generateOutFileUrl(`${testScenario}/${outfilename}`);
95
+ };
96
+ const scenarioOutDirectoryUrl = generateScenarioOutFileUrl("");
97
+ toIgnoreActions[scenarioOutDirectoryUrl] = "ignore";
98
+ }
99
+ } else {
100
+ activeTestMap = testMap;
101
+ }
102
+ // ignore tout ceux aui sont désactivé
103
+ const outDirectorySnapshot = takeDirectorySnapshot(outDirectoryUrl, {
104
+ ...filesystemActions,
105
+ ...toIgnoreActions,
106
+ });
84
107
  const captureSideEffects = createCaptureSideEffects({
85
108
  sourceFileUrl,
86
109
  rootDirectoryUrl,
@@ -107,7 +130,6 @@ export const snapshotTests = async (
107
130
  markdown += "\n\n";
108
131
  markdown += generatedByLink;
109
132
  }
110
-
111
133
  const scenarioDirs = [];
112
134
  for (const [scenario, { fn, callSite }] of activeTestMap) {
113
135
  markdown += "\n\n";
@@ -135,6 +157,9 @@ export const snapshotTests = async (
135
157
  });
136
158
  markdown += sideEffectsMarkdown;
137
159
  }
160
+ if (typeof fnReturnValue === "function") {
161
+ await fnReturnValue();
162
+ }
138
163
  // if (sideEffectFilePattern === "./side_effects/[filename]/[filename].md") {
139
164
  // const scenarioParentDirUrl = new URL("./", sideEffectFileUrl);
140
165
  // const dirContent = readDirectorySync(scenarioParentDirUrl);