From a584ca43e21d07e9d6fcc3f7792ab39644851a1e Mon Sep 17 00:00:00 2001 From: finalerock44 <77282157+finalerock44@users.noreply.github.com> Date: Mon, 14 Sep 2026 17:37:59 +0100 Subject: [PATCH] fix: register tsx's ESM loader only so mocha 12.0.1 can load find-up@8 Bare `tsx` also patches the CJS resolver, which turns an ESM dependency's `import` into a `require`. Mocha 12.0.1 bumped find-up 5 -> 8 (ESM-only), whose unicorn-magic dependency exports nothing under the `require` condition, so the suite died with ERR_PACKAGE_PATH_NOT_EXPORTED before running a test. `tsx/esm` registers just the ESM loader and leaves the require(esm) path to Node. Co-Authored-By: Claude Opus 5 (1M context) --- .mocharc.json | 2 +- scripts/test-runner.mjs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.mocharc.json b/.mocharc.json index b771cc0..160f481 100644 --- a/.mocharc.json +++ b/.mocharc.json @@ -1,5 +1,5 @@ { - "node-option": ["import=tsx"], + "node-option": ["import=tsx/esm"], "extensions": ["ts"], "watch-extensions": ["ts"], "recursive": true, diff --git a/scripts/test-runner.mjs b/scripts/test-runner.mjs index c6a4441..9e873ca 100755 --- a/scripts/test-runner.mjs +++ b/scripts/test-runner.mjs @@ -162,9 +162,16 @@ async function runTests() { } // Run tests. Mocha + .mocharc.json handle TypeScript loading via `tsx` - // (see `node-option: ["import=tsx"]` there). Mocha 11 imports files as + // (see `node-option: ["import=tsx/esm"]` there). Mocha imports files as // ESM, so the `require: ts-node/register` hook doesn't get applied; tsx // registers an ESM loader that resolves TS relative imports correctly. + // It has to be `tsx/esm` rather than bare `tsx`: the latter also patches + // the CJS resolver, which rewrites an ESM dependency's `import` into a + // `require` and then cannot resolve an import-only `exports` map. Mocha's + // own lib/cli/*.cjs requires ESM-only find-up@8, whose unicorn-magic + // dependency exports nothing under the `require` condition — plain Node + // loads that fine via require(esm); tsx's CJS hook throws + // ERR_PACKAGE_PATH_NOT_EXPORTED before a single test runs. console.log('Running tests...'); const mochaArgs = [ 'mocha',