feat(core-eslint): component-must-have-test rule

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-13 07:40:40 +02:00
parent 9ef545b714
commit f933ca74ff
2 changed files with 102 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
import fs from "node:fs";
import path from "node:path";
function isComponentFile(filename) {
if (!filename.endsWith(".tsx")) return false;
const base = path.basename(filename);
if (
base === "index.tsx" ||
base.endsWith(".test.tsx") ||
base.endsWith(".stories.tsx") ||
base.endsWith(".spec.tsx")
) {
return false;
}
return (
filename.includes("/packages/core-ui/src/") ||
/\/packages\/[^/]+\/src\/ui\//.test(filename)
);
}
/** @type {import("eslint").Rule.RuleModule} */
export default {
meta: {
type: "problem",
docs: {
description:
"Every component file must have a sibling *.test.tsx for behavioural coverage.",
},
schema: [],
messages: {
missingTest:
"Component {{filename}} has no sibling test at {{expected}}. Write the red test first.",
},
},
create(context) {
return {
Program(node) {
const filename = context.filename;
if (!isComponentFile(filename)) return;
const expected = filename.replace(/\.tsx$/, ".test.tsx");
if (fs.existsSync(expected)) return;
context.report({
node,
messageId: "missingTest",
data: {
filename: path.basename(filename),
expected: path.basename(expected),
},
});
},
};
},
};