Skip to content

Commit

Permalink
snapshot testing
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Aug 28, 2024
1 parent 513a8a7 commit 89edddf
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
Binary file modified bun.lockb
Binary file not shown.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
"@tscircuit/soup": "^0.0.58",
"@types/bun": "latest",
"gerber-to-svg": "^4.2.8",
"tsup": "^8.2.4"
"tsup": "^8.2.4",
"bun-match-svg": "^0.0.1",
"pcb-stackup": "^4.2.8"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"bun-match-svg": "^0.0.1",
"fast-json-stable-stringify": "^2.1.0"
}
}
7 changes: 7 additions & 0 deletions tests/__snapshots__/simple1.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tests/__snapshots__/simple2.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion tests/fixtures/__snapshots__/simple1.snap.svg

This file was deleted.

27 changes: 18 additions & 9 deletions tests/fixtures/preload.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import "bun-match-svg"
import { expect } from "bun:test"
import gerberToSvg from "gerber-to-svg"
import pcbStackup from "pcb-stackup"
import { Readable } from "stream"

async function toMatchGerberSnapshot(
this: any,
gerberOutput: Record<string, string>,
testPathOriginal: string,
svgName?: string,
) {
const svg = await new Promise((resolve, reject) => {
gerberToSvg(gerberOutput.Edge_Cuts, {}, (err, svg) => {
if (err) return reject(err)
resolve(svg)
})
})
// Create layers array from gerberOutput
const layers = Object.entries(gerberOutput).map(([filename, content]) => ({
filename,
gerber: Readable.from(content),
}))

return expect(svg).toMatchSvgSnapshot(import.meta.path, svgName)
try {
const stackup = await pcbStackup(layers)

// We'll use the top layer SVG for comparison, but you could choose bottom or both
const svg = stackup.top.svg

return expect(svg).toMatchSvgSnapshot(testPathOriginal, svgName)
} catch (error) {
throw new Error(`Failed to generate PCB stackup: ${error}`)
}
}

expect.extend({
Expand All @@ -26,7 +35,7 @@ expect.extend({
declare module "bun:test" {
interface Matchers<T = unknown> {
toMatchGerberSnapshot(
testPath: string,
testImportMetaPath: string,
svgName?: string,
): Promise<MatcherResult>
}
Expand Down
7 changes: 5 additions & 2 deletions tests/generate-gerber-with-basic-elements.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from "bun:test"
import { test, expect } from "bun:test"
import { convertSoupToGerberCommands } from "src/convert-soup-to-gerber-commands"
import {
stringifyGerberCommandLayers,
Expand Down Expand Up @@ -77,6 +77,9 @@ test("Generate simple gerber with basic elements", async () => {
// console.log(stringifyGerberCommands(gerber_cmds.B_Mask))

// TODO parse gerber to check for correctness
const gerbers = stringifyGerberCommandLayers(gerber_cmds)

await maybeOutputGerber(stringifyGerberCommandLayers(gerber_cmds))
await maybeOutputGerber(gerbers)

expect(gerbers).toMatchGerberSnapshot(import.meta.path, "simple2")
})

0 comments on commit 89edddf

Please sign in to comment.