bun-match-svg 0.0.13 ā 0.0.15
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/cli.ts +18 -5
- package/index.ts +2 -2
- package/package.json +1 -1
- package/tests/cli.test.ts +122 -0
- package/tests/toMatchMultipleSvgSnapshots.test.ts +2 -1
- package/tests/toMatchSvgSnapshot.test.ts +6 -2
package/cli.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
|
-
import { mkdir, writeFile, access } from "node:fs/promises"
|
|
3
|
+
import { mkdir, writeFile, access, readFile, appendFile } 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"
|
|
@@ -21,7 +21,7 @@ const PRELOAD_FILE = `import "bun-match-svg"`
|
|
|
21
21
|
const BUNFIG = `[test]
|
|
22
22
|
preload = ["./tests/fixtures/preload.ts"]`
|
|
23
23
|
|
|
24
|
-
async function installDependency() {
|
|
24
|
+
export async function installDependency() {
|
|
25
25
|
return new Promise((resolve, reject) => {
|
|
26
26
|
const install = spawn('bun', ['add', '-d', 'bun-match-svg'], {
|
|
27
27
|
stdio: 'inherit'
|
|
@@ -37,7 +37,7 @@ async function installDependency() {
|
|
|
37
37
|
})
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
async function fileExists(path: string): Promise<boolean> {
|
|
40
|
+
export async function fileExists(path: string): Promise<boolean> {
|
|
41
41
|
try {
|
|
42
42
|
await access(path)
|
|
43
43
|
return true
|
|
@@ -46,7 +46,7 @@ async function fileExists(path: string): Promise<boolean> {
|
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
async function init() {
|
|
49
|
+
export async function init() {
|
|
50
50
|
try {
|
|
51
51
|
console.log("š¦ Installing bun-match-svg...")
|
|
52
52
|
await installDependency()
|
|
@@ -71,7 +71,20 @@ async function init() {
|
|
|
71
71
|
await writeFile("bunfig.toml", BUNFIG)
|
|
72
72
|
console.log("ā
Created bunfig.toml")
|
|
73
73
|
} else {
|
|
74
|
-
|
|
74
|
+
const bunfigContent = await readFile("bunfig.toml", "utf-8")
|
|
75
|
+
if (bunfigContent.includes('preload = ["./tests/fixtures/preload.ts"]')) {
|
|
76
|
+
console.log("š bunfig.toml already has preload configuration, skipping.")
|
|
77
|
+
} else if (bunfigContent.match(/^\s*\[test\]\s*$/m)) {
|
|
78
|
+
const updatedContent = bunfigContent.replace(
|
|
79
|
+
/(^\s*\[test\]\s*$)/m,
|
|
80
|
+
`$1\npreload = ["./tests/fixtures/preload.ts"]`,
|
|
81
|
+
)
|
|
82
|
+
await writeFile("bunfig.toml", updatedContent)
|
|
83
|
+
console.log("ā
Updated bunfig.toml with preload configuration.")
|
|
84
|
+
} else {
|
|
85
|
+
await appendFile("bunfig.toml", `\n\n${BUNFIG}`)
|
|
86
|
+
console.log("ā
Added preload configuration to bunfig.toml.")
|
|
87
|
+
}
|
|
75
88
|
}
|
|
76
89
|
|
|
77
90
|
console.log("\nš You can now run: bun test")
|
package/index.ts
CHANGED
|
@@ -14,7 +14,7 @@ async function toMatchSvgSnapshot(
|
|
|
14
14
|
const testPath = testPathOriginal.replace(/\.test\.tsx?$/, "")
|
|
15
15
|
const snapshotDir = path.join(path.dirname(testPath), "__snapshots__")
|
|
16
16
|
const snapshotName = svgName
|
|
17
|
-
? `${svgName}.snap.svg`
|
|
17
|
+
? `${path.basename(testPath)}-${svgName}.snap.svg`
|
|
18
18
|
: `${path.basename(testPath)}.snap.svg`
|
|
19
19
|
const filePath = path.join(snapshotDir, snapshotName)
|
|
20
20
|
|
|
@@ -101,7 +101,7 @@ async function toMatchMultipleSvgSnapshots(
|
|
|
101
101
|
const testPath = testPathOriginal.replace(/\.test\.tsx?$/, "")
|
|
102
102
|
const snapshotDir = path.join(path.dirname(testPath), "__snapshots__")
|
|
103
103
|
const snapshotName = svgName
|
|
104
|
-
? `${svgName}.snap.svg`
|
|
104
|
+
? `${path.basename(testPath)}-${svgName}.snap.svg`
|
|
105
105
|
: `${path.basename(testPath)}.snap.svg`
|
|
106
106
|
const filePath = path.join(snapshotDir, snapshotName)
|
|
107
107
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import {
|
|
2
|
+
afterAll,
|
|
3
|
+
afterEach,
|
|
4
|
+
beforeAll,
|
|
5
|
+
beforeEach,
|
|
6
|
+
describe,
|
|
7
|
+
expect,
|
|
8
|
+
spyOn,
|
|
9
|
+
test,
|
|
10
|
+
type Mock,
|
|
11
|
+
} from "bun:test"
|
|
12
|
+
import * as fs from "node:fs/promises"
|
|
13
|
+
import * as path from "node:path"
|
|
14
|
+
import * as cli from "../cli"
|
|
15
|
+
|
|
16
|
+
const tempDir = path.join(__dirname, "cli-test-temp")
|
|
17
|
+
const originalCwd = process.cwd()
|
|
18
|
+
|
|
19
|
+
describe("cli init", () => {
|
|
20
|
+
let installSpy: Mock<typeof cli.installDependency>
|
|
21
|
+
let consoleSpy: Mock<typeof console.log>
|
|
22
|
+
|
|
23
|
+
beforeAll(async () => {
|
|
24
|
+
await fs.mkdir(tempDir, { recursive: true })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
afterAll(async () => {
|
|
28
|
+
await fs.rm(tempDir, { recursive: true, force: true })
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
beforeEach(async () => {
|
|
32
|
+
process.chdir(tempDir)
|
|
33
|
+
installSpy = spyOn(cli, "installDependency").mockImplementation(async () => {})
|
|
34
|
+
consoleSpy = spyOn(console, "log").mockImplementation(() => {})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
afterEach(async () => {
|
|
38
|
+
process.chdir(originalCwd)
|
|
39
|
+
const files = await fs.readdir(tempDir)
|
|
40
|
+
for (const file of files) {
|
|
41
|
+
await fs.rm(path.join(tempDir, file), { recursive: true, force: true })
|
|
42
|
+
}
|
|
43
|
+
installSpy.mockRestore()
|
|
44
|
+
consoleSpy.mockRestore()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test("should create files and config on a fresh project", async () => {
|
|
48
|
+
await cli.init()
|
|
49
|
+
|
|
50
|
+
expect(installSpy).toHaveBeenCalledTimes(1)
|
|
51
|
+
|
|
52
|
+
// Check files
|
|
53
|
+
const testFileExists = await cli.fileExists("tests/svg.test.ts")
|
|
54
|
+
expect(testFileExists).toBe(true)
|
|
55
|
+
const preloadFileExists = await cli.fileExists("tests/fixtures/preload.ts")
|
|
56
|
+
expect(preloadFileExists).toBe(true)
|
|
57
|
+
const bunfigExists = await cli.fileExists("bunfig.toml")
|
|
58
|
+
expect(bunfigExists).toBe(true)
|
|
59
|
+
|
|
60
|
+
// Check bunfig.toml content
|
|
61
|
+
const bunfigContent = await fs.readFile("bunfig.toml", "utf-8")
|
|
62
|
+
expect(bunfigContent).toContain(`[test]`)
|
|
63
|
+
expect(bunfigContent).toContain(`preload = ["./tests/fixtures/preload.ts"]`)
|
|
64
|
+
|
|
65
|
+
// Check console logs
|
|
66
|
+
const logs = consoleSpy.mock.calls.flat().join("\n")
|
|
67
|
+
expect(logs).toContain("ā
Created example test in tests/svg.test.ts")
|
|
68
|
+
expect(logs).toContain("ā
Created preload file in tests/fixtures/preload.ts")
|
|
69
|
+
expect(logs).toContain("ā
Created bunfig.toml")
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test("should skip creating files that already exist", async () => {
|
|
73
|
+
// Pre-create files
|
|
74
|
+
await fs.mkdir("tests/fixtures", { recursive: true })
|
|
75
|
+
await fs.writeFile("tests/svg.test.ts", "test")
|
|
76
|
+
await fs.writeFile("tests/fixtures/preload.ts", "preload")
|
|
77
|
+
await fs.writeFile(
|
|
78
|
+
"bunfig.toml",
|
|
79
|
+
`[test]\npreload = ["./tests/fixtures/preload.ts"]`,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
await cli.init()
|
|
83
|
+
|
|
84
|
+
const logs = consoleSpy.mock.calls.flat().join("\n")
|
|
85
|
+
expect(logs).toContain("š tests/svg.test.ts already exists, skipping.")
|
|
86
|
+
expect(logs).toContain(
|
|
87
|
+
"š tests/fixtures/preload.ts already exists, skipping.",
|
|
88
|
+
)
|
|
89
|
+
expect(logs).toContain(
|
|
90
|
+
"š bunfig.toml already has preload configuration, skipping.",
|
|
91
|
+
)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test("should update bunfig.toml if [test] section exists", async () => {
|
|
95
|
+
await fs.writeFile("bunfig.toml", "[test]")
|
|
96
|
+
|
|
97
|
+
await cli.init()
|
|
98
|
+
|
|
99
|
+
const bunfigContent = await fs.readFile("bunfig.toml", "utf-8")
|
|
100
|
+
expect(bunfigContent).toBe(
|
|
101
|
+
`[test]\npreload = ["./tests/fixtures/preload.ts"]`,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
const logs = consoleSpy.mock.calls.flat().join("\n")
|
|
105
|
+
expect(logs).toContain("ā
Updated bunfig.toml with preload configuration.")
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test("should append to bunfig.toml if it exists but is unrelated", async () => {
|
|
109
|
+
await fs.writeFile("bunfig.toml", "[some-other-section]")
|
|
110
|
+
|
|
111
|
+
await cli.init()
|
|
112
|
+
|
|
113
|
+
const bunfigContent = await fs.readFile("bunfig.toml", "utf-8")
|
|
114
|
+
expect(bunfigContent).toContain("[some-other-section]")
|
|
115
|
+
expect(bunfigContent).toContain(
|
|
116
|
+
`\n\n[test]\npreload = ["./tests/fixtures/preload.ts"]`,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
const logs = consoleSpy.mock.calls.flat().join("\n")
|
|
120
|
+
expect(logs).toContain("ā
Added preload configuration to bunfig.toml.")
|
|
121
|
+
})
|
|
122
|
+
})
|
|
@@ -25,9 +25,10 @@ const testSvgs = [
|
|
|
25
25
|
const svgNames: string[] = []
|
|
26
26
|
for (let i = 0; i < testSvgs.length; i++) svgNames.push(`test${i + 1}`)
|
|
27
27
|
|
|
28
|
+
const testPathBase = path.basename(import.meta.path.replace(/\.test\.tsx?$/, ""))
|
|
28
29
|
const snapshotDir = path.join(__dirname, "__snapshots__")
|
|
29
30
|
const snapshotPaths = svgNames.map((svgName) =>
|
|
30
|
-
path.join(snapshotDir, `${svgName}.snap.svg`),
|
|
31
|
+
path.join(snapshotDir, `${testPathBase}-${svgName}.snap.svg`),
|
|
31
32
|
)
|
|
32
33
|
|
|
33
34
|
beforeAll(() => {
|
|
@@ -7,9 +7,13 @@ const testSvg = `<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg
|
|
|
7
7
|
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
|
|
8
8
|
</svg>`
|
|
9
9
|
|
|
10
|
+
const testPathBase = path.basename(import.meta.path.replace(/\.test\.tsx?$/, ""))
|
|
10
11
|
const snapshotDir = path.join(__dirname, "__snapshots__")
|
|
11
|
-
const snapshotPath = path.join(snapshotDir,
|
|
12
|
-
const metadataSnapshotPath = path.join(
|
|
12
|
+
const snapshotPath = path.join(snapshotDir, `${testPathBase}-test.snap.svg`)
|
|
13
|
+
const metadataSnapshotPath = path.join(
|
|
14
|
+
snapshotDir,
|
|
15
|
+
`${testPathBase}-metadata.snap.svg`,
|
|
16
|
+
)
|
|
13
17
|
|
|
14
18
|
beforeAll(() => {
|
|
15
19
|
if (!fs.existsSync(snapshotDir)) {
|