forked from tscircuit/bun-match-svg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
174 lines (154 loc) · 4.75 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { expect, type MatcherResult } from "bun:test"
import * as fs from "node:fs"
import * as path from "node:path"
import looksSame from "looks-same"
async function toMatchSvgSnapshot(
// biome-ignore lint/suspicious/noExplicitAny: bun doesn't expose
this: any,
receivedMaybePromise: string | Promise<string>,
testPathOriginal: string,
svgName?: string,
): Promise<MatcherResult> {
const received = await receivedMaybePromise
const testPath = testPathOriginal.replace(/\.test\.tsx?$/, "")
const snapshotDir = path.join(path.dirname(testPath), "__snapshots__")
const snapshotName = svgName
? `${svgName}.snap.svg`
: `${path.basename(testPath)}.snap.svg`
const filePath = path.join(snapshotDir, snapshotName)
if (!fs.existsSync(snapshotDir)) {
fs.mkdirSync(snapshotDir, { recursive: true })
}
const updateSnapshot =
process.argv.includes("--update-snapshots") ||
process.argv.includes("-u") ||
Boolean(process.env.BUN_UPDATE_SNAPSHOTS)
if (!fs.existsSync(filePath) || updateSnapshot) {
console.log("Writing snapshot to", filePath)
fs.writeFileSync(filePath, received)
return {
message: () => `Snapshot created at ${filePath}`,
pass: true,
}
}
const existingSnapshot = fs.readFileSync(filePath, "utf-8")
const result = await looksSame(
Buffer.from(received),
Buffer.from(existingSnapshot),
{
strict: false,
tolerance: 2,
},
)
if (result.equal) {
return {
message: () => "Snapshot matches",
pass: true,
}
}
const diffPath = filePath.replace(".snap.svg", ".diff.png")
await looksSame.createDiff({
reference: Buffer.from(existingSnapshot),
current: Buffer.from(received),
diff: diffPath,
highlightColor: "#ff00ff",
})
return {
message: () => `Snapshot does not match. Diff saved at ${diffPath}`,
pass: false,
}
}
async function toMatchMultipleSvgSnapshots(
// biome-ignore lint/suspicious/noExplicitAny: bun doesn't expose
this: any,
receivedMaybePromise: string[] | Promise<string[]>,
testPathOriginal: string,
svgNames: string[],
): Promise<MatcherResult> {
const passed = []
const failed = []
for (let index = 0; index < svgNames.length; index++) {
const svgName = svgNames[index]
const received = await receivedMaybePromise
const testPath = testPathOriginal.replace(/\.test\.tsx?$/, "")
const snapshotDir = path.join(path.dirname(testPath), "__snapshots__")
const snapshotName = svgName
? `${svgName}.snap.svg`
: `${path.basename(testPath)}.snap.svg`
const filePath = path.join(snapshotDir, snapshotName)
if (!fs.existsSync(snapshotDir)) {
fs.mkdirSync(snapshotDir, { recursive: true })
}
const updateSnapshot =
process.argv.includes("--update-snapshots") ||
process.argv.includes("-u") ||
Boolean(process.env.BUN_UPDATE_SNAPSHOTS)
if (!fs.existsSync(filePath) || updateSnapshot) {
console.log("Writing snapshot to", filePath)
fs.writeFileSync(filePath, received[index] as any)
passed.push({
message: `Snapshot ${svgName} created at ${filePath}`,
pass: true,
})
continue
}
const existingSnapshot = fs.readFileSync(filePath, "utf-8")
const result = await looksSame(
Buffer.from(received[index] as any),
Buffer.from(existingSnapshot),
{
strict: false,
tolerance: 2,
},
)
if (result.equal) {
passed.push({
message: `Snapshot ${svgName} matches`,
pass: true,
})
continue
}
const diffPath = filePath.replace(".snap.svg", ".diff.png")
await looksSame.createDiff({
reference: Buffer.from(existingSnapshot),
current: Buffer.from(received[index] as any),
diff: diffPath,
highlightColor: "#ff00ff",
})
failed.push({
message: `Snapshot ${svgName} does not match. Diff saved at ${diffPath}`,
pass: false,
})
}
let aggregatedMessage = ""
if (failed.length === 0) {
for (const result of passed) aggregatedMessage += `${result.message}\n`
return {
pass: true,
message: () => aggregatedMessage,
}
}
for (const result of failed) aggregatedMessage += `${result.message}\n`
return {
pass: false,
message: () => aggregatedMessage,
}
}
expect.extend({
// biome-ignore lint/suspicious/noExplicitAny:
toMatchSvgSnapshot: toMatchSvgSnapshot as any,
// biome-ignore lint/suspicious/noExplicitAny:
toMatchMultipleSvgSnapshots: toMatchMultipleSvgSnapshots as any,
})
declare module "bun:test" {
interface Matchers<T = unknown> {
toMatchSvgSnapshot(
testPath: string,
svgName?: string,
): Promise<MatcherResult>
toMatchMultipleSvgSnapshots(
testPath: string,
svgNames?: string[],
): Promise<MatcherResult>
}
}