eslint-plugin-traceability 1.8.3 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,9 +1,9 @@
1
- ## [1.8.3](https://github.com/voder-ai/eslint-plugin-traceability/compare/v1.8.2...v1.8.3) (2025-12-04)
1
+ # [1.9.0](https://github.com/voder-ai/eslint-plugin-traceability/compare/v1.8.3...v1.9.0) (2025-12-04)
2
2
 
3
3
 
4
- ### Bug Fixes
4
+ ### Features
5
5
 
6
- * avoid running husky in consumers and repair smoke test ([849dbc2](https://github.com/voder-ai/eslint-plugin-traceability/commit/849dbc2d5a8f80890bbee94e1c4f2a6e15068b6d))
6
+ * add require-test-traceability rule for test files ([1eca595](https://github.com/voder-ai/eslint-plugin-traceability/commit/1eca595d7c1b12b224c3b7a647405c0bff7e7346))
7
7
 
8
8
  # Changelog
9
9
 
@@ -14,7 +14,7 @@ import { detectStaleAnnotations, updateAnnotationReferences, batchUpdateAnnotati
14
14
  * @story docs/stories/002.0-DYNAMIC-RULE-LOADING.story.md
15
15
  * @req REQ-RULE-LIST - Enumerate supported rule file names for plugin discovery
16
16
  */
17
- declare const RULE_NAMES: readonly ["require-story-annotation", "require-req-annotation", "require-branch-annotation", "valid-annotation-format", "valid-story-reference", "valid-req-reference", "prefer-implements-annotation"];
17
+ declare const RULE_NAMES: readonly ["require-story-annotation", "require-req-annotation", "require-branch-annotation", "valid-annotation-format", "valid-story-reference", "valid-req-reference", "prefer-implements-annotation", "require-test-traceability"];
18
18
  type RuleName = (typeof RULE_NAMES)[number];
19
19
  declare const rules: Record<RuleName, Rule.RuleModule>;
20
20
  declare const plugin: {
package/lib/src/index.js CHANGED
@@ -18,6 +18,7 @@ const RULE_NAMES = [
18
18
  "valid-story-reference",
19
19
  "valid-req-reference",
20
20
  "prefer-implements-annotation",
21
+ "require-test-traceability",
21
22
  ];
22
23
  const rules = {};
23
24
  exports.rules = rules;
@@ -89,6 +90,7 @@ const TRACEABILITY_RULE_SEVERITIES = {
89
90
  "traceability/valid-annotation-format": "warn",
90
91
  "traceability/valid-story-reference": "error",
91
92
  "traceability/valid-req-reference": "error",
93
+ "traceability/require-test-traceability": "error",
92
94
  };
93
95
  /**
94
96
  * @story docs/stories/007.0-DEV-ERROR-REPORTING.story.md
@@ -0,0 +1,21 @@
1
+ import type { Rule } from "eslint";
2
+ /**
3
+ * Enforce traceability conventions in test files.
4
+ *
5
+ * This rule validates that:
6
+ * - Test files have a file-level @supports annotation listing tested requirements.
7
+ * - describe()/it()/test()/context() blocks include story and requirement references
8
+ * following project conventions.
9
+ *
10
+ * @story docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md
11
+ * @req REQ-TEST-FILE-SUPPORTS
12
+ * @req REQ-TEST-DESCRIBE-STORY
13
+ * @req REQ-TEST-IT-REQ-PREFIX
14
+ * @req REQ-TEST-SUPPORTS-VALID
15
+ * @req REQ-TEST-PATTERN-DETECT
16
+ * @req REQ-TEST-FRAMEWORK-COMPAT
17
+ * @req REQ-TEST-NESTED-DESCRIBE
18
+ * @req REQ-TEST-ERROR-CONTEXT
19
+ */
20
+ declare const rule: Rule.RuleModule;
21
+ export default rule;
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Determine if a file should be treated as a test file based on patterns.
5
+ *
6
+ * @supports docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md REQ-TEST-PATTERN-DETECT
7
+ */
8
+ function determineIsTestFile(filename, rawPatterns = [
9
+ "/tests/",
10
+ "/test/",
11
+ "/__tests__/",
12
+ ".test.",
13
+ ".spec.",
14
+ ]) {
15
+ return rawPatterns.some((pattern) => filename.includes(pattern.replace("**", "")));
16
+ }
17
+ /**
18
+ * Ensure the file has a @supports annotation listing tested requirements.
19
+ *
20
+ * @supports docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md REQ-TEST-FILE-SUPPORTS REQ-TEST-SUPPORTS-VALID
21
+ */
22
+ function ensureFileSupportsAnnotation(context, sourceCode) {
23
+ const fileComments = sourceCode.getAllComments() || [];
24
+ const fileHasSupports = fileComments.some((comment) => /@supports\b/.test(comment.value || ""));
25
+ if (!fileHasSupports) {
26
+ const node = fileComments[0] || (sourceCode.ast && sourceCode.ast);
27
+ context.report({
28
+ node: node,
29
+ messageId: "missingFileSupports",
30
+ });
31
+ }
32
+ }
33
+ /**
34
+ * Check if a callee name corresponds to a test framework function.
35
+ *
36
+ * @supports docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md REQ-TEST-FRAMEWORK-COMPAT
37
+ */
38
+ function isTestCallName(name) {
39
+ return ["describe", "it", "test", "context"].includes(name);
40
+ }
41
+ function getCalleeName(node) {
42
+ if (node.callee.type === "Identifier") {
43
+ return node.callee.name;
44
+ }
45
+ if (node.callee.type === "MemberExpression" &&
46
+ node.callee.object.type === "Identifier") {
47
+ return node.callee.object.name;
48
+ }
49
+ return null;
50
+ }
51
+ function getFirstArgumentLiteral(node) {
52
+ const arg = node.arguments && node.arguments[0];
53
+ if (!arg)
54
+ return null;
55
+ if (arg.type === "Literal" && typeof arg.value === "string") {
56
+ return arg.value;
57
+ }
58
+ return null;
59
+ }
60
+ /**
61
+ * Enforce traceability conventions in test files.
62
+ *
63
+ * This rule validates that:
64
+ * - Test files have a file-level @supports annotation listing tested requirements.
65
+ * - describe()/it()/test()/context() blocks include story and requirement references
66
+ * following project conventions.
67
+ *
68
+ * @story docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md
69
+ * @req REQ-TEST-FILE-SUPPORTS
70
+ * @req REQ-TEST-DESCRIBE-STORY
71
+ * @req REQ-TEST-IT-REQ-PREFIX
72
+ * @req REQ-TEST-SUPPORTS-VALID
73
+ * @req REQ-TEST-PATTERN-DETECT
74
+ * @req REQ-TEST-FRAMEWORK-COMPAT
75
+ * @req REQ-TEST-NESTED-DESCRIBE
76
+ * @req REQ-TEST-ERROR-CONTEXT
77
+ */
78
+ const rule = {
79
+ meta: {
80
+ type: "problem",
81
+ docs: {
82
+ description: "Enforce traceability annotations and naming conventions in test files",
83
+ recommended: "error",
84
+ },
85
+ schema: [
86
+ {
87
+ type: "object",
88
+ properties: {
89
+ testFilePatterns: {
90
+ type: "array",
91
+ items: { type: "string" },
92
+ default: [
93
+ "**/tests/**/*.test.{js,ts}",
94
+ "**/tests/**/*.spec.{js,ts}",
95
+ "**/__tests__/**/*.{js,ts}",
96
+ "**/*.{test,spec}.{js,ts}",
97
+ ],
98
+ },
99
+ requireDescribeStory: {
100
+ type: "boolean",
101
+ default: true,
102
+ },
103
+ requireTestReqPrefix: {
104
+ type: "boolean",
105
+ default: true,
106
+ },
107
+ describePattern: {
108
+ type: "string",
109
+ default: "Story [0-9]+\\.[0-9]+-",
110
+ },
111
+ },
112
+ additionalProperties: false,
113
+ },
114
+ ],
115
+ messages: {
116
+ missingFileSupports: "Test file must have @supports annotation listing tested requirements.",
117
+ missingDescribeStory: "describe() block should reference story (e.g., 'Story 009.0-DEV-...').",
118
+ missingReqPrefix: "Test name should start with requirement ID (e.g., '[REQ-MAINT-DETECT] ...').",
119
+ },
120
+ },
121
+ create(context) {
122
+ const filename = context.getFilename();
123
+ const options = (context.options && context.options[0]) || {};
124
+ const { testFilePatterns = [
125
+ "/tests/",
126
+ "/test/",
127
+ "/__tests__/",
128
+ ".test.",
129
+ ".spec.",
130
+ ], requireDescribeStory = true, requireTestReqPrefix = true, describePattern = "Story [0-9]+\\.[0-9]+-", } = options;
131
+ const isTestFile = determineIsTestFile(filename, testFilePatterns);
132
+ if (!isTestFile) {
133
+ return {};
134
+ }
135
+ const sourceCode = context.getSourceCode();
136
+ ensureFileSupportsAnnotation(context, sourceCode);
137
+ const describeRegex = new RegExp(describePattern);
138
+ return {
139
+ // @supports docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md REQ-TEST-DESCRIBE-STORY REQ-TEST-IT-REQ-PREFIX REQ-TEST-NESTED-DESCRIBE REQ-TEST-ERROR-CONTEXT
140
+ CallExpression(node) {
141
+ const calleeName = getCalleeName(node);
142
+ if (!calleeName || !isTestCallName(calleeName)) {
143
+ return;
144
+ }
145
+ const description = getFirstArgumentLiteral(node);
146
+ if (!description)
147
+ return;
148
+ if (requireDescribeStory && calleeName === "describe") {
149
+ if (!describeRegex.test(description)) {
150
+ context.report({
151
+ node: node,
152
+ messageId: "missingDescribeStory",
153
+ });
154
+ }
155
+ }
156
+ if (requireTestReqPrefix &&
157
+ (calleeName === "it" || calleeName === "test")) {
158
+ if (!/^\[REQ-[^\]]+]/.test(description)) {
159
+ context.report({
160
+ node: node,
161
+ messageId: "missingReqPrefix",
162
+ });
163
+ }
164
+ }
165
+ },
166
+ };
167
+ },
168
+ };
169
+ exports.default = rule;
@@ -56,6 +56,7 @@ describe("Plugin Default Export and Configs (Story 001.0-DEV-PLUGIN-SETUP)", ()
56
56
  "valid-story-reference",
57
57
  "valid-req-reference",
58
58
  "prefer-implements-annotation",
59
+ "require-test-traceability",
59
60
  ];
60
61
  // Act: get actual rule names from plugin
61
62
  const actual = Object.keys(index_1.rules);
@@ -80,6 +81,7 @@ describe("Plugin Default Export and Configs (Story 001.0-DEV-PLUGIN-SETUP)", ()
80
81
  expect(recommendedRules).toHaveProperty("traceability/require-branch-annotation", "error");
81
82
  expect(recommendedRules).toHaveProperty("traceability/valid-story-reference", "error");
82
83
  expect(recommendedRules).toHaveProperty("traceability/valid-req-reference", "error");
84
+ expect(recommendedRules).toHaveProperty("traceability/require-test-traceability", "error");
83
85
  });
84
86
  it("[REQ-ERROR-SEVERITY] configs.strict uses same severity mapping as recommended", () => {
85
87
  const strictRules = index_1.configs.strict[0].rules;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ /**
7
+ * Tests for: docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md
8
+ * @supports docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md REQ-TEST-FILE-SUPPORTS REQ-TEST-DESCRIBE-STORY REQ-TEST-IT-REQ-PREFIX REQ-TEST-FRAMEWORK-COMPAT REQ-TEST-PATTERN-DETECT
9
+ */
10
+ const eslint_1 = require("eslint");
11
+ const require_test_traceability_1 = __importDefault(require("../../src/rules/require-test-traceability"));
12
+ const ruleTester = new eslint_1.RuleTester({
13
+ languageOptions: {
14
+ parserOptions: { ecmaVersion: 2020, sourceType: "module" },
15
+ },
16
+ });
17
+ describe("require-test-traceability rule (Story 020.0-DEV-TEST-ANNOTATION-VALIDATION)", () => {
18
+ ruleTester.run("require-test-traceability", require_test_traceability_1.default, {
19
+ valid: [
20
+ {
21
+ // [REQ-TEST-FILE-SUPPORTS] file-level @supports present and describe/test satisfied
22
+ code: `/**\n * @supports docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md REQ-TEST-FILE-SUPPORTS\n */\ndescribe('Story 020.0-DEV-TEST-ANNOTATION-VALIDATION', () => { it('[REQ-EXAMPLE] does something', () => {}); });`,
23
+ filename: "tests/rules/require-test-traceability.test.ts",
24
+ },
25
+ {
26
+ // [REQ-TEST-FRAMEWORK-COMPAT] mocha style `context` is treated as a test call but only name checks apply
27
+ code: `/**\n * @supports docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md REQ-TEST-FRAMEWORK-COMPAT\n */\ncontext('Story 020.0-DEV-TEST-ANNOTATION-VALIDATION', () => {});`,
28
+ filename: "tests/some/context.test.ts",
29
+ },
30
+ {
31
+ // Ensure non-test files are ignored (REQ-TEST-PATTERN-DETECT)
32
+ code: `/**\n * @supports docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md REQ-TEST-PATTERN-DETECT\n */\ndescribe('Story 020.0-DEV-TEST-ANNOTATION-VALIDATION', () => {});`,
33
+ filename: "src/not-a-test-file.ts",
34
+ },
35
+ ],
36
+ invalid: [
37
+ {
38
+ // [REQ-TEST-FILE-SUPPORTS] missing @supports in test file
39
+ code: `describe('Story 020.0-DEV-TEST-ANNOTATION-VALIDATION', () => { it('[REQ-ONE] works', () => {}); });`,
40
+ filename: "tests/rules/missing-supports.test.ts",
41
+ errors: [{ messageId: "missingFileSupports" }],
42
+ },
43
+ {
44
+ // [REQ-TEST-DESCRIBE-STORY] describe without story phrase
45
+ code: `/**\n * @supports docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md REQ-TEST-DESCRIBE-STORY\n */\ndescribe('no story reference here', () => {});`,
46
+ filename: "tests/rules/bad-describe.test.ts",
47
+ errors: [{ messageId: "missingDescribeStory" }],
48
+ },
49
+ {
50
+ // [REQ-TEST-IT-REQ-PREFIX] test name without [REQ-XXX] prefix
51
+ code: `/**\n * @supports docs/stories/020.0-DEV-TEST-ANNOTATION-VALIDATION.story.md REQ-TEST-IT-REQ-PREFIX\n */\nit('missing prefix', () => {});`,
52
+ filename: "tests/rules/bad-test-name.test.ts",
53
+ errors: [{ messageId: "missingReqPrefix" }],
54
+ },
55
+ ],
56
+ });
57
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-traceability",
3
- "version": "1.8.3",
3
+ "version": "1.9.0",
4
4
  "description": "A customizable ESLint plugin that enforces traceability annotations in your code, ensuring each implementation is linked to its requirement or test case.",
5
5
  "main": "lib/src/index.js",
6
6
  "types": "lib/src/index.d.ts",
@@ -189,6 +189,52 @@ function example() {
189
189
  }
190
190
  ```
191
191
 
192
+ ### traceability/require-test-traceability
193
+
194
+ Description: Enforces traceability conventions in test files by requiring:
195
+
196
+ - A file-level `@supports` annotation indicating which story and requirement(s) the test file exercises.
197
+ - Story references in top-level `describe` blocks.
198
+ - Requirement identifiers in `it`/`test` names using a `[REQ-XXX]` prefix.
199
+
200
+ The rule is designed to complement the function-level rules (such as `require-story-annotation` and `require-req-annotation`) by ensuring that tests explicitly declare which requirements and stories they validate. It is enabled in both the `recommended` and `strict` presets alongside the other core rules.
201
+
202
+ Options:
203
+
204
+ The rule accepts an optional configuration object:
205
+
206
+ - `testFilePatterns` (string[], optional) – Glob-style patterns (relative to the project root) used to identify test files. Only files matching at least one pattern are checked by this rule. Defaults to `["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"]`.
207
+ - `requireDescribeStory` (boolean, optional) – When `true` (default), requires that each top-level `describe` block include a story reference somewhere in its description text (for example, a path such as `docs/stories/010.0-PAYMENTS.story.md` or a shorter project-specific alias that your team uses consistently).
208
+ - `requireTestReqPrefix` (boolean, optional) – When `true` (default), requires each `it`/`test` block name to begin with a requirement identifier in square brackets, such as `[REQ-PAYMENTS-REFUND]`. The exact `REQ-` pattern is shared with the `valid-annotation-format` rule’s requirement ID checks.
209
+ - `describePattern` (string, optional) – A JavaScript regular expression **source** (without leading and trailing `/`) that the `describe` description text must match when `requireDescribeStory` is enabled. This lets you enforce a project-specific format such as requiring a canonical story path or a `STORY-` style identifier in the `describe` string. If omitted, a built-in default that loosely matches a typical story path (similar to `docs/stories/<name>.story.md`) is used.
210
+
211
+ Behavior notes:
212
+
213
+ - The rule only analyzes files whose paths match `testFilePatterns`.
214
+ - File-level `@supports` annotations are typically placed in a JSDoc block at the top of the file; the rule checks that at least one `@supports` tag is present and that it includes a story/requirement reference (for example, `@supports docs/stories/010.0-PAYMENTS.story.md#REQ-PAYMENTS-REFUND`).
215
+ - Top-level `describe` calls (such as `describe("payments refunds docs/stories/010.0-PAYMENTS.story.md", ...)`) are inspected when `requireDescribeStory` is `true`. Their first argument must be a string literal that satisfies `describePattern`.
216
+ - Test cases declared via `it(...)` or `test(...)` must use a string literal name beginning with a requirement prefix like `[REQ-PAYMENTS-REFUND]` when `requireTestReqPrefix` is `true`.
217
+
218
+ Default Severity: `error`
219
+
220
+ Example:
221
+
222
+ ```javascript
223
+ /**
224
+ * @supports docs/stories/010.0-PAYMENTS.story.md#REQ-PAYMENTS-REFUND
225
+ */
226
+
227
+ describe("Refunds flow docs/stories/010.0-PAYMENTS.story.md", () => {
228
+ it("[REQ-PAYMENTS-REFUND] issues refund on successful request", () => {
229
+ // ...
230
+ });
231
+
232
+ test("[REQ-PAYMENTS-REFUND-EDGE] handles partial refunds", () => {
233
+ // ...
234
+ });
235
+ });
236
+ ```
237
+
192
238
  ## Configuration Presets
193
239
 
194
240
  The plugin provides two built-in presets for easy configuration:
@@ -208,6 +254,7 @@ Core rules enabled by the `recommended` preset:
208
254
  - `traceability/valid-annotation-format`: `warn`
209
255
  - `traceability/valid-story-reference`: `error`
210
256
  - `traceability/valid-req-reference`: `error`
257
+ - `traceability/require-test-traceability`: `error`
211
258
 
212
259
  Usage:
213
260
 
@@ -520,5 +567,4 @@ If `--from` or `--to` is missing, the CLI prints an error, shows the help text,
520
567
  In CI:
521
568
 
522
569
  ```bash
523
- npm run traceability:verify
524
- ```
570
+ npm run traceability:verify