Skip to content

Commit

Permalink
Tests & Linting fixes (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lewdcario authored Mar 4, 2024
1 parent 9e33db3 commit 79a3c50
Show file tree
Hide file tree
Showing 30 changed files with 231 additions and 202 deletions.
51 changes: 12 additions & 39 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@
"es2020": true,
"browser": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"airbnb-base",
"prettier"
],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "airbnb-base"],
"ignorePatterns": ["**/dist/*"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 8,
"requireConfigFile": false
},
"plugins": [
"@typescript-eslint",
"@stylistic/eslint-plugin"
],
"plugins": ["@typescript-eslint", "@stylistic/eslint-plugin"],
"root": true,
"rules": {
"@typescript-eslint/no-non-null-assertion": "off",
Expand All @@ -38,23 +30,12 @@
}
],
"block-spacing": "error",
"brace-style": [
"error",
"stroustrup"
],
"brace-style": ["error", "stroustrup"],
"class-methods-use-this": 0,
"comma-dangle": [
"error",
"never"
],
"curly": [
"error",
"multi-line"
],
"comma-dangle": ["error", "never"],
"curly": ["error", "multi-line"],
"global-require": "off",
"indent": [
"error", "tab"
],
"indent": ["error", "tab"],
"import/extensions": "off",
"import/prefer-default-export": "off",
"import/no-dynamic-require": "off",
Expand All @@ -74,10 +55,7 @@
"mode": "strict"
}
],
"linebreak-style": [
"error",
"unix"
],
"linebreak-style": ["error", "unix"],
"max-classes-per-file": "off",
"max-len": [
"error",
Expand Down Expand Up @@ -110,10 +88,11 @@
"prev": "*",
"next": "function"
},
{
{
"blankLine": "always",
"prev": "*",
"next": "export" }
"next": "export"
}
],
"object-curly-newline": [
"error",
Expand All @@ -135,14 +114,8 @@
}
}
],
"object-curly-spacing": [
"error",
"always"
],
"semi": [
"error",
"always"
],
"object-curly-spacing": ["error", "always"],
"semi": ["error", "always"],
"space-before-function-paren": [
"error",
{
Expand Down
49 changes: 49 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Lint

on:
pull_request:
branches: [main]
push:
branches: [main]

jobs:
linting:
name: Linting
runs-on: ubuntu-latest

strategy:
matrix:
NODE_VERSION: [20.x]

steps:
- name: Install NodeJS
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.NODE_VERSION }}

- name: Code Checkout
uses: actions/checkout@v3

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: Cache Node.js modules
id: yarn-cache
uses: actions/cache@v3
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install Dependencies
run: yarn install --frozen-lockfile --silent
env:
CI: true

- name: Build the project
run: yarn build

- name: Lint the project with Yarn
run: yarn lint
48 changes: 24 additions & 24 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
name: Node.js Package

on:
release:
types: [created]
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
- run: yarn install --frozen-lockfile
- run: yarn test
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
- run: yarn install --frozen-lockfile
- run: yarn test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: yarn install --frozen-lockfile
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: yarn install --frozen-lockfile
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
25 changes: 22 additions & 3 deletions __tests__/Client.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
describe,
expect,
it
describe, expect, it
} from '@jest/globals';
import { readdir } from 'fs/promises';
import { join } from 'path';

import { ExtendedClient as Client } from '../src/Classes/ExtendedClient';
Expand Down Expand Up @@ -65,6 +64,26 @@ describe('Client', () => {
await expect(init()).resolves.not.toThrow();
});

it('Should load with the correct number of events', async () => {
let client: Client | null = null;
const eventPath = join(__dirname, 'valid', 'events');

const init = async () => {
client = new Client({ intents: [] });
await client.init({
commandPath: join(__dirname, 'valid', 'commands'),
eventPath
});
};

await expect(init()).resolves.not.toThrow();

const eventFiles = await readdir(eventPath);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((client as any)!._eventHandler.size).toBe(eventFiles.length + 1); // "interactionCreate" event is always added
});

it('Should throw if command builder doesn\'t come with an execute handler', async () => {
const init = async () => {
const client = new Client({ intents: [] });
Expand Down
8 changes: 1 addition & 7 deletions __tests__/invalid-command/commands/chat/builders/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@ import { ChatInputCommand } from '../../../../../src';

export const ns = 'sme';

export default new ChatInputCommand()
.setBuilder((builder) =>
builder
.setName('ping')
.setDescription('Ping Pong Command')
)
.setGlobal(true);
export default new ChatInputCommand().setBuilder((builder) => builder.setName('ping').setDescription('Ping Pong Command')).setGlobal(true);
2 changes: 1 addition & 1 deletion __tests__/invalid-command/commands/chat/execution/ping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChatInputCommandInteraction } from 'discord.js';

export default async function ping(interaction: ChatInputCommandInteraction) {
return interaction.reply('Pong!')
return interaction.reply('Pong!');
}
2 changes: 1 addition & 1 deletion __tests__/invalid-event/commands/chat/execution/ping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChatInputCommandInteraction } from 'discord.js';

export default async function ping(interaction: ChatInputCommandInteraction) {
return interaction.reply('Pong!')
return interaction.reply('Pong!');
}
6 changes: 1 addition & 5 deletions __tests__/valid/commands/chat/builders/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import handler from '../execution/ping';
export const ns = 'sme';

export default new ChatInputCommand()
.setBuilder((builder) =>
builder
.setName('ping')
.setDescription('Ping Pong Command')
)
.setBuilder((builder) => builder.setName('ping').setDescription('Ping Pong Command'))
.setGlobal(true)
.setExecute(handler);
2 changes: 1 addition & 1 deletion __tests__/valid/commands/chat/execution/ping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChatInputCommandInteraction } from 'discord.js';

export default async function ping(interaction: ChatInputCommandInteraction) {
return interaction.reply('Pong!')
return interaction.reply('Pong!');
}
4 changes: 2 additions & 2 deletions __tests__/valid/events/ready.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Events } from 'discord.js';
import { Client, Event } from '../../../src';
import { Event } from '../../../src';

async function onReady(client: Client) {
async function onReady() {
// Noop
}

Expand Down
26 changes: 11 additions & 15 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ export default {
// errorOnDeprecated: false,

// The default configuration for fake timers
// fakeTimers: {
// "enableGlobally": false
// },
fakeTimers: { enableGlobally: true },

// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],
Expand Down Expand Up @@ -88,9 +86,7 @@ export default {
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
},
moduleNameMapper: { '^@/(.*)$': '<rootDir>/src/$1' },

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
Expand Down Expand Up @@ -155,13 +151,10 @@ export default {
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
testMatch: ["**/*.test.ts"],
testMatch: ['**/*.test.ts'],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
testPathIgnorePatterns: [
'/node_modules/',
'/dist/'
],
testPathIgnorePatterns: ['/node_modules/', '/dist/'],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],
Expand All @@ -174,10 +167,13 @@ export default {

// A map from regular expressions to paths to transformers
transform: {
'^.+\\.ts?$': ['ts-jest', {
isolatedModules: true,
tsconfig: 'tsconfig.json'
}]
'^.+\\.ts?$': [
'ts-jest',
{
isolatedModules: true,
tsconfig: 'tsconfig.json'
}
]
},

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@progressive-victory/client",
"version": "0.3.3",
"version": "0.3.4",
"description": "Modified Discord.js client for discord bots",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"types": "typings/index.d.ts",
"files": [
"dist"
],
Expand All @@ -15,8 +15,8 @@
"license": "MIT",
"private": false,
"scripts": {
"lint": "prettier --check . && eslint . --ext .ts",
"format": "prettier --write . && eslint --fix --format=pretty src __tests__",
"lint": "eslint . --ext .ts",
"format": "prettier --write . && eslint --fix src __tests__",
"build": "swc ./src --out-dir dist && tsc --emitDeclarationOnly",
"start": "node ./dist/index.js",
"prepublish": "yarn build",
Expand All @@ -39,10 +39,9 @@
"@typescript-eslint/parser": "^6.7.5",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"jest": "^29.7.0",
"prettier": "^3.2.4",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
Expand Down
Loading

0 comments on commit 79a3c50

Please sign in to comment.