bun-match-svg 0.0.1 → 0.0.3
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/.github/workflows/CODEOWNERS +1 -0
- package/.github/workflows/bun-formatcheck.yml +26 -0
- package/.github/workflows/bun-pver-release.yml +25 -0
- package/.github/workflows/bun-test.yml +24 -0
- package/LICENSE +21 -0
- package/README.md +75 -13
- package/index.ts +93 -1
- package/package.json +4 -3
- package/tests/toMatchMultipleSvgSnapshots.test.ts +59 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @seveibar @tscircuit/core
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Created using @tscircuit/plop (npm install -g @tscircuit/plop)
|
|
2
|
+
name: Format Check
|
|
3
|
+
|
|
4
|
+
on:
|
|
5
|
+
push:
|
|
6
|
+
branches: [main]
|
|
7
|
+
pull_request:
|
|
8
|
+
branches: [main]
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
format-check:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v3
|
|
16
|
+
|
|
17
|
+
- name: Setup bun
|
|
18
|
+
uses: oven-sh/setup-bun@v1
|
|
19
|
+
with:
|
|
20
|
+
bun-version: latest
|
|
21
|
+
|
|
22
|
+
- name: Install dependencies
|
|
23
|
+
run: bun install
|
|
24
|
+
|
|
25
|
+
- name: Run format check
|
|
26
|
+
run: bun run format:check
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Created using @tscircuit/plop (npm install -g @tscircuit/plop)
|
|
2
|
+
name: Publish to npm
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v3
|
|
12
|
+
- name: Setup bun
|
|
13
|
+
uses: oven-sh/setup-bun@v1
|
|
14
|
+
with:
|
|
15
|
+
bun-version: latest
|
|
16
|
+
- uses: actions/setup-node@v3
|
|
17
|
+
with:
|
|
18
|
+
node-version: 20
|
|
19
|
+
registry-url: https://registry.npmjs.org/
|
|
20
|
+
- run: npm install -g pver
|
|
21
|
+
- run: bun install --frozen-lockfile
|
|
22
|
+
- run: bun run build
|
|
23
|
+
- run: pver release
|
|
24
|
+
env:
|
|
25
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Created using @tscircuit/plop (npm install -g @tscircuit/plop)
|
|
2
|
+
name: Bun Test
|
|
3
|
+
|
|
4
|
+
on:
|
|
5
|
+
pull_request:
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
test:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
|
|
11
|
+
steps:
|
|
12
|
+
- name: Checkout code
|
|
13
|
+
uses: actions/checkout@v2
|
|
14
|
+
|
|
15
|
+
- name: Setup bun
|
|
16
|
+
uses: oven-sh/setup-bun@v1
|
|
17
|
+
with:
|
|
18
|
+
bun-version: latest
|
|
19
|
+
|
|
20
|
+
- name: Install dependencies
|
|
21
|
+
run: bun install
|
|
22
|
+
|
|
23
|
+
- name: Run tests
|
|
24
|
+
run: bun test
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 tscircuit Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,23 +1,85 @@
|
|
|
1
1
|
# bun-match-svg
|
|
2
2
|
|
|
3
|
+
A custom matcher for Bun tests to compare SVG snapshots.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add -D bun-match-svg
|
|
9
|
+
```
|
|
10
|
+
|
|
3
11
|
## Usage
|
|
4
12
|
|
|
13
|
+
### Basic Usage
|
|
14
|
+
|
|
15
|
+
Import the library in your test file:
|
|
16
|
+
|
|
5
17
|
```ts
|
|
6
18
|
import "bun-match-svg"
|
|
7
19
|
|
|
8
|
-
test("
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
pinCount: 8,
|
|
14
|
-
}
|
|
20
|
+
test("your SVG test", () => {
|
|
21
|
+
const svgContent = generateSomeSvg() // Your function to generate SVG
|
|
22
|
+
expect(svgContent).toMatchSvgSnapshot(import.meta.path, "uniqueName")
|
|
23
|
+
})
|
|
24
|
+
```
|
|
15
25
|
|
|
16
|
-
|
|
26
|
+
The `toMatchSvgSnapshot` matcher takes two arguments:
|
|
17
27
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
1. `import.meta.path`: The path of the current test file.
|
|
29
|
+
2. `uniqueName` (optional): A unique name for the snapshot. If not provided, it will use the test file name.
|
|
30
|
+
|
|
31
|
+
### Automatically Preload
|
|
32
|
+
|
|
33
|
+
To make `toMatchSvgSnapshot` available in all your test files without importing it in each one:
|
|
34
|
+
|
|
35
|
+
1. Create a file `tests/fixtures/preload.ts` with the following content:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import "bun-match-svg"
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
2. Define a `bunfig.toml` file in your project root with:
|
|
42
|
+
|
|
43
|
+
```toml
|
|
44
|
+
[test]
|
|
45
|
+
preload = ["./tests/fixtures/preload.ts"]
|
|
23
46
|
```
|
|
47
|
+
|
|
48
|
+
Now `toMatchSvgSnapshot` will be automatically available in every test file.
|
|
49
|
+
|
|
50
|
+
## How It Works
|
|
51
|
+
|
|
52
|
+
- On first run, it creates a snapshot of your SVG.
|
|
53
|
+
- On subsequent runs, it compares the current SVG with the snapshot.
|
|
54
|
+
- If differences are found, it generates a diff image.
|
|
55
|
+
|
|
56
|
+
## Updating Snapshots
|
|
57
|
+
|
|
58
|
+
To update existing snapshots, run your tests with:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
bun test --update-snapshots
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Or set the environment variable:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
BUN_UPDATE_SNAPSHOTS=1 bun test
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Configuration
|
|
71
|
+
|
|
72
|
+
The matcher uses `looks-same` for comparison with these default settings:
|
|
73
|
+
|
|
74
|
+
- `strict: false`
|
|
75
|
+
- `tolerance: 2`
|
|
76
|
+
|
|
77
|
+
There currently isn't a way to configure this, but PRs welcome!
|
|
78
|
+
|
|
79
|
+
## Contributing
|
|
80
|
+
|
|
81
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
This project is open source and available under the [MIT License](LICENSE).
|
package/index.ts
CHANGED
|
@@ -6,10 +6,11 @@ import looksSame from "looks-same"
|
|
|
6
6
|
async function toMatchSvgSnapshot(
|
|
7
7
|
// biome-ignore lint/suspicious/noExplicitAny: bun doesn't expose
|
|
8
8
|
this: any,
|
|
9
|
-
|
|
9
|
+
receivedMaybePromise: string | Promise<string>,
|
|
10
10
|
testPathOriginal: string,
|
|
11
11
|
svgName?: string,
|
|
12
12
|
): Promise<MatcherResult> {
|
|
13
|
+
const received = await receivedMaybePromise
|
|
13
14
|
const testPath = testPathOriginal.replace(/\.test\.tsx?$/, "")
|
|
14
15
|
const snapshotDir = path.join(path.dirname(testPath), "__snapshots__")
|
|
15
16
|
const snapshotName = svgName
|
|
@@ -67,9 +68,96 @@ async function toMatchSvgSnapshot(
|
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
|
|
71
|
+
async function toMatchMultipleSvgSnapshots(
|
|
72
|
+
// biome-ignore lint/suspicious/noExplicitAny: bun doesn't expose
|
|
73
|
+
this: any,
|
|
74
|
+
receivedMaybePromise: string[] | Promise<string[]>,
|
|
75
|
+
testPathOriginal: string,
|
|
76
|
+
svgNames: string[],
|
|
77
|
+
): Promise<MatcherResult> {
|
|
78
|
+
const passed = []
|
|
79
|
+
const failed = []
|
|
80
|
+
for (let index = 0; index < svgNames.length; index++) {
|
|
81
|
+
const svgName = svgNames[index]
|
|
82
|
+
const received = await receivedMaybePromise
|
|
83
|
+
const testPath = testPathOriginal.replace(/\.test\.tsx?$/, "")
|
|
84
|
+
const snapshotDir = path.join(path.dirname(testPath), "__snapshots__")
|
|
85
|
+
const snapshotName = svgName
|
|
86
|
+
? `${svgName}.snap.svg`
|
|
87
|
+
: `${path.basename(testPath)}.snap.svg`
|
|
88
|
+
const filePath = path.join(snapshotDir, snapshotName)
|
|
89
|
+
|
|
90
|
+
if (!fs.existsSync(snapshotDir)) {
|
|
91
|
+
fs.mkdirSync(snapshotDir, { recursive: true })
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const updateSnapshot =
|
|
95
|
+
process.argv.includes("--update-snapshots") ||
|
|
96
|
+
process.argv.includes("-u") ||
|
|
97
|
+
Boolean(process.env.BUN_UPDATE_SNAPSHOTS)
|
|
98
|
+
|
|
99
|
+
if (!fs.existsSync(filePath) || updateSnapshot) {
|
|
100
|
+
console.log("Writing snapshot to", filePath)
|
|
101
|
+
fs.writeFileSync(filePath, received[index])
|
|
102
|
+
passed.push({
|
|
103
|
+
message: `Snapshot ${svgName} created at ${filePath}`,
|
|
104
|
+
pass: true,
|
|
105
|
+
})
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const existingSnapshot = fs.readFileSync(filePath, "utf-8")
|
|
110
|
+
|
|
111
|
+
const result = await looksSame(
|
|
112
|
+
Buffer.from(received[index]),
|
|
113
|
+
Buffer.from(existingSnapshot),
|
|
114
|
+
{
|
|
115
|
+
strict: false,
|
|
116
|
+
tolerance: 2,
|
|
117
|
+
},
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
if (result.equal) {
|
|
121
|
+
passed.push({
|
|
122
|
+
message: `Snapshot ${svgName} matches`,
|
|
123
|
+
pass: true,
|
|
124
|
+
})
|
|
125
|
+
continue
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const diffPath = filePath.replace(".snap.svg", ".diff.png")
|
|
129
|
+
await looksSame.createDiff({
|
|
130
|
+
reference: Buffer.from(existingSnapshot),
|
|
131
|
+
current: Buffer.from(received[index]),
|
|
132
|
+
diff: diffPath,
|
|
133
|
+
highlightColor: "#ff00ff",
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
failed.push({
|
|
137
|
+
message: `Snapshot ${svgName} does not match. Diff saved at ${diffPath}`,
|
|
138
|
+
pass: false,
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
let aggregatedMessage = ""
|
|
142
|
+
if (failed.length === 0) {
|
|
143
|
+
for (const result of passed) aggregatedMessage += `${result.message}\n`
|
|
144
|
+
return {
|
|
145
|
+
pass: true,
|
|
146
|
+
message: () => aggregatedMessage,
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
for (const result of failed) aggregatedMessage += `${result.message}\n`
|
|
150
|
+
return {
|
|
151
|
+
pass: false,
|
|
152
|
+
message: () => aggregatedMessage,
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
70
156
|
expect.extend({
|
|
71
157
|
// biome-ignore lint/suspicious/noExplicitAny:
|
|
72
158
|
toMatchSvgSnapshot: toMatchSvgSnapshot as any,
|
|
159
|
+
// biome-ignore lint/suspicious/noExplicitAny:
|
|
160
|
+
toMatchMultipleSvgSnapshots: toMatchMultipleSvgSnapshots as any,
|
|
73
161
|
})
|
|
74
162
|
|
|
75
163
|
declare module "bun:test" {
|
|
@@ -78,5 +166,9 @@ declare module "bun:test" {
|
|
|
78
166
|
testPath: string,
|
|
79
167
|
svgName?: string,
|
|
80
168
|
): Promise<MatcherResult>
|
|
169
|
+
toMatchMultipleSvgSnapshots(
|
|
170
|
+
testPath: string,
|
|
171
|
+
svgNames?: string[],
|
|
172
|
+
): Promise<MatcherResult>
|
|
81
173
|
}
|
|
82
174
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bun-match-svg",
|
|
3
3
|
"module": "index.ts",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"devDependencies": {
|
|
7
7
|
"@types/bun": "latest"
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"looks-same": "^9.0.1"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"test": "bun test"
|
|
16
|
+
"test": "bun test",
|
|
17
|
+
"build": "echo 'no build step'"
|
|
17
18
|
}
|
|
18
|
-
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { expect, test, beforeAll, afterAll } from "bun:test"
|
|
2
|
+
import * as fs from "node:fs"
|
|
3
|
+
import * as path from "node:path"
|
|
4
|
+
import "../index"
|
|
5
|
+
|
|
6
|
+
const testSvgs = [
|
|
7
|
+
`<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
|
|
8
|
+
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
|
|
9
|
+
</svg>`,
|
|
10
|
+
`<svg width="400" height="110" xmlns="http://www.w3.org/2000/svg">
|
|
11
|
+
<defs>
|
|
12
|
+
<pattern id="patt1" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse">
|
|
13
|
+
<circle cx="10" cy="10" r="10" fill="red" />
|
|
14
|
+
</pattern>
|
|
15
|
+
</defs>
|
|
16
|
+
|
|
17
|
+
<rect width="200" height="100" x="0" y="0" stroke="black" fill="url(#patt1)" />
|
|
18
|
+
</svg>
|
|
19
|
+
`,
|
|
20
|
+
`<svg height="220" width="500" xmlns="http://www.w3.org/2000/svg">
|
|
21
|
+
<polygon points="100,10 150,190 50,190" style="fill:lime;stroke:purple;stroke-width:3" />
|
|
22
|
+
</svg>`,
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
const svgNames: string[] = []
|
|
26
|
+
for (let i = 0; i < testSvgs.length; i++) svgNames.push(`test${i + 1}`)
|
|
27
|
+
|
|
28
|
+
const snapshotDir = path.join(__dirname, "__snapshots__")
|
|
29
|
+
const snapshotPaths = svgNames.map((svgName) =>
|
|
30
|
+
path.join(snapshotDir, `${svgName}.snap.svg`),
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
beforeAll(() => {
|
|
34
|
+
if (!fs.existsSync(snapshotDir)) {
|
|
35
|
+
fs.mkdirSync(snapshotDir, { recursive: true })
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
afterAll(() => {
|
|
40
|
+
for (const snapshotPath of snapshotPaths)
|
|
41
|
+
if (fs.existsSync(snapshotPath)) {
|
|
42
|
+
fs.unlinkSync(snapshotPath)
|
|
43
|
+
}
|
|
44
|
+
if (fs.existsSync(snapshotDir)) {
|
|
45
|
+
fs.rmdirSync(snapshotDir, { recursive: true })
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test("toMatchMultipleSvgSnapshots creates and matches snapshots", async () => {
|
|
50
|
+
// First run: create snapshot
|
|
51
|
+
await expect(testSvgs).toMatchMultipleSvgSnapshots(import.meta.path, svgNames)
|
|
52
|
+
|
|
53
|
+
// Verify snapshot was created
|
|
54
|
+
for (const snapshotPath of snapshotPaths)
|
|
55
|
+
expect(fs.existsSync(snapshotPath)).toBe(true)
|
|
56
|
+
|
|
57
|
+
// Second run: match existing snapshot
|
|
58
|
+
await expect(testSvgs).toMatchMultipleSvgSnapshots(import.meta.path, svgNames)
|
|
59
|
+
})
|