From 28fbc465673af30324152a39096e4bee01c48b95 Mon Sep 17 00:00:00 2001 From: Kelechi Ebiri Date: Sun, 21 Sep 2025 14:12:15 +0100 Subject: [PATCH 1/3] 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/3] 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/3] 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.