From f22b60e23f7e0f9c44aabfd6deceab30e54dce3c Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Wed, 25 Feb 2026 22:47:19 +0000 Subject: [PATCH] Add test identification utilities for vitest examples --- examples/vitest-js/get-failed-test-ids.js | 22 ++++++++++++++++++++++ examples/vitest-js/get-test-ids.js | 21 +++++++++++++++++++++ examples/vitest-js/package.json | 4 +++- examples/vitest-js/src/wednesday.test.js | 6 ++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 examples/vitest-js/get-failed-test-ids.js create mode 100644 examples/vitest-js/get-test-ids.js create mode 100644 examples/vitest-js/src/wednesday.test.js diff --git a/examples/vitest-js/get-failed-test-ids.js b/examples/vitest-js/get-failed-test-ids.js new file mode 100644 index 00000000..f9556b55 --- /dev/null +++ b/examples/vitest-js/get-failed-test-ids.js @@ -0,0 +1,22 @@ +import { execSync } from "node:child_process"; +import { readFileSync } from "node:fs"; +import { createHash } from "node:crypto"; +import { relative } from "node:path"; + +let json; +try { + json = execSync("npx vitest run --reporter=json 2>/dev/null", { + encoding: "utf8", + }); +} catch (e) { + json = e.stdout; +} +const { testResults } = JSON.parse(json); + +for (const result of testResults) { + if (result.status !== "failed") continue; + const rel = relative(".", result.name); + const content = readFileSync(result.name, "utf8"); + const hash = createHash("sha256").update(content).digest("hex"); + console.log(`${rel} ${hash}`); +} diff --git a/examples/vitest-js/get-test-ids.js b/examples/vitest-js/get-test-ids.js new file mode 100644 index 00000000..74de6bb4 --- /dev/null +++ b/examples/vitest-js/get-test-ids.js @@ -0,0 +1,21 @@ +import { readFileSync, readdirSync, statSync } from "node:fs"; +import { createHash } from "node:crypto"; +import { join } from "node:path"; + +function walk(dir) { + const results = []; + for (const entry of readdirSync(dir)) { + if (entry === "node_modules") continue; + const full = join(dir, entry); + if (statSync(full).isDirectory()) results.push(...walk(full)); + else results.push(full); + } + return results; +} + +for (const file of walk(".")) { + const content = readFileSync(file, "utf8"); + if (!content.includes("vitest")) continue; + const hash = createHash("sha256").update(content).digest("hex"); + console.log(`${file} ${hash}`); +} diff --git a/examples/vitest-js/package.json b/examples/vitest-js/package.json index 6e0bb693..803f6032 100644 --- a/examples/vitest-js/package.json +++ b/examples/vitest-js/package.json @@ -3,7 +3,9 @@ "private": true, "type": "module", "scripts": { - "test": "vitest run" + "test": "vitest run", + "get-test-ids": "node get-test-ids.js", + "get-failed-test-ids": "node get-failed-test-ids.js" }, "devDependencies": { "vitest": "^3.0.0" diff --git a/examples/vitest-js/src/wednesday.test.js b/examples/vitest-js/src/wednesday.test.js new file mode 100644 index 00000000..26d36939 --- /dev/null +++ b/examples/vitest-js/src/wednesday.test.js @@ -0,0 +1,6 @@ +import { test, expect } from "vitest"; +import { childsDay } from "./index.js"; + +test("wednesday's child is full of woe", () => { + expect(childsDay(new Date("2026-02-25"))).toBe("WRONG VALUE"); +}); -- knotmirror 0.0.1