Skip to content

Commit b0ddbc7

Browse files
admirjashariclaude
andcommitted
SP-1383: parameterize module discovery tests to clear Sonar smell
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent fca9d9c commit b0ddbc7

1 file changed

Lines changed: 13 additions & 47 deletions

File tree

tests/core/command/module-discovery.spec.ts

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -66,48 +66,24 @@ describe("ModuleHandler.discoverAndRegisterModules", () => {
6666
expect(handler.registeredModules).toHaveLength(0);
6767
});
6868

69-
it("warns when the module export is not a class", () => {
70-
const root = createCommandsTree({ "notclass/module.js": "module.exports = { notAClass: true };" });
69+
it.each([
70+
["export is not a class", "module.exports = { notAClass: true };", "is not a class/constructor function"],
71+
["class has no register method", "module.exports = class {};", "does not have a 'register' method"],
72+
["dependency is missing", "require('a-package-that-does-not-exist-xyz');", "Could not require module"],
73+
[
74+
"require fails with ENOENT",
75+
"require('fs').readFileSync('/no/such/path/xyz-content-cli-test');",
76+
"does not contain a compiled module.js file",
77+
],
78+
["require throws unexpectedly", "throw new Error('boom');", "Error processing module in"],
79+
])("logs the expected message when a module's %s", (_case, moduleContent, expectedMessage) => {
80+
const root = createCommandsTree({ "mod/module.js": moduleContent });
7181
const handler = newHandler();
7282

7383
handler.discoverAndRegisterModules(root, false);
7484

7585
expect(handler.registeredModules).toHaveLength(0);
76-
expect(messages().some(m => m.includes("is not a class/constructor function"))).toBe(true);
77-
});
78-
79-
it("warns when the module class has no register method", () => {
80-
const root = createCommandsTree({ "noregister/module.js": "module.exports = class {};" });
81-
const handler = newHandler();
82-
83-
handler.discoverAndRegisterModules(root, false);
84-
85-
expect(handler.registeredModules).toHaveLength(0);
86-
expect(messages().some(m => m.includes("does not have a 'register' method"))).toBe(true);
87-
});
88-
89-
it("warns when a module cannot be required due to a missing dependency", () => {
90-
const root = createCommandsTree({
91-
"badrequire/module.js": "require('a-package-that-does-not-exist-xyz');",
92-
});
93-
const handler = newHandler();
94-
95-
handler.discoverAndRegisterModules(root, false);
96-
97-
expect(handler.registeredModules).toHaveLength(0);
98-
expect(messages().some(m => m.includes("Could not require module"))).toBe(true);
99-
});
100-
101-
it("warns when requiring a module fails with an ENOENT error", () => {
102-
const root = createCommandsTree({
103-
"enoent/module.js": "require('fs').readFileSync('/no/such/path/xyz-content-cli-test');",
104-
});
105-
const handler = newHandler();
106-
107-
handler.discoverAndRegisterModules(root, false);
108-
109-
expect(handler.registeredModules).toHaveLength(0);
110-
expect(messages().some(m => m.includes("does not contain a compiled module.js file"))).toBe(true);
86+
expect(messages().some(m => m.includes(expectedMessage))).toBe(true);
11187
});
11288

11389
it("logs an error when the commands path is not a directory", () => {
@@ -122,16 +98,6 @@ describe("ModuleHandler.discoverAndRegisterModules", () => {
12298
expect(messages().some(m => m.includes("Failed to read modules directory"))).toBe(true);
12399
});
124100

125-
it("logs an error when requiring a module throws unexpectedly", () => {
126-
const root = createCommandsTree({ "throws/module.js": "throw new Error('boom');" });
127-
const handler = newHandler();
128-
129-
handler.discoverAndRegisterModules(root, false);
130-
131-
expect(handler.registeredModules).toHaveLength(0);
132-
expect(messages().some(m => m.includes("Error processing module in"))).toBe(true);
133-
});
134-
135101
it("logs an error when the commands directory does not exist", () => {
136102
const root = fs.mkdtempSync(join(tmpdir(), "mh-nocommands-"));
137103
createdRoots.push(root);

0 commit comments

Comments
 (0)