From 28fbc465673af30324152a39096e4bee01c48b95 Mon Sep 17 00:00:00 2001 From: Kelechi Ebiri Date: Sun, 21 Sep 2025 14:12:15 +0100 Subject: [PATCH 1/7] Add typescript-async-loaders example Signed-off-by: Kelechi Ebiri --- .../custom-loader.mjs | 6 ++++++ .../typescript-async-loaders/package.json | 19 +++++++++++++++++++ .../test/async-loader.test.ts | 7 +++++++ 3 files changed, 32 insertions(+) create mode 100644 packages/typescript-async-loaders/custom-loader.mjs create mode 100644 packages/typescript-async-loaders/package.json create mode 100644 packages/typescript-async-loaders/test/async-loader.test.ts diff --git a/packages/typescript-async-loaders/custom-loader.mjs b/packages/typescript-async-loaders/custom-loader.mjs new file mode 100644 index 0000000..d7a5626 --- /dev/null +++ b/packages/typescript-async-loaders/custom-loader.mjs @@ -0,0 +1,6 @@ +export async function load(url, context, defaultLoad) { + if (url.endsWith('.ts')) { + await new Promise(resolve => setTimeout(resolve, 1)); + } + return defaultLoad(url, context, defaultLoad); +} diff --git a/packages/typescript-async-loaders/package.json b/packages/typescript-async-loaders/package.json new file mode 100644 index 0000000..14a23d0 --- /dev/null +++ b/packages/typescript-async-loaders/package.json @@ -0,0 +1,19 @@ +{ + "name": "example-typescript-async-loaders", + "version": "1.0.0", + "type": "module", + "engines": { + "node": ">=18.6.0" + }, + "scripts": { + "test": "NODE_OPTIONS='--loader=ts-node/esm --loader=./custom-loader.mjs' mocha test/**/*.test.ts" + }, + "devDependencies": { + "mocha": "^11.6.0", + "chai": "^4.3.7", + "ts-node": "^10.9.0", + "typescript": "^5.0.0", + "@types/mocha": "^10.0.0", + "@types/chai": "^4.3.0" + } +} diff --git a/packages/typescript-async-loaders/test/async-loader.test.ts b/packages/typescript-async-loaders/test/async-loader.test.ts new file mode 100644 index 0000000..8424ae7 --- /dev/null +++ b/packages/typescript-async-loaders/test/async-loader.test.ts @@ -0,0 +1,7 @@ +import { expect } from 'chai'; + +describe('Async Loader Test', () => { + it('should work with async loaders', () => { + expect(true).to.be.true; + }); +}); From 817db84e3ad9a001aa0c358f5bf9acab8c17a0a2 Mon Sep 17 00:00:00 2001 From: Mark Wiemer Date: Mon, 29 Sep 2025 18:02:44 -0700 Subject: [PATCH 2/7] Rework typescript-async-loaders example --- packages/typescript-async-loaders/README.md | 5 +++++ packages/typescript-async-loaders/custom-loader.mjs | 5 +++-- packages/typescript-async-loaders/package.json | 8 +++----- .../test/async-loader.test.ts | 13 +++++++++---- 4 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 packages/typescript-async-loaders/README.md diff --git a/packages/typescript-async-loaders/README.md b/packages/typescript-async-loaders/README.md new file mode 100644 index 0000000..0e2693f --- /dev/null +++ b/packages/typescript-async-loaders/README.md @@ -0,0 +1,5 @@ +## TypeScript and async custom loaders + +Mocha 11.7.0 added support for Node's experimental [`require_module`](https://nodejs.org/docs/v22.20.0/api/modules.html#loading-ecmascript-modules-using-require) feature, which makes file loading a bit more complicated for Mocha 11. In this example, we showcase using ts-node **and** a custom asynchronous module loader to run a TypeScript test file. + +This is an advanced example. For a simpler example, see the [`typescript`](../typescript) sibling package. diff --git a/packages/typescript-async-loaders/custom-loader.mjs b/packages/typescript-async-loaders/custom-loader.mjs index d7a5626..3351ae8 100644 --- a/packages/typescript-async-loaders/custom-loader.mjs +++ b/packages/typescript-async-loaders/custom-loader.mjs @@ -1,6 +1,7 @@ export async function load(url, context, defaultLoad) { - if (url.endsWith('.ts')) { - await new Promise(resolve => setTimeout(resolve, 1)); + if (url.endsWith(".ts")) { + console.log("Loaded with custom loader"); + await new Promise((resolve) => setTimeout(resolve, 1)); } return defaultLoad(url, context, defaultLoad); } diff --git a/packages/typescript-async-loaders/package.json b/packages/typescript-async-loaders/package.json index 14a23d0..1963bf7 100644 --- a/packages/typescript-async-loaders/package.json +++ b/packages/typescript-async-loaders/package.json @@ -3,17 +3,15 @@ "version": "1.0.0", "type": "module", "engines": { - "node": ">=18.6.0" + "node": "^18.18.0 || ^20.9.0 || >=21.1.0" }, "scripts": { "test": "NODE_OPTIONS='--loader=ts-node/esm --loader=./custom-loader.mjs' mocha test/**/*.test.ts" }, "devDependencies": { - "mocha": "^11.6.0", - "chai": "^4.3.7", + "mocha": "^11.7.0", "ts-node": "^10.9.0", "typescript": "^5.0.0", - "@types/mocha": "^10.0.0", - "@types/chai": "^4.3.0" + "@types/mocha": "^10.0.0" } } diff --git a/packages/typescript-async-loaders/test/async-loader.test.ts b/packages/typescript-async-loaders/test/async-loader.test.ts index 8424ae7..3f4feb4 100644 --- a/packages/typescript-async-loaders/test/async-loader.test.ts +++ b/packages/typescript-async-loaders/test/async-loader.test.ts @@ -1,7 +1,12 @@ -import { expect } from 'chai'; +import assert from "assert"; +import { describe, it } from "mocha"; -describe('Async Loader Test', () => { - it('should work with async loaders', () => { - expect(true).to.be.true; +function add(a: number, b: number): number { + return a + b; +} + +describe("async loaders", () => { + it("should work", () => { + assert.strictEqual(add(2, 3), 5); }); }); From 989afa021542605ca482568729ea5020e019afc7 Mon Sep 17 00:00:00 2001 From: Mark Wiemer <7833360+mark-wiemer@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:44:44 -0700 Subject: [PATCH 3/7] Update packages/typescript-async-loaders/README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Josh Goldberg ✨ --- packages/typescript-async-loaders/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typescript-async-loaders/README.md b/packages/typescript-async-loaders/README.md index 0e2693f..371c6d4 100644 --- a/packages/typescript-async-loaders/README.md +++ b/packages/typescript-async-loaders/README.md @@ -2,4 +2,4 @@ Mocha 11.7.0 added support for Node's experimental [`require_module`](https://nodejs.org/docs/v22.20.0/api/modules.html#loading-ecmascript-modules-using-require) feature, which makes file loading a bit more complicated for Mocha 11. In this example, we showcase using ts-node **and** a custom asynchronous module loader to run a TypeScript test file. -This is an advanced example. For a simpler example, see the [`typescript`](../typescript) sibling package. +This example shows more integrations than what most projects need. For a more straightforward example, see the [`typescript`](../typescript) sibling package. From 9930dcd77eb3bb8c6f7c3bca12f6ad91cf2f9d77 Mon Sep 17 00:00:00 2001 From: Kelechi Ebiri Date: Wed, 11 Feb 2026 04:47:27 +0100 Subject: [PATCH 4/7] chore: trigger CI From 8933a876d925251da92ac45f457ac29d4ab62e8a Mon Sep 17 00:00:00 2001 From: Kelechi Ebiri Date: Wed, 18 Feb 2026 20:05:59 +0100 Subject: [PATCH 5/7] fix: use mocha loader flags instead of NODE_OPTIONS for Windows compatibility Signed-off-by: Kelechi Ebiri --- packages/typescript-async-loaders/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typescript-async-loaders/package.json b/packages/typescript-async-loaders/package.json index 1963bf7..71f68a8 100644 --- a/packages/typescript-async-loaders/package.json +++ b/packages/typescript-async-loaders/package.json @@ -6,7 +6,7 @@ "node": "^18.18.0 || ^20.9.0 || >=21.1.0" }, "scripts": { - "test": "NODE_OPTIONS='--loader=ts-node/esm --loader=./custom-loader.mjs' mocha test/**/*.test.ts" + "test": "mocha --loader=ts-node/esm --loader=./custom-loader.mjs test/**/*.test.ts" }, "devDependencies": { "mocha": "^11.7.0", From 4cea5060ab2ecebc05a56f6c68512954889ab4af Mon Sep 17 00:00:00 2001 From: Kelechi Ebiri Date: Sun, 1 Mar 2026 10:09:38 +0100 Subject: [PATCH 6/7] fix: chain loaders properly to allow ts-node to compile TypeScript Signed-off-by: Kelechi Ebiri --- packages/typescript-async-loaders/custom-loader.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/typescript-async-loaders/custom-loader.mjs b/packages/typescript-async-loaders/custom-loader.mjs index 3351ae8..3617500 100644 --- a/packages/typescript-async-loaders/custom-loader.mjs +++ b/packages/typescript-async-loaders/custom-loader.mjs @@ -1,7 +1,7 @@ -export async function load(url, context, defaultLoad) { +export async function load(url, context, nextLoad) { if (url.endsWith(".ts")) { console.log("Loaded with custom loader"); await new Promise((resolve) => setTimeout(resolve, 1)); } - return defaultLoad(url, context, defaultLoad); + return nextLoad(url, context); } From 53eec3a4c73f16916457955c808c64af68a3e30b Mon Sep 17 00:00:00 2001 From: Kelechi Ebiri Date: Thu, 19 Mar 2026 23:52:19 +0100 Subject: [PATCH 7/7] fix: register custom loader for TypeScript support in Mocha tests --- packages/typescript-async-loaders/custom-loader.mjs | 5 +++++ packages/typescript-async-loaders/package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/typescript-async-loaders/custom-loader.mjs b/packages/typescript-async-loaders/custom-loader.mjs index 3617500..ef2dc11 100644 --- a/packages/typescript-async-loaders/custom-loader.mjs +++ b/packages/typescript-async-loaders/custom-loader.mjs @@ -1,3 +1,8 @@ +import { register } from "node:module"; +import { pathToFileURL } from "node:url"; + +register("ts-node/esm", pathToFileURL("./")); + export async function load(url, context, nextLoad) { if (url.endsWith(".ts")) { console.log("Loaded with custom loader"); diff --git a/packages/typescript-async-loaders/package.json b/packages/typescript-async-loaders/package.json index 71f68a8..2d72a70 100644 --- a/packages/typescript-async-loaders/package.json +++ b/packages/typescript-async-loaders/package.json @@ -6,7 +6,7 @@ "node": "^18.18.0 || ^20.9.0 || >=21.1.0" }, "scripts": { - "test": "mocha --loader=ts-node/esm --loader=./custom-loader.mjs test/**/*.test.ts" + "test": "mocha --loader=./custom-loader.mjs test/**/*.test.ts" }, "devDependencies": { "mocha": "^11.7.0",