From a7b9a3a46373693352d6bc395826f409fe80e7c0 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 24 Jul 2024 18:18:00 +0100 Subject: [PATCH 1/3] Create unit test plan --- package.json | 2 +- tests/registry.test.ts | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/registry.test.ts diff --git a/package.json b/package.json index 102b607..3488a56 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "clean": "rm -rf node_modules/ dist/", "build": "tsc", "start": "node ./dist/bot.js", - "test": "jest --passWithNoTests", + "test": "jest", "lint": "eslint .", "lint:fix": "eslint . --fix", "db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js", diff --git a/tests/registry.test.ts b/tests/registry.test.ts new file mode 100644 index 0000000..3c1aa09 --- /dev/null +++ b/tests/registry.test.ts @@ -0,0 +1,7 @@ +describe("RegisterCommands", () => { + test.todo("EXPECT every command in the commands folder to be registered"); +}); + +describe("RegisterButtonEvents", () => { + test.todo("EXEPCT every button event in the button events folder to be registered"); +}); -- 2.43.4 From a718011640601d4036d785c5382441d03cfcffff Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 25 Jul 2024 18:41:44 +0100 Subject: [PATCH 2/3] Add tests to check if all events are registered --- tests/registry.test.ts | 63 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/tests/registry.test.ts b/tests/registry.test.ts index 3c1aa09..38d012c 100644 --- a/tests/registry.test.ts +++ b/tests/registry.test.ts @@ -1,7 +1,66 @@ +import {CoreClient} from "../src/client/client"; +import Registry from "../src/registry"; +import fs from "fs"; +import path from "path"; + describe("RegisterCommands", () => { - test.todo("EXPECT every command in the commands folder to be registered"); + test("EXPECT every command in the commands folder to be registered", () => { + const registeredCommands: string[] = []; + + CoreClient.RegisterCommand = jest.fn().mockImplementation((name: string) => { + registeredCommands.push(name); + }); + + Registry.RegisterCommands(); + + const commandFiles = getFilesInDirectory(path.join(process.cwd(), "src", "commands")) + .filter(x => x.endsWith(".ts")); + + for (let file of commandFiles) { + expect(registeredCommands).toContain(file.split("/").pop()!.split(".")[0]); + } + + expect(commandFiles.length).toBe(registeredCommands.length); + }); }); describe("RegisterButtonEvents", () => { - test.todo("EXEPCT every button event in the button events folder to be registered"); + test("EXEPCT every button event in the button events folder to be registered", () => { + const registeredButtonEvents: string[] = []; + + CoreClient.RegisterButtonEvent = jest.fn().mockImplementation((name: string) => { + registeredButtonEvents.push(name); + }); + + Registry.RegisterButtonEvents(); + + const eventFiles = getFilesInDirectory(path.join(process.cwd(), "src", "buttonEvents")) + .filter(x => x.endsWith(".ts")); + + for (let file of eventFiles) { + expect(registeredButtonEvents).toContain(file.split("/").pop()!.split(".")[0].toLowerCase()); + } + + expect(eventFiles.length).toBe(registeredButtonEvents.length); + }); }); + +function getFilesInDirectory(dir: string): string[] { + let results: string[] = []; + const list = fs.readdirSync(dir); + + list.forEach(file => { + file = path.join(dir, file); + const stat = fs.statSync(file); + + if (stat && stat.isDirectory()) { + /* recurse into a subdirectory */ + results = results.concat(getFilesInDirectory(file)); + } else { + /* is a file */ + results.push(file); + } + }); + + return results; +} -- 2.43.4 From 425266eed9f2f28d59c22b6bd5d72fe09a5dc84a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 25 Jul 2024 18:42:57 +0100 Subject: [PATCH 3/3] Fix lint --- tests/registry.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/registry.test.ts b/tests/registry.test.ts index 38d012c..71d80db 100644 --- a/tests/registry.test.ts +++ b/tests/registry.test.ts @@ -16,7 +16,7 @@ describe("RegisterCommands", () => { const commandFiles = getFilesInDirectory(path.join(process.cwd(), "src", "commands")) .filter(x => x.endsWith(".ts")); - for (let file of commandFiles) { + for (const file of commandFiles) { expect(registeredCommands).toContain(file.split("/").pop()!.split(".")[0]); } @@ -37,7 +37,7 @@ describe("RegisterButtonEvents", () => { const eventFiles = getFilesInDirectory(path.join(process.cwd(), "src", "buttonEvents")) .filter(x => x.endsWith(".ts")); - for (let file of eventFiles) { + for (const file of eventFiles) { expect(registeredButtonEvents).toContain(file.split("/").pop()!.split(".")[0].toLowerCase()); } -- 2.43.4