vitest-image-snapshot 0.6.48 → 0.6.50

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/dist/index.js CHANGED
@@ -72,6 +72,8 @@ function pngBuffer(imageData) {
72
72
  }
73
73
  //#endregion
74
74
  //#region src/SnapshotManager.ts
75
+ const replaceableChars = /[<>:"/\\|?*]/g;
76
+ const unreplaceableChars = /\p{Control}+/gu;
75
77
  /** Manage reference/actual/diff snapshot files. */
76
78
  var ImageSnapshotManager = class {
77
79
  config;
@@ -87,13 +89,13 @@ var ImageSnapshotManager = class {
87
89
  };
88
90
  }
89
91
  referencePath(snapshotName) {
90
- return path.join(this.testDir, this.config.snapshotDir, `${snapshotName}.png`);
92
+ return path.join(this.testDir, this.config.snapshotDir, `${sanitizeName(snapshotName)}.png`);
91
93
  }
92
94
  actualPath(snapshotName) {
93
- return path.join(this.testDir, this.config.actualDir, `${snapshotName}.png`);
95
+ return path.join(this.testDir, this.config.actualDir, `${sanitizeName(snapshotName)}.png`);
94
96
  }
95
97
  diffPath(snapshotName) {
96
- return path.join(this.testDir, this.config.diffDir, `${snapshotName}.png`);
98
+ return path.join(this.testDir, this.config.diffDir, `${sanitizeName(snapshotName)}.png`);
97
99
  }
98
100
  /** Update failing snapshots (only in "all" mode with vitest -u) */
99
101
  shouldUpdate() {
@@ -123,6 +125,10 @@ var ImageSnapshotManager = class {
123
125
  await fs.promises.writeFile(filepath, buffer);
124
126
  }
125
127
  };
128
+ /** Replace chars that are invalid in Windows filenames. */
129
+ function sanitizeName(input) {
130
+ return input.replace(replaceableChars, "_").replace(unreplaceableChars, "").trim();
131
+ }
126
132
  //#endregion
127
133
  //#region src/ImageSnapshotMatcher.ts
128
134
  /** Register toMatchImage() matcher with Vitest */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest-image-snapshot",
3
- "version": "0.6.48",
3
+ "version": "0.6.50",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -1,6 +1,9 @@
1
1
  import * as fs from "node:fs";
2
2
  import * as path from "node:path";
3
3
 
4
+ const replaceableChars = /[<>:"/\\|?*]/g;
5
+ const unreplaceableChars = /\p{Control}+/gu;
6
+
4
7
  export interface SnapshotConfig {
5
8
  snapshotDir?: string;
6
9
  diffDir?: string;
@@ -29,7 +32,7 @@ export class ImageSnapshotManager {
29
32
  return path.join(
30
33
  this.testDir,
31
34
  this.config.snapshotDir,
32
- `${snapshotName}.png`,
35
+ `${sanitizeName(snapshotName)}.png`,
33
36
  );
34
37
  }
35
38
 
@@ -37,12 +40,16 @@ export class ImageSnapshotManager {
37
40
  return path.join(
38
41
  this.testDir,
39
42
  this.config.actualDir,
40
- `${snapshotName}.png`,
43
+ `${sanitizeName(snapshotName)}.png`,
41
44
  );
42
45
  }
43
46
 
44
47
  diffPath(snapshotName: string): string {
45
- return path.join(this.testDir, this.config.diffDir, `${snapshotName}.png`);
48
+ return path.join(
49
+ this.testDir,
50
+ this.config.diffDir,
51
+ `${sanitizeName(snapshotName)}.png`,
52
+ );
46
53
  }
47
54
 
48
55
  /** Update failing snapshots (only in "all" mode with vitest -u) */
@@ -88,3 +95,11 @@ export class ImageSnapshotManager {
88
95
  await fs.promises.writeFile(filepath, buffer);
89
96
  }
90
97
  }
98
+
99
+ /** Replace chars that are invalid in Windows filenames. */
100
+ function sanitizeName(input: string): string {
101
+ return input
102
+ .replace(replaceableChars, "_")
103
+ .replace(unreplaceableChars, "")
104
+ .trim();
105
+ }
@@ -17,6 +17,30 @@ test("generates correct file paths", () => {
17
17
  );
18
18
  });
19
19
 
20
+ test("sanitizes invalid filename characters", () => {
21
+ const manager = new ImageSnapshotManager(path.join("/path/to/test.ts"));
22
+ const snap = (name: string) => manager.referencePath(name);
23
+ const snapDir = path.join("/path/to", "__image_snapshots__");
24
+
25
+ // filesystem-invalid chars replaced with underscores
26
+ expect(snap("Category > test")).toBe(
27
+ path.join(snapDir, "Category _ test.png"),
28
+ );
29
+ expect(snap('a<>:"/\\|?*b')).toBe(path.join(snapDir, "a_________b.png"));
30
+
31
+ // control chars removed, outer whitespace trimmed
32
+ expect(snap("word1\x02word2")).toBe(path.join(snapDir, "word1word2.png"));
33
+ expect(snap(" spaced ")).toBe(path.join(snapDir, "spaced.png"));
34
+
35
+ // sanitization applied to all path types
36
+ expect(manager.actualPath("a > b")).toBe(
37
+ path.join("/path/to", "__image_actual__", "a _ b.png"),
38
+ );
39
+ expect(manager.diffPath("a > b")).toBe(
40
+ path.join("/path/to", "__image_diffs__", "a _ b.png"),
41
+ );
42
+ });
43
+
20
44
  test("supports custom directory names", () => {
21
45
  const testPath = path.join("/path/to/test.ts");
22
46
  const manager = new ImageSnapshotManager(testPath, {