Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #44 from Vylpes/feature/tests
Browse files Browse the repository at this point in the history
Tests and CI
  • Loading branch information
Vylpes authored Aug 21, 2021
2 parents abe94cc + 7e623b4 commit 10fe450
Show file tree
Hide file tree
Showing 16 changed files with 3,813 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
tests
node_modules
dist
48 changes: 9 additions & 39 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,49 +1,19 @@
{
"parserOptions": {
"ecmaVersion": 6
},
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended"
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"camelcase": "error",
"brace-style": [
"error",
"1tbs"
],
"comma-dangle": [
"error",
"never"
],
"comma-spacing": [
"error",
{
"before": false,
"after": true
}
],
"comma-style": [
"error",
"last"
],
"arrow-body-style": [
"error",
"as-needed"
],
"arrow-parens": [
"error",
"as-needed"
],
"arrow-spacing": "error",
"no-var": "error",
"prefer-template": "error",
"prefer-const": "error"
},
"globals": {
"exports": "writable",
"module": "writable",
"require": "writable",
"process": "writable",
"console": "writable"
}
}
}
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ commands/
events/
/bot.ts

!tests/commands/
!tests/events/
!tests/__mocks/commands
!tests/__mocks/events

# Linux Environment Files
*.swp
6 changes: 4 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
testEnvironment: 'node'
}
preset: 'ts-jest',
testEnvironment: 'node',
};
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"scripts": {
"build": "tsc",
"test": "jest --coverage",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
"lint": "eslint ."
},
"author": "Vylpes",
"license": "MIT",
Expand All @@ -25,7 +24,13 @@
],
"repository": "github:vylpes/vylbot-core",
"devDependencies": {
"@types/jest": "^26.0.24",
"@types/node": "^16.3.2",
"@typescript-eslint/eslint-plugin": "^4.29.2",
"@typescript-eslint/parser": "^4.29.2",
"eslint": "^7.32.0",
"jest": "^27.0.6",
"ts-jest": "^27.0.4",
"typescript": "^4.3.5"
}
}
7 changes: 1 addition & 6 deletions src/client/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ export class Events {
// Emit when a message is sent
// Used to check for commands
public onMessage(message: Message): IEventResponse {
if (!message) return {
valid: false,
message: "Message was not supplied.",
};

if (!message.guild) return {
valid: false,
message: "Message was not sent in a guild, ignoring.",
Expand All @@ -50,7 +45,7 @@ export class Events {
const res = this._util.loadCommand(name, args, message);

if (!res.valid) {
if (res.message != 'File does not exist') return {
return {
valid: false,
message: res.message,
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Util {
} else {
return {
valid: false,
message: "Comamnd folder does not exist",
message: "Command folder does not exist",
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/__mocks/commands/noCategory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Command } from "../../../src/type/command";

export class noCategory extends Command {
constructor() {
super();
}
}
8 changes: 8 additions & 0 deletions tests/__mocks/commands/normal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Command } from "../../../src/type/command";

export class normal extends Command {
constructor() {
super();
this._category = "General";
}
}
8 changes: 8 additions & 0 deletions tests/__mocks/commands/roles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Command } from "../../../src/type/command";

export class roles extends Command {
constructor() {
super();
this._roles = [ "Moderator" ];
}
}
5 changes: 5 additions & 0 deletions tests/__mocks/events/normal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Event } from "../../../src/type/event";

export class normal extends Event {
public override channelCreate() {}
}
139 changes: 139 additions & 0 deletions tests/client/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { CoreClient } from "../../src/client/client";

import { Client } from "discord.js";
import * as dotenv from "dotenv";
import { Events } from "../../src/client/events";
import { Util } from "../../src/client/util";

jest.mock("discord.js");
jest.mock("dotenv");
jest.mock("../../src/client/events");
jest.mock("../../src/client/util");

describe('Constructor', () => {
test('Constructor_ExpectSuccessfulInitialisation', () => {
const coreClient = new CoreClient();

expect(coreClient).toBeInstanceOf(Client);
expect(dotenv.config).toBeCalledTimes(1);
expect(Events).toBeCalledTimes(1);
expect(Util).toBeCalledTimes(1);
});
});

describe('Start', () => {
test('Given Env Is Valid, Expect Successful Start', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}

const coreClient = new CoreClient();

expect(() => coreClient.start()).not.toThrow();
expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
});

test('Given BOT_TOKEN Is Null, Expect Failure', () => {
process.env = {
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}

const coreClient = new CoreClient();

expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env");
});

test('Given BOT_TOKEN Is Empty, Expect Failure', () => {
process.env = {
BOT_TOKEN: '',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}

const coreClient = new CoreClient();

expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env");
});

test('Given BOT_PREFIX Is Null, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}

const coreClient = new CoreClient();

expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
});

test('Given BOT_PREFIX Is Empty, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}

const coreClient = new CoreClient();

expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
});

test('Given FOLDERS_COMMANDS Is Null, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_EVENTS: 'events',
}

const coreClient = new CoreClient();

expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
});

test('Given FOLDERS_COMMANDS Is Empty, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: '',
FOLDERS_EVENTS: 'events',
}

const coreClient = new CoreClient();

expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
});

test('Given FOLDERS_EVENTS Is Null, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
}

const coreClient = new CoreClient();

expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
});

test('Given FOLDERS_EVENTS Is Empty, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: '',
}

const coreClient = new CoreClient();

expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
});
});
Loading

0 comments on commit 10fe450

Please sign in to comment.