From ff9f3e458eaec933c95dc86745f1ebec88fc59b2 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 26 Jul 2024 18:31:06 +0100 Subject: [PATCH] Add some unit tests (#321) - Add some unit tests to the project - These aren't massive, more just checks to ensure certain things are as they should - Such as checking all commands are actually registered #15 Reviewed-on: https://git.vylpes.xyz/External/card-drop/pulls/321 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- package.json | 2 +- tests/registry.test.ts | 66 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 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..71d80db --- /dev/null +++ b/tests/registry.test.ts @@ -0,0 +1,66 @@ +import {CoreClient} from "../src/client/client"; +import Registry from "../src/registry"; +import fs from "fs"; +import path from "path"; + +describe("RegisterCommands", () => { + 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 (const file of commandFiles) { + expect(registeredCommands).toContain(file.split("/").pop()!.split(".")[0]); + } + + expect(commandFiles.length).toBe(registeredCommands.length); + }); +}); + +describe("RegisterButtonEvents", () => { + 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 (const 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; +}