67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, it } from "vitest";
|
|
|
|
import { evaluateTargets, formatSummary, walk } from "./verify-test-density.mjs";
|
|
|
|
const tempRoots = [];
|
|
|
|
function makeRoot() {
|
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "verify-test-density-"));
|
|
tempRoots.push(root);
|
|
return root;
|
|
}
|
|
|
|
function writeFile(root, filePath, contents = "export {};") {
|
|
const fullPath = path.join(root, filePath);
|
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
fs.writeFileSync(fullPath, contents, "utf8");
|
|
}
|
|
|
|
afterEach(() => {
|
|
for (const root of tempRoots.splice(0)) {
|
|
fs.rmSync(root, { force: true, recursive: true });
|
|
}
|
|
});
|
|
|
|
describe("verify-test-density", () => {
|
|
it("counts source and test files while ignoring generated and declaration files", () => {
|
|
const root = makeRoot();
|
|
writeFile(root, "src/index.ts");
|
|
writeFile(root, "src/component.tsx");
|
|
writeFile(root, "src/component.test.tsx");
|
|
writeFile(root, "src/types.d.ts");
|
|
writeFile(root, "src/generated/client.ts");
|
|
writeFile(root, "src/.react-router/types.ts");
|
|
writeFile(root, "src/node_modules/pkg/source.ts");
|
|
|
|
assert.deepEqual(walk(path.join(root, "src")), { source: 2, test: 1 });
|
|
});
|
|
|
|
it("passes, fails, and skips targets using stable report lines", () => {
|
|
const root = makeRoot();
|
|
writeFile(root, "well/a.ts");
|
|
writeFile(root, "well/a.test.ts");
|
|
writeFile(root, "weak/a.ts");
|
|
writeFile(root, "weak/b.ts");
|
|
|
|
const result = evaluateTargets(
|
|
[
|
|
{ name: "well-covered", root: "well", minRatio: 0.5 },
|
|
{ name: "under-covered", root: "weak", minRatio: 0.5 },
|
|
{ name: "empty", root: "missing", minRatio: 0.5 },
|
|
],
|
|
root
|
|
);
|
|
|
|
assert.deepEqual(result.passes, [
|
|
"PASS well-covered source=1 tests=1 ratio=1.000 min=0.500",
|
|
"SKIP empty no source files",
|
|
]);
|
|
assert.deepEqual(result.failures, ["FAIL under-covered source=2 tests=0 ratio=0.000 min=0.500 (drift=-0.500)"]);
|
|
assert.equal(formatSummary(result), "Summary: 2 passed, 0 observed, 1 failed");
|
|
});
|
|
});
|