bun-match-svg 0.0.9 ā 0.0.11
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/README.md +10 -1
- package/bun.lockb +0 -0
- package/cli.ts +32 -12
- package/index.ts +12 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
# bun-match-svg
|
|
2
2
|
|
|
3
|
-
A custom matcher for Bun tests to compare SVG snapshots.
|
|
3
|
+
A custom matcher for Bun tests to compare SVG snapshots. Great for GitHub visual snapshot testing.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
test("your SVG test", () => {
|
|
7
|
+
const svgContent = generateSomeSvg() // Your function to generate SVG
|
|
8
|
+
expect(svgContent).toMatchSvgSnapshot(import.meta.path, "uniqueName")
|
|
9
|
+
})
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+

|
|
4
13
|
|
|
5
14
|
## Quick Start
|
|
6
15
|
|
package/bun.lockb
CHANGED
|
Binary file
|
package/cli.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
|
-
import { mkdir, writeFile } from "node:fs/promises"
|
|
3
|
+
import { mkdir, writeFile, access } from "node:fs/promises"
|
|
4
4
|
import { spawn } from "node:child_process"
|
|
5
5
|
|
|
6
6
|
const EXAMPLE_TEST = `import { expect, test } from "bun:test"
|
|
@@ -37,23 +37,43 @@ async function installDependency() {
|
|
|
37
37
|
})
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
async function fileExists(path: string): Promise<boolean> {
|
|
41
|
+
try {
|
|
42
|
+
await access(path)
|
|
43
|
+
return true
|
|
44
|
+
} catch {
|
|
45
|
+
return false
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
40
49
|
async function init() {
|
|
41
50
|
try {
|
|
42
51
|
console.log("š¦ Installing bun-match-svg...")
|
|
43
52
|
await installDependency()
|
|
44
53
|
|
|
45
54
|
await mkdir("tests/fixtures", { recursive: true })
|
|
46
|
-
|
|
47
|
-
await
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
|
|
56
|
+
if (!await fileExists("tests/svg.test.ts")) {
|
|
57
|
+
await writeFile("tests/svg.test.ts", EXAMPLE_TEST)
|
|
58
|
+
console.log("ā
Created example test in tests/svg.test.ts")
|
|
59
|
+
} else {
|
|
60
|
+
console.log("š tests/svg.test.ts already exists, skipping.")
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!await fileExists("tests/fixtures/preload.ts")) {
|
|
64
|
+
await writeFile("tests/fixtures/preload.ts", PRELOAD_FILE)
|
|
65
|
+
console.log("ā
Created preload file in tests/fixtures/preload.ts")
|
|
66
|
+
} else {
|
|
67
|
+
console.log("š tests/fixtures/preload.ts already exists, skipping.")
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!await fileExists("bunfig.toml")) {
|
|
71
|
+
await writeFile("bunfig.toml", BUNFIG)
|
|
72
|
+
console.log("ā
Created bunfig.toml")
|
|
73
|
+
} else {
|
|
74
|
+
console.log("š bunfig.toml already exists, skipping.")
|
|
75
|
+
}
|
|
76
|
+
|
|
57
77
|
console.log("\nš You can now run: bun test")
|
|
58
78
|
} catch (error) {
|
|
59
79
|
console.error("ā Error during initialization:", error)
|
package/index.ts
CHANGED
|
@@ -28,10 +28,14 @@ async function toMatchSvgSnapshot(
|
|
|
28
28
|
Boolean(process.env["BUN_UPDATE_SNAPSHOTS"])
|
|
29
29
|
|
|
30
30
|
if (!fs.existsSync(filePath) || updateSnapshot) {
|
|
31
|
-
|
|
31
|
+
if (updateSnapshot && fs.existsSync(filePath)) {
|
|
32
|
+
console.log("Updating snapshot at", filePath)
|
|
33
|
+
} else {
|
|
34
|
+
console.log("Writing snapshot to", filePath)
|
|
35
|
+
}
|
|
32
36
|
fs.writeFileSync(filePath, received)
|
|
33
37
|
return {
|
|
34
|
-
message: () => `Snapshot created at ${filePath}`,
|
|
38
|
+
message: () => `Snapshot ${updateSnapshot ? 'updated' : 'created'} at ${filePath}`,
|
|
35
39
|
pass: true,
|
|
36
40
|
}
|
|
37
41
|
}
|
|
@@ -97,10 +101,14 @@ async function toMatchMultipleSvgSnapshots(
|
|
|
97
101
|
Boolean(process.env["BUN_UPDATE_SNAPSHOTS"])
|
|
98
102
|
|
|
99
103
|
if (!fs.existsSync(filePath) || updateSnapshot) {
|
|
100
|
-
|
|
104
|
+
if (updateSnapshot && fs.existsSync(filePath)) {
|
|
105
|
+
console.log("Updating snapshot at", filePath)
|
|
106
|
+
} else {
|
|
107
|
+
console.log("Writing snapshot to", filePath)
|
|
108
|
+
}
|
|
101
109
|
fs.writeFileSync(filePath, received[index] as any)
|
|
102
110
|
passed.push({
|
|
103
|
-
message: `Snapshot ${svgName} created at ${filePath}`,
|
|
111
|
+
message: `Snapshot ${svgName} ${updateSnapshot ? 'updated' : 'created'} at ${filePath}`,
|
|
104
112
|
pass: true,
|
|
105
113
|
})
|
|
106
114
|
continue
|