bun-match-svg 0.0.11 → 0.0.13

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/index.ts CHANGED
@@ -26,16 +26,15 @@ async function toMatchSvgSnapshot(
26
26
  process.argv.includes("--update-snapshots") ||
27
27
  process.argv.includes("-u") ||
28
28
  Boolean(process.env["BUN_UPDATE_SNAPSHOTS"])
29
+ const forceUpdate = Boolean(process.env["FORCE_BUN_UPDATE_SNAPSHOTS"])
29
30
 
30
- if (!fs.existsSync(filePath) || updateSnapshot) {
31
- if (updateSnapshot && fs.existsSync(filePath)) {
32
- console.log("Updating snapshot at", filePath)
33
- } else {
34
- console.log("Writing snapshot to", filePath)
35
- }
31
+ const fileExists = fs.existsSync(filePath)
32
+
33
+ if (!fileExists) {
34
+ console.log("Writing snapshot to", filePath)
36
35
  fs.writeFileSync(filePath, received)
37
36
  return {
38
- message: () => `Snapshot ${updateSnapshot ? 'updated' : 'created'} at ${filePath}`,
37
+ message: () => `Snapshot created at ${filePath}`,
39
38
  pass: true,
40
39
  }
41
40
  }
@@ -51,6 +50,21 @@ async function toMatchSvgSnapshot(
51
50
  },
52
51
  )
53
52
 
53
+ if (updateSnapshot) {
54
+ if (!forceUpdate && result.equal) {
55
+ return {
56
+ message: () => "Snapshot matches",
57
+ pass: true,
58
+ }
59
+ }
60
+ console.log("Updating snapshot at", filePath)
61
+ fs.writeFileSync(filePath, received)
62
+ return {
63
+ message: () => `Snapshot updated at ${filePath}`,
64
+ pass: true,
65
+ }
66
+ }
67
+
54
68
  if (result.equal) {
55
69
  return {
56
70
  message: () => "Snapshot matches",
@@ -99,16 +113,15 @@ async function toMatchMultipleSvgSnapshots(
99
113
  process.argv.includes("--update-snapshots") ||
100
114
  process.argv.includes("-u") ||
101
115
  Boolean(process.env["BUN_UPDATE_SNAPSHOTS"])
116
+ const forceUpdate = Boolean(process.env["FORCE_BUN_UPDATE_SNAPSHOTS"])
102
117
 
103
- if (!fs.existsSync(filePath) || updateSnapshot) {
104
- if (updateSnapshot && fs.existsSync(filePath)) {
105
- console.log("Updating snapshot at", filePath)
106
- } else {
107
- console.log("Writing snapshot to", filePath)
108
- }
118
+ const fileExists = fs.existsSync(filePath)
119
+
120
+ if (!fileExists) {
121
+ console.log("Writing snapshot to", filePath)
109
122
  fs.writeFileSync(filePath, received[index] as any)
110
123
  passed.push({
111
- message: `Snapshot ${svgName} ${updateSnapshot ? 'updated' : 'created'} at ${filePath}`,
124
+ message: `Snapshot ${svgName} created at ${filePath}`,
112
125
  pass: true,
113
126
  })
114
127
  continue
@@ -125,6 +138,23 @@ async function toMatchMultipleSvgSnapshots(
125
138
  },
126
139
  )
127
140
 
141
+ if (updateSnapshot) {
142
+ if (!forceUpdate && result.equal) {
143
+ passed.push({
144
+ message: `Snapshot ${svgName} matches`,
145
+ pass: true,
146
+ })
147
+ continue
148
+ }
149
+ console.log("Updating snapshot at", filePath)
150
+ fs.writeFileSync(filePath, received[index] as any)
151
+ passed.push({
152
+ message: `Snapshot ${svgName} updated at ${filePath}`,
153
+ pass: true,
154
+ })
155
+ continue
156
+ }
157
+
128
158
  if (result.equal) {
129
159
  passed.push({
130
160
  message: `Snapshot ${svgName} matches`,
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "bun-match-svg",
3
3
  "module": "index.ts",
4
- "version": "0.0.11",
4
+ "version": "0.0.13",
5
5
  "type": "module",
6
+ "license": "MIT",
6
7
  "bin": {
7
8
  "bun-match-svg": "./cli.ts"
8
9
  },
@@ -9,6 +9,7 @@ const testSvg = `<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg
9
9
 
10
10
  const snapshotDir = path.join(__dirname, "__snapshots__")
11
11
  const snapshotPath = path.join(snapshotDir, "test.snap.svg")
12
+ const metadataSnapshotPath = path.join(snapshotDir, "metadata.snap.svg")
12
13
 
13
14
  beforeAll(() => {
14
15
  if (!fs.existsSync(snapshotDir)) {
@@ -20,6 +21,9 @@ afterAll(() => {
20
21
  if (fs.existsSync(snapshotPath)) {
21
22
  fs.unlinkSync(snapshotPath)
22
23
  }
24
+ if (fs.existsSync(metadataSnapshotPath)) {
25
+ fs.unlinkSync(metadataSnapshotPath)
26
+ }
23
27
  if (fs.existsSync(snapshotDir)) {
24
28
  fs.rmdirSync(snapshotDir, { recursive: true })
25
29
  }
@@ -36,6 +40,20 @@ test("toMatchSvgSnapshot creates and matches snapshot", async () => {
36
40
  await expect(testSvg).toMatchSvgSnapshot(import.meta.path, "test")
37
41
  })
38
42
 
43
+ test("does not update snapshot when visually identical", async () => {
44
+ const svgWithComment = `<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
45
+ <!-- comment -->
46
+ <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
47
+ </svg>`
48
+
49
+ await expect(testSvg).toMatchSvgSnapshot(import.meta.path, "metadata")
50
+ const original = fs.readFileSync(metadataSnapshotPath, "utf-8")
51
+
52
+ await expect(svgWithComment).toMatchSvgSnapshot(import.meta.path, "metadata")
53
+ const updated = fs.readFileSync(metadataSnapshotPath, "utf-8")
54
+ expect(updated).toBe(original)
55
+ })
56
+
39
57
  // test("toMatchSvgSnapshot detects differences", async () => {
40
58
  // const modifiedSvg = testSvg.replace('r="40"', 'r="30"')
41
59