graphics-debug 0.0.44 → 0.0.45
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/README.md +33 -12
- package/dist/lib/matcher.d.ts +4 -1
- package/dist/lib/matcher.js +5 -3
- package/dist/lib/matcher.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,7 +30,6 @@ Don't have access to the cli? Paste into the online version: https://graphicsdeb
|
|
|
30
30
|
|
|
31
31
|
https://github.com/user-attachments/assets/9f3f41e6-b0fe-416a-a551-ba5c5b920cad
|
|
32
32
|
|
|
33
|
-
|
|
34
33
|
## Format
|
|
35
34
|
|
|
36
35
|
The `graphics` json object is very simple, here's the basic schema:
|
|
@@ -167,9 +166,20 @@ import { getSvgFromGraphicsObject } from "graphics-debug"
|
|
|
167
166
|
|
|
168
167
|
// Create your graphics object
|
|
169
168
|
const graphicsObject = {
|
|
170
|
-
points: [
|
|
171
|
-
|
|
172
|
-
|
|
169
|
+
points: [
|
|
170
|
+
{ x: 0, y: 0, label: "Origin" },
|
|
171
|
+
{ x: 100, y: 100, color: "red" },
|
|
172
|
+
],
|
|
173
|
+
lines: [
|
|
174
|
+
{
|
|
175
|
+
points: [
|
|
176
|
+
{ x: 0, y: 0 },
|
|
177
|
+
{ x: 100, y: 100 },
|
|
178
|
+
],
|
|
179
|
+
strokeColor: "blue",
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
title: "My Graph",
|
|
173
183
|
}
|
|
174
184
|
|
|
175
185
|
// Generate SVG string directly from the object
|
|
@@ -192,22 +202,35 @@ Then use the matcher in your tests:
|
|
|
192
202
|
```tsx
|
|
193
203
|
import { expect, test } from "bun:test"
|
|
194
204
|
import "graphics-debug/matcher"
|
|
195
|
-
import type { GraphicsObject } from "graphics-debug"
|
|
205
|
+
import type { GraphicsObject } from "graphics-debug"
|
|
196
206
|
|
|
197
207
|
// Your test graphics object
|
|
198
208
|
const graphicsObject: GraphicsObject = {
|
|
199
|
-
points: [
|
|
200
|
-
|
|
201
|
-
|
|
209
|
+
points: [
|
|
210
|
+
{ x: 0, y: 0, label: "Origin" },
|
|
211
|
+
{ x: 100, y: 100, color: "red" },
|
|
212
|
+
],
|
|
213
|
+
lines: [
|
|
214
|
+
{
|
|
215
|
+
points: [
|
|
216
|
+
{ x: 0, y: 0 },
|
|
217
|
+
{ x: 100, y: 100 },
|
|
218
|
+
],
|
|
219
|
+
strokeColor: "blue",
|
|
220
|
+
},
|
|
221
|
+
],
|
|
222
|
+
title: "My Test Graphics",
|
|
202
223
|
}
|
|
203
224
|
|
|
204
225
|
test("should match the expected visualization", async () => {
|
|
205
226
|
// First run creates the snapshot
|
|
206
227
|
// Subsequent runs will compare against saved snapshot
|
|
207
228
|
await expect(graphicsObject).toMatchGraphicsSvg(import.meta.path)
|
|
208
|
-
|
|
229
|
+
|
|
209
230
|
// You can also provide a custom name for the snapshot:
|
|
210
|
-
await expect(graphicsObject).toMatchGraphicsSvg(import.meta.path,
|
|
231
|
+
await expect(graphicsObject).toMatchGraphicsSvg(import.meta.path, {
|
|
232
|
+
svgName: "custom-name",
|
|
233
|
+
})
|
|
211
234
|
})
|
|
212
235
|
```
|
|
213
236
|
|
|
@@ -250,5 +273,3 @@ Here is the content of the `exampleGraphics.json` file:
|
|
|
250
273
|
```
|
|
251
274
|
|
|
252
275
|
You can load this example into the application to visualize the graphics objects and understand how the `graphics-debug` module works.
|
|
253
|
-
|
|
254
|
-
|
package/dist/lib/matcher.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
declare module "bun:test" {
|
|
2
2
|
interface Matchers<T = unknown> {
|
|
3
|
-
toMatchGraphicsSvg(testPath: string,
|
|
3
|
+
toMatchGraphicsSvg(testPath: string, opts?: {
|
|
4
|
+
backgroundColor?: string;
|
|
5
|
+
svgName?: string;
|
|
6
|
+
}): Promise<MatcherResult>;
|
|
4
7
|
}
|
|
5
8
|
}
|
package/dist/lib/matcher.js
CHANGED
|
@@ -7,16 +7,18 @@ import { expect } from "bun:test";
|
|
|
7
7
|
import * as fs from "node:fs";
|
|
8
8
|
import * as path from "node:path";
|
|
9
9
|
import looksSame from "looks-same";
|
|
10
|
-
async function toMatchGraphicsSvg(receivedMaybePromise, testPathOriginal,
|
|
10
|
+
async function toMatchGraphicsSvg(receivedMaybePromise, testPathOriginal, opts = {}) {
|
|
11
11
|
const received = await receivedMaybePromise;
|
|
12
12
|
const testPath = testPathOriginal.replace(/\.test\.tsx?$/, "");
|
|
13
13
|
const snapshotDir = path.join(path.dirname(testPath), "__snapshots__");
|
|
14
|
-
const snapshotName = svgName ? `${svgName}.snap.svg` : `${path.basename(testPath)}.snap.svg`;
|
|
14
|
+
const snapshotName = opts.svgName ? `${opts.svgName}.snap.svg` : `${path.basename(testPath)}.snap.svg`;
|
|
15
15
|
const filePath = path.join(snapshotDir, snapshotName);
|
|
16
16
|
if (!fs.existsSync(snapshotDir)) {
|
|
17
17
|
fs.mkdirSync(snapshotDir, { recursive: true });
|
|
18
18
|
}
|
|
19
|
-
const receivedSvg = getSvgFromGraphicsObject(received
|
|
19
|
+
const receivedSvg = getSvgFromGraphicsObject(received, {
|
|
20
|
+
backgroundColor: opts.backgroundColor
|
|
21
|
+
});
|
|
20
22
|
const updateSnapshot = process.argv.includes("--update-snapshots") || process.argv.includes("-u") || Boolean(process.env["BUN_UPDATE_SNAPSHOTS"]);
|
|
21
23
|
if (!fs.existsSync(filePath) || updateSnapshot) {
|
|
22
24
|
console.log("Writing snapshot to", filePath);
|
package/dist/lib/matcher.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../lib/matcher.ts"],"sourcesContent":["import { expect, type MatcherResult } from \"bun:test\"\nimport * as fs from \"node:fs\"\nimport * as path from \"node:path\"\nimport looksSame from \"looks-same\"\nimport { GraphicsObject } from \"./types\"\nimport { getSvgFromGraphicsObject } from \"./getSvgFromGraphicsObject\"\n\n/**\n * Custom matcher for Bun tests to compare GraphicsObjects as SVGs\n *\n * @param this Matcher context\n * @param receivedMaybePromise GraphicsObject or Promise<GraphicsObject> to test\n * @param testPathOriginal Path to the test file\n * @param svgName Optional name for the snapshot\n * @returns Matcher result\n */\nasync function toMatchGraphicsSvg(\n // biome-ignore lint/suspicious/noExplicitAny: bun doesn't expose matcher type\n this: any,\n receivedMaybePromise: GraphicsObject | Promise<GraphicsObject>,\n testPathOriginal: string,\n svgName?: string,\n): Promise<MatcherResult> {\n const received = await receivedMaybePromise\n const testPath = testPathOriginal.replace(/\\.test\\.tsx?$/, \"\")\n const snapshotDir = path.join(path.dirname(testPath), \"__snapshots__\")\n const snapshotName = svgName\n ? `${svgName}.snap.svg`\n : `${path.basename(testPath)}.snap.svg`\n const filePath = path.join(snapshotDir, snapshotName)\n\n if (!fs.existsSync(snapshotDir)) {\n fs.mkdirSync(snapshotDir, { recursive: true })\n }\n\n // Convert GraphicsObject to SVG\n const receivedSvg = getSvgFromGraphicsObject(received)\n\n const updateSnapshot =\n process.argv.includes(\"--update-snapshots\") ||\n process.argv.includes(\"-u\") ||\n Boolean(process.env[\"BUN_UPDATE_SNAPSHOTS\"])\n\n if (!fs.existsSync(filePath) || updateSnapshot) {\n console.log(\"Writing snapshot to\", filePath)\n fs.writeFileSync(filePath, receivedSvg)\n return {\n message: () => `Snapshot created at ${filePath}`,\n pass: true,\n }\n }\n\n const existingSnapshot = fs.readFileSync(filePath, \"utf-8\")\n\n const result: any = await looksSame(\n Buffer.from(receivedSvg),\n Buffer.from(existingSnapshot),\n {\n strict: false,\n tolerance: 2,\n },\n )\n\n if (result.equal) {\n return {\n message: () => \"Snapshot matches\",\n pass: true,\n }\n }\n\n const diffPath = filePath.replace(\".snap.svg\", \".diff.png\")\n await looksSame.createDiff({\n reference: Buffer.from(existingSnapshot),\n current: Buffer.from(receivedSvg),\n diff: diffPath,\n highlightColor: \"#ff00ff\",\n })\n\n return {\n message: () => `Snapshot does not match. Diff saved at ${diffPath}`,\n pass: false,\n }\n}\n\n// Add the custom matcher to the expect object\nexpect.extend({\n // biome-ignore lint/suspicious/noExplicitAny: bun's types don't expose matcher type\n toMatchGraphicsSvg: toMatchGraphicsSvg as any,\n})\n\n// Extend the TypeScript interface for the matcher\ndeclare module \"bun:test\" {\n interface Matchers<T = unknown> {\n toMatchGraphicsSvg(\n testPath: string,\n svgName?: string,\n ): Promise<MatcherResult>\n }\n}\n"],"mappings":";;;;;AAAA,SAAS,cAAkC;AAC3C,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,OAAO,eAAe;AAatB,eAAe,mBAGb,sBACA,kBACA,
|
|
1
|
+
{"version":3,"sources":["../../lib/matcher.ts"],"sourcesContent":["import { expect, type MatcherResult } from \"bun:test\"\nimport * as fs from \"node:fs\"\nimport * as path from \"node:path\"\nimport looksSame from \"looks-same\"\nimport { GraphicsObject } from \"./types\"\nimport { getSvgFromGraphicsObject } from \"./getSvgFromGraphicsObject\"\n\n/**\n * Custom matcher for Bun tests to compare GraphicsObjects as SVGs\n *\n * @param this Matcher context\n * @param receivedMaybePromise GraphicsObject or Promise<GraphicsObject> to test\n * @param testPathOriginal Path to the test file\n * @param svgName Optional name for the snapshot\n * @returns Matcher result\n */\nasync function toMatchGraphicsSvg(\n // biome-ignore lint/suspicious/noExplicitAny: bun doesn't expose matcher type\n this: any,\n receivedMaybePromise: GraphicsObject | Promise<GraphicsObject>,\n testPathOriginal: string,\n opts: { backgroundColor?: string; svgName?: string } = {},\n): Promise<MatcherResult> {\n const received = await receivedMaybePromise\n const testPath = testPathOriginal.replace(/\\.test\\.tsx?$/, \"\")\n const snapshotDir = path.join(path.dirname(testPath), \"__snapshots__\")\n const snapshotName = opts.svgName\n ? `${opts.svgName}.snap.svg`\n : `${path.basename(testPath)}.snap.svg`\n const filePath = path.join(snapshotDir, snapshotName)\n\n if (!fs.existsSync(snapshotDir)) {\n fs.mkdirSync(snapshotDir, { recursive: true })\n }\n\n // Convert GraphicsObject to SVG\n const receivedSvg = getSvgFromGraphicsObject(received, {\n backgroundColor: opts.backgroundColor,\n })\n\n const updateSnapshot =\n process.argv.includes(\"--update-snapshots\") ||\n process.argv.includes(\"-u\") ||\n Boolean(process.env[\"BUN_UPDATE_SNAPSHOTS\"])\n\n if (!fs.existsSync(filePath) || updateSnapshot) {\n console.log(\"Writing snapshot to\", filePath)\n fs.writeFileSync(filePath, receivedSvg)\n return {\n message: () => `Snapshot created at ${filePath}`,\n pass: true,\n }\n }\n\n const existingSnapshot = fs.readFileSync(filePath, \"utf-8\")\n\n const result: any = await looksSame(\n Buffer.from(receivedSvg),\n Buffer.from(existingSnapshot),\n {\n strict: false,\n tolerance: 2,\n },\n )\n\n if (result.equal) {\n return {\n message: () => \"Snapshot matches\",\n pass: true,\n }\n }\n\n const diffPath = filePath.replace(\".snap.svg\", \".diff.png\")\n await looksSame.createDiff({\n reference: Buffer.from(existingSnapshot),\n current: Buffer.from(receivedSvg),\n diff: diffPath,\n highlightColor: \"#ff00ff\",\n })\n\n return {\n message: () => `Snapshot does not match. Diff saved at ${diffPath}`,\n pass: false,\n }\n}\n\n// Add the custom matcher to the expect object\nexpect.extend({\n // biome-ignore lint/suspicious/noExplicitAny: bun's types don't expose matcher type\n toMatchGraphicsSvg: toMatchGraphicsSvg as any,\n})\n\n// Extend the TypeScript interface for the matcher\ndeclare module \"bun:test\" {\n interface Matchers<T = unknown> {\n toMatchGraphicsSvg(\n testPath: string,\n opts?: { backgroundColor?: string; svgName?: string },\n ): Promise<MatcherResult>\n }\n}\n"],"mappings":";;;;;AAAA,SAAS,cAAkC;AAC3C,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,OAAO,eAAe;AAatB,eAAe,mBAGb,sBACA,kBACA,OAAuD,CAAC,GAChC;AACxB,QAAM,WAAW,MAAM;AACvB,QAAM,WAAW,iBAAiB,QAAQ,iBAAiB,EAAE;AAC7D,QAAM,cAAmB,UAAU,aAAQ,QAAQ,GAAG,eAAe;AACrE,QAAM,eAAe,KAAK,UACtB,GAAG,KAAK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