feat(core-eslint): add entity-must-have-test and no-relative-parent-import rules

Two CLAUDE.md conventions had no mechanical gate, so both drifted:
entity models shipped without sibling tests, and feature test files
imported src modules via `../` instead of the `@/` alias.

- `entity-must-have-test` — every entities/models/<x>.ts needs a sibling
  <x>.test.ts (errors and barrels excluded).
- `no-relative-parent-import-in-tests` — feature test files must import
  src via `@/`, not `../`. Scoped to feature packages; core packages are
  governed by their own generator templates.

Both register at warn level, bringing the conformance rule count to 15.
This commit is contained in:
2026-05-21 11:49:45 +02:00
parent c099d7182b
commit d3944f40db
7 changed files with 260 additions and 3 deletions

View File

@@ -0,0 +1,55 @@
import fs from "node:fs";
import path from "node:path";
/**
* Entity models (`entities/models/<x>.ts`) are pure domain logic — schemas,
* invariants, derivations. They are the cheapest layer to test and the most
* expensive to get wrong, so every model file must carry a sibling test.
*
* Scope is `entities/models/` only. Error classes (`entities/errors/`) are
* conventionally covered by a consolidated `errors.test.ts`, and barrels
* (`index.ts`) hold no logic — both are excluded.
*/
function isEntityModelFile(filename) {
const normalized = filename.replace(/\\/g, "/");
if (!normalized.includes("/entities/models/")) return false;
if (!normalized.endsWith(".ts")) return false;
if (normalized.endsWith(".test.ts")) return false;
if (normalized.endsWith(".d.ts")) return false;
if (normalized.endsWith("/index.ts")) return false;
return true;
}
/** @type {import("eslint").Rule.RuleModule} */
export default {
meta: {
type: "problem",
docs: {
description:
"Every entity model file (entities/models/<x>.ts) must have a sibling <x>.test.ts.",
},
schema: [],
messages: {
missingTest:
"Entity model {{filename}} has no sibling test at {{expected}}. Entity models are pure domain logic — cover them with a unit test.",
},
},
create(context) {
return {
Program(node) {
const filename = context.filename;
if (!isEntityModelFile(filename)) return;
const expected = filename.replace(/\.ts$/, ".test.ts");
if (fs.existsSync(expected)) return;
context.report({
node,
messageId: "missingTest",
data: {
filename: path.basename(filename),
expected: path.basename(expected),
},
});
},
};
},
};

View File

@@ -0,0 +1,74 @@
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 "./entity-must-have-test.js";
function makeModelsDir() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "emht-"));
const modelsDir = path.join(dir, "src", "entities", "models");
fs.mkdirSync(modelsDir, { recursive: true });
return modelsDir;
}
function makeEntityFixture({ withTest }) {
const modelsDir = makeModelsDir();
const entity = path.join(modelsDir, "cookie.ts");
fs.writeFileSync(entity, `export const cookie = {};`);
if (withTest) {
fs.writeFileSync(
path.join(modelsDir, "cookie.test.ts"),
`import { it } from "vitest"; it("works", () => {});`,
);
}
return { entity };
}
const tester = new RuleTester({
languageOptions: { ecmaVersion: "latest", sourceType: "module" },
});
describe("entity-must-have-test", () => {
it("passes when a sibling .test.ts exists", () => {
const { entity } = makeEntityFixture({ withTest: true });
tester.run("entity-must-have-test", rule, {
valid: [{ filename: entity, code: fs.readFileSync(entity, "utf8") }],
invalid: [],
});
});
it("fires when no sibling test file exists", () => {
const { entity } = makeEntityFixture({ withTest: false });
tester.run("entity-must-have-test", rule, {
valid: [],
invalid: [
{
filename: entity,
code: fs.readFileSync(entity, "utf8"),
errors: [{ messageId: "missingTest" }],
},
],
});
});
it("ignores files outside entities/models", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "emht-"));
const other = path.join(dir, "helper.ts");
fs.writeFileSync(other, `export const x = 1;`);
tester.run("entity-must-have-test", rule, {
valid: [{ filename: other, code: "export const x = 1;" }],
invalid: [],
});
});
it("ignores the index.ts barrel inside entities/models", () => {
const modelsDir = makeModelsDir();
const index = path.join(modelsDir, "index.ts");
fs.writeFileSync(index, `export {};`);
tester.run("entity-must-have-test", rule, {
valid: [{ filename: index, code: "export {};" }],
invalid: [],
});
});
});

View File

@@ -0,0 +1,49 @@
/**
* Feature test files import from `src/` through the `@/` alias, never via
* `../` parent traversal (CLAUDE.md "Key Conventions"). A `../` import in a
* test file is always reaching across `src/` directories — `@/` keeps those
* imports stable under file moves and makes the test's dependencies legible.
*
* Scoped to feature packages (`packages/<name>/src/`, excluding `core-*`):
* the convention is part of the feature template's contract. Core packages
* are generated and governed by their own templates, and tooling packages
* (turbo/generators, scripts) legitimately use relative paths.
*/
function isFeatureSrcTestFile(filename) {
const normalized = filename.replace(/\\/g, "/");
if (!normalized.endsWith(".test.ts") && !normalized.endsWith(".test.tsx")) {
return false;
}
return /\/packages\/(?!core-)[^/]+\/src\//.test(normalized);
}
/** @type {import("eslint").Rule.RuleModule} */
export default {
meta: {
type: "problem",
docs: {
description:
"Feature test files must import src modules via the @/ alias, not ../ parent paths.",
},
schema: [],
messages: {
relativeParentImport:
'Test file imports "{{source}}" with a ../ parent path. Use the "@/" alias for src imports (e.g. "@/application/...").',
},
},
create(context) {
if (!isFeatureSrcTestFile(context.filename)) return {};
return {
ImportDeclaration(node) {
const source = node.source.value;
if (typeof source === "string" && source.startsWith("../")) {
context.report({
node: node.source,
messageId: "relativeParentImport",
data: { source },
});
}
},
};
},
};

View File

@@ -0,0 +1,73 @@
import { describe, it } from "vitest";
import { RuleTester } from "eslint";
import rule from "./no-relative-parent-import-in-tests.js";
const tester = new RuleTester({
languageOptions: { ecmaVersion: "latest", sourceType: "module" },
});
const featureTest = "/repo/packages/auth/src/di/container.test.ts";
const featureSrc = "/repo/packages/auth/src/di/container.ts";
const coreTest = "/repo/packages/core-audit/src/di/bind-audit.test.ts";
describe("no-relative-parent-import-in-tests", () => {
it("passes when a feature test uses the @/ alias", () => {
tester.run("no-relative-parent-import-in-tests", rule, {
valid: [
{
filename: featureTest,
code: `import { x } from "@/infrastructure/x";`,
},
],
invalid: [],
});
});
it("passes for same-directory ./ imports", () => {
tester.run("no-relative-parent-import-in-tests", rule, {
valid: [
{ filename: featureTest, code: `import { x } from "./container";` },
],
invalid: [],
});
});
it("fires when a feature test imports via ../", () => {
tester.run("no-relative-parent-import-in-tests", rule, {
valid: [],
invalid: [
{
filename: featureTest,
code: `import { x } from "../infrastructure/x";`,
errors: [
{
messageId: "relativeParentImport",
data: { source: "../infrastructure/x" },
},
],
},
],
});
});
it("ignores ../ imports in non-test source files", () => {
tester.run("no-relative-parent-import-in-tests", rule, {
valid: [
{
filename: featureSrc,
code: `import { x } from "../infrastructure/x";`,
},
],
invalid: [],
});
});
it("ignores core-package test files (governed by their own templates)", () => {
tester.run("no-relative-parent-import-in-tests", rule, {
valid: [
{ filename: coreTest, code: `import { x } from "../noop-audit-log";` },
],
invalid: [],
});
});
});