From 8d90f28307c0d900b728fba8c895e82cb38ac1bf Mon Sep 17 00:00:00 2001 From: Fleny Date: Wed, 7 Jan 2026 20:51:26 +0100 Subject: [PATCH] test: Refactor mocha config & test scripts (#4664) * Merge mocha config file * Simplify scripts, remove coverage path fixing Codecov is able to fix the coverage paths on its own * Readd coveragePathFixing script * Fix coverage path fix script --- .mocharc.base.cjs | 9 ------- .mocharc.cjs | 27 ++++++++++++++++---- package.json | 7 +++--- packages/bot/package.json | 14 +++++------ packages/discordeno/package.json | 12 ++++----- packages/gateway/package.json | 14 +++++------ packages/rest/package.json | 14 +++++------ packages/types/package.json | 9 +++---- packages/utils/package.json | 12 ++++----- turbo.json | 42 +++++++++++++++++++++----------- 10 files changed, 85 insertions(+), 75 deletions(-) delete mode 100644 .mocharc.base.cjs diff --git a/.mocharc.base.cjs b/.mocharc.base.cjs deleted file mode 100644 index 4d585c8b0..000000000 --- a/.mocharc.base.cjs +++ /dev/null @@ -1,9 +0,0 @@ -// This needs to be a different file as this is used for Deno and Bun - -module.exports = { - recursive: true, - timeout: 2000, - 'watch-extensions': 'ts', - 'watch-files': ['src', 'tests'], - parallel: false, -} diff --git a/.mocharc.cjs b/.mocharc.cjs index cc65d8d85..ae8736faa 100644 --- a/.mocharc.cjs +++ b/.mocharc.cjs @@ -1,6 +1,23 @@ -module.exports = { - ...require('./.mocharc.base.cjs'), - require: 'ts-node/register', - loader: 'ts-node/esm', - 'enable-source-maps': true, +// @ts-check + +// If we are running in Bun or Deno, they have native TypeScript support with .js imports, node requires .ts imports +const supportsTypescript = 'Bun' in globalThis || 'Deno' in globalThis + +/** @type {import("mocha").MochaInstanceOptions & Record} */ +const mochaConfig = { + timeout: 2000, + 'watch-extensions': 'ts', + 'watch-files': ['src', 'tests'], } + +if (!supportsTypescript) { + mochaConfig.require = ['ts-node/register'] + + // Node options + mochaConfig.loader = ['ts-node/esm'] + // Node will output a ExperimentalWarning about --loader (--experimental-loader) and a DeprecationWarning because ts-node uses fs.Stat + mochaConfig['no-warnings'] = true + mochaConfig['enable-source-maps'] = true +} + +module.exports = mochaConfig diff --git a/package.json b/package.json index 52a96fe73..7156d505e 100644 --- a/package.json +++ b/package.json @@ -10,17 +10,16 @@ "scripts": { "build": "turbo build", "build:type": "turbo build:type", + "release-build": "turbo build build:type", "build:doc": "typedoc --options ./typedoc.jsonc", "build:watch": "turbo watch build build:type", - "release-build": "turbo build build:type", "test:unit": "turbo test:unit", - "test:unit-coverage": "turbo test:unit-coverage", - "test:bun-unit": "turbo test:bun-unit", "test:deno-unit": "turbo test:deno-unit", + "test:bun-unit": "turbo test:bun-unit", "test:integration": "turbo test:integration", "test:e2e": "turbo test:e2e", - "test:type": "turbo build:type", "test:test-type": "turbo test:test-type", + "test:type": "turbo build:type", "check": "turbo check", "check:all": "biome check --write", "postinstall": "husky" diff --git a/packages/bot/package.json b/packages/bot/package.json index b02b0524f..dc9399558 100644 --- a/packages/bot/package.json +++ b/packages/bot/package.json @@ -17,14 +17,12 @@ "build:type": "tsc --declaration --emitDeclarationOnly --declarationDir dist", "release-build": "yarn build && yarn build:type", "check": "biome check --write", - "test:unit-coverage": "c8 mocha --no-warnings 'tests/unit/**/*.spec.ts'", - "test:unit": "c8 --r lcov mocha --no-warnings 'tests/unit/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js discordeno", - "test:unit:watch": "mocha --no-warnings --watch --parallel 'tests/unit/**/*.spec.ts'", - "test:deno-unit": "deno -A ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/unit/**/*.spec.ts'", - "test:bun-unit": "bun ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/unit/**/*.spec.ts'", - "test:e2e": "c8 --r lcov mocha --no-warnings --jobs 1 --t 30000 'tests/e2e/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js rest", - "test:type": "tsc --noEmit", - "test:test-type": "tsc --project tests/tsconfig.json" + "test:unit": "c8 -r lcov mocha 'tests/unit/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js bot", + "test:deno-unit": "deno -A npm:mocha 'tests/unit/**/*.spec.ts'", + "test:bun-unit": "bun --bun mocha 'tests/unit/**/*.spec.ts'", + "test:e2e": "c8 -r lcov mocha -t 30000 'tests/e2e/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js bot", + "test:test-type": "tsc --project tests/tsconfig.json", + "test:type": "tsc --noEmit" }, "dependencies": { "@discordeno/gateway": "workspace:^", diff --git a/packages/discordeno/package.json b/packages/discordeno/package.json index 64ce5990a..4a57766db 100644 --- a/packages/discordeno/package.json +++ b/packages/discordeno/package.json @@ -17,13 +17,11 @@ "build:type": "tsc --declaration --emitDeclarationOnly --declarationDir dist", "release-build": "yarn build && yarn build:type", "check": "biome check --write", - "test:unit-coverage": "c8 mocha --no-warnings 'tests/**/*.spec.ts'", - "test:unit": "c8 --r lcov mocha --no-warnings 'tests/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js discordeno", - "test:unit:watch": "mocha --no-warnings --watch --parallel 'tests/**/*.spec.ts'", - "test:deno-unit": "deno -A ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/**/*.spec.ts'", - "test:bun-unit": "bun ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/**/*.spec.ts'", - "test:type": "tsc --noEmit", - "test:test-type": "tsc --project tests/tsconfig.json" + "test:unit": "c8 -r lcov mocha 'tests/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js discordeno", + "test:deno-unit": "deno -A npm:mocha 'tests/**/*.spec.ts'", + "test:bun-unit": "bun --bun mocha 'tests/**/*.spec.ts'", + "test:test-type": "tsc --project tests/tsconfig.json", + "test:type": "tsc --noEmit" }, "dependencies": { "@discordeno/bot": "workspace:^", diff --git a/packages/gateway/package.json b/packages/gateway/package.json index e5ea4ea9c..a3c2979a8 100644 --- a/packages/gateway/package.json +++ b/packages/gateway/package.json @@ -17,14 +17,12 @@ "build:type": "tsc --declaration --emitDeclarationOnly --declarationDir dist", "release-build": "yarn build && yarn build:type", "check": "biome check --write", - "test:unit-coverage": "c8 mocha --no-warnings 'tests/unit/**/*.spec.ts'", - "test:unit": "c8 --r lcov mocha --no-warnings 'tests/unit/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js gateway", - "test:unit:watch": "mocha --no-warnings --watch --parallel 'tests/unit/**/*.spec.ts'", - "test:deno-unit": "deno -A ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/unit/**/*.spec.ts'", - "test:bun-unit": "bun ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/unit/**/*.spec.ts'", - "test:integration": "c8 --r lcov mocha --no-warnings --t 15000 'tests/integration/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js gateway", - "test:type": "tsc --noEmit", - "test:test-type": "tsc --project tests/tsconfig.json" + "test:unit": "c8 -r lcov mocha 'tests/unit/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js gateway", + "test:deno-unit": "deno -A npm:mocha 'tests/unit/**/*.spec.ts'", + "test:bun-unit": "bun --bun mocha 'tests/unit/**/*.spec.ts'", + "test:integration": "c8 -r lcov mocha -t 15000 'tests/integration/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js gateway", + "test:test-type": "tsc --project tests/tsconfig.json", + "test:type": "tsc --noEmit" }, "dependencies": { "@discordeno/types": "workspace:^", diff --git a/packages/rest/package.json b/packages/rest/package.json index 5e7411b04..1dcf0523b 100644 --- a/packages/rest/package.json +++ b/packages/rest/package.json @@ -17,14 +17,12 @@ "build:type": "tsc --declaration --emitDeclarationOnly --declarationDir dist", "release-build": "yarn build && yarn build:type", "check": "biome check --write", - "test:unit-coverage": "c8 mocha --no-warnings 'tests/unit/**/*.spec.ts'", - "test:unit": "c8 --r lcov mocha --no-warnings 'tests/unit/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js rest", - "test:unit:watch": "mocha --no-warnings --watch --parallel 'tests/unit/**/*.spec.ts'", - "test:deno-unit": "deno -A ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/unit/**/*.spec.ts'", - "test:bun-unit": "bun ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/unit/**/*.spec.ts'", - "test:e2e": "c8 --r lcov mocha --exit --no-warnings --jobs 1 --t 60000 'tests/e2e/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js rest", - "test:type": "tsc --noEmit", - "test:test-type": "tsc --project tests/tsconfig.json" + "test:unit": "c8 -r lcov mocha 'tests/unit/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js rest", + "test:deno-unit": "deno -A npm:mocha 'tests/unit/**/*.spec.ts'", + "test:bun-unit": "bun --bun mocha 'tests/unit/**/*.spec.ts'", + "test:e2e": "c8 -r lcov mocha -t 60000 'tests/e2e/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js rest", + "test:test-type": "tsc --project tests/tsconfig.json", + "test:type": "tsc --noEmit" }, "dependencies": { "@discordeno/types": "workspace:^", diff --git a/packages/types/package.json b/packages/types/package.json index c6e6fc971..b79c7ca40 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -17,11 +17,10 @@ "build:type": "tsc --declaration --emitDeclarationOnly --declarationDir dist", "release-build": "yarn build && yarn build:type", "check": "biome check --write", - "test:unit-coverage": "c8 mocha --no-warnings 'tests/**/*.spec.ts'", - "test:unit": "c8 --r lcov mocha --no-warnings 'tests/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js utils", - "test:unit:watch": "mocha --no-warnings --watch --parallel 'tests/**/*.spec.ts'", - "test:deno-unit": "deno -A ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/**/*.spec.ts'", - "test:bun-unit": "bun ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/**/*.spec.ts'", + "test:unit": "c8 -r lcov mocha 'tests/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js types", + "test:deno-unit": "deno -A npm:mocha 'tests/**/*.spec.ts'", + "test:bun-unit": "bun --bun mocha 'tests/**/*.spec.ts'", + "test:test-type": "tsc --project tests/tsconfig.json", "test:type": "tsc --noEmit" }, "devDependencies": { diff --git a/packages/utils/package.json b/packages/utils/package.json index be7a815aa..a7feb9c23 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -17,13 +17,11 @@ "build:type": "tsc --declaration --emitDeclarationOnly --declarationDir dist", "release-build": "yarn build && yarn build:type", "check": "biome check --write", - "test:unit-coverage": "c8 mocha --no-warnings 'tests/**/*.spec.ts'", - "test:unit": "c8 --r lcov mocha --no-warnings 'tests/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js utils", - "test:unit:watch": "mocha --no-warnings --watch --parallel 'tests/**/*.spec.ts'", - "test:deno-unit": "deno -A ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/**/*.spec.ts'", - "test:bun-unit": "bun ../../node_modules/mocha/bin/mocha.js --config ../../.mocharc.base.cjs 'tests/**/*.spec.ts'", - "test:type": "tsc --noEmit", - "test:test-type": "tsc --project tests/tsconfig.json" + "test:unit": "c8 -r lcov mocha 'tests/**/*.spec.ts' && node ../../scripts/coveragePathFixing.js utils", + "test:deno-unit": "deno -A npm:mocha 'tests/**/*.spec.ts'", + "test:bun-unit": "bun --bun mocha 'tests/**/*.spec.ts'", + "test:test-type": "tsc --project tests/tsconfig.json", + "test:type": "tsc --noEmit" }, "dependencies": { "@discordeno/types": "workspace:^" diff --git a/turbo.json b/turbo.json index 50d37da4f..a9c2c53b9 100644 --- a/turbo.json +++ b/turbo.json @@ -2,39 +2,53 @@ "$schema": "https://turbo.build/schema.json", "tasks": { "build": { - "outputs": ["dist/**/*.js"], - "inputs": ["src/**/*.ts"] + "inputs": ["src/**/*.ts"], + "outputs": ["dist/**/*.js"] }, "build:type": { "dependsOn": ["^build:type"], + "inputs": ["src/**/*.ts"], "outputs": ["dist/**/*.d.ts", "dist/**/*.d.ts.map", "dist/tsconfig.tsbuildinfo"] }, - "test:unit-coverage": { - "dependsOn": ["^build"], - "outputs": ["coverage/**"] + "check": { + "dependsOn": ["build:type"], + "cache": false }, "test:unit": { "dependsOn": ["^build", "build"], + // We need to exclude e2e and integration test manually since when there aren't e2e or integration we don't have a unit folder + "inputs": ["src/**/*.ts", "tests/**/*.spec.ts", "!tests/e2e/**/*.spec.ts", "!tests/integration**/*.spec.ts"], "outputs": ["coverage/**"] }, + "test:deno-unit": { + "dependsOn": ["^build", "build"], + // We need to exclude e2e and integration test manually since when there aren't e2e or integration we don't have a unit folder + "inputs": ["src/**/*.ts", "tests/**/*.spec.ts", "!tests/e2e/**/*.spec.ts", "!tests/integration**/*.spec.ts"] + }, + "test:bun-unit": { + "dependsOn": ["^build", "build"], + // We need to exclude e2e and integration test manually since when there aren't e2e or integration we don't have a unit folder + "inputs": ["src/**/*.ts", "tests/**/*.spec.ts", "!tests/e2e/**/*.spec.ts", "!tests/integration**/*.spec.ts"] + }, "test:integration": { "dependsOn": ["^build"], + "inputs": ["src/**/*.ts", "tests/integration**/*.spec.ts"], "outputs": ["coverage/**"] }, - "test:test-type": { - "dependsOn": ["^build:type"] - }, - "test:deno-unit": { - "dependsOn": ["^build", "build"] - }, - "test:bun-unit": { - "dependsOn": ["^build", "build"] - }, "test:e2e": { "dependsOn": ["^build"], + "inputs": ["src/**/*.ts", "tests/e2e**/*.spec.ts"], "outputs": ["coverage/**"], "env": ["DISCORD_TOKEN", "E2E_TEST_GUILD_ID"] }, + "test:test-type": { + "dependsOn": ["^build:type"], + "inputs": ["src/**/*.ts", "tests/**/*.ts"] + }, + "test:type": { + "dependsOn": ["^build:type"], + "inputs": ["src/**/*.ts"] + }, "check": { "dependsOn": ["build:type"], "cache": false