feat(core-eslint): component-must-have-test rule
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
53
packages/core-eslint/rules/component-must-have-test.js
Normal file
53
packages/core-eslint/rules/component-must-have-test.js
Normal 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),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
49
packages/core-eslint/rules/component-must-have-test.test.js
Normal file
49
packages/core-eslint/rules/component-must-have-test.test.js
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import { describe, it } from "vitest";
|
||||||
|
import { RuleTester } from "eslint";
|
||||||
|
import path from "node:path";
|
||||||
|
import os from "node:os";
|
||||||
|
import fs from "node:fs";
|
||||||
|
import rule from "./component-must-have-test.js";
|
||||||
|
|
||||||
|
function makeFixture({ withTest }) {
|
||||||
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "cmht-"));
|
||||||
|
const dir = path.join(root, "packages", "core-ui", "src", "atoms", "Button");
|
||||||
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
|
const component = path.join(dir, "Button.tsx");
|
||||||
|
fs.writeFileSync(component, `export const Button = () => <button>x</button>;`);
|
||||||
|
if (withTest) {
|
||||||
|
fs.writeFileSync(path.join(dir, "Button.test.tsx"), `import { it } from "vitest"; it("works", () => {});`);
|
||||||
|
}
|
||||||
|
return { component };
|
||||||
|
}
|
||||||
|
|
||||||
|
const tester = new RuleTester({
|
||||||
|
languageOptions: {
|
||||||
|
parser: await import("@typescript-eslint/parser"),
|
||||||
|
ecmaVersion: "latest",
|
||||||
|
sourceType: "module",
|
||||||
|
parserOptions: { ecmaFeatures: { jsx: true } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("component-must-have-test", () => {
|
||||||
|
it("passes when sibling .test.tsx exists", () => {
|
||||||
|
const { component } = makeFixture({ withTest: true });
|
||||||
|
tester.run("component-must-have-test", rule, {
|
||||||
|
valid: [{ filename: component, code: fs.readFileSync(component, "utf8") }],
|
||||||
|
invalid: [],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fires when no sibling .test.tsx exists", () => {
|
||||||
|
const { component } = makeFixture({ withTest: false });
|
||||||
|
tester.run("component-must-have-test", rule, {
|
||||||
|
valid: [],
|
||||||
|
invalid: [{
|
||||||
|
filename: component,
|
||||||
|
code: fs.readFileSync(component, "utf8"),
|
||||||
|
errors: [{ messageId: "missingTest" }],
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user