vitest-suites.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { readFileSync } from "node:fs";
  2. import path from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. const suitesPath = "test/vitest-suites.json";
  5. export function readSuites(root) {
  6. return JSON.parse(readFileSync(path.join(root, suitesPath), "utf8")).suites;
  7. }
  8. export function suiteFiles(root, name) {
  9. const suites = readSuites(root);
  10. const files = suites[name];
  11. if (!files)
  12. throw new Error(
  13. `Unknown Vitest suite "${name}"; ${suitesPath} declares ${Object.keys(suites).join(", ")}`,
  14. );
  15. return files;
  16. }
  17. export function allSuiteFiles(root) {
  18. return Object.values(readSuites(root)).flat();
  19. }
  20. export const repositoryRoot = fileURLToPath(new URL("../../", import.meta.url));
  21. // Use the source inventory rather than Git so exported source archives enforce
  22. // the same coverage contract. Vendored suites and repository node:test gates
  23. // are deliberately outside the first-party Vitest discovery boundary.
  24. export function suiteInventoryViolations(files, suites) {
  25. const actual = new Set(files);
  26. const declared = Object.values(suites).flat();
  27. const registered = new Set();
  28. const violations = [];
  29. for (const file of declared) {
  30. if (registered.has(file))
  31. violations.push(`${file}: duplicate Vitest registration`);
  32. registered.add(file);
  33. if (!actual.has(file))
  34. violations.push(`${file}: registered Vitest file is missing`);
  35. }
  36. for (const file of files) {
  37. if (
  38. file.startsWith("packages/") &&
  39. /\.(?:test|spec)\.[cm]?[jt]sx?$/u.test(file) &&
  40. !registered.has(file)
  41. )
  42. violations.push(
  43. `${file}: first-party test is absent from test/vitest-suites.json`,
  44. );
  45. }
  46. return violations;
  47. }