Skip to content

fix: change OR to AND in TS file extension check for ERR_UNKNOWN_FILE_EXTENSION - #5811

Closed
mark-wiemer with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-mocha-v117-3-4-error
Closed

mark-wiemer with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-mocha-v117-3-4-error

Conversation

Copilot AI commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

PR #5408 introduced a regression where the condition guarding TS file error handling used || instead of &&. This caused any .ts file that fails both require() and import() to always surface the unhelpful requireErr, masking the actual runtime error from import().

The PR description stated the intent correctly ("if the file is .cts, .mts, or .ts and the error is ERR_UNKNOWN_FILE_EXTENSION"), but the code used OR.

Changes

  • lib/nodejs/esm-utils.js: || → && in the requireModule error handler
  • test/node-unit/fixtures/esm-ts-runtime-error/: New fixture — a .ts file with top-level await in an ESM package that throws a runtime error, triggering the exact bug scenario (require() → ERR_REQUIRE_ASYNC_MODULE, import() → actual error)
  • test/node-unit/esm-utils.spec.js: Test asserting the importErr is surfaced, not requireErr
// Before: any .ts file failure always threw requireErr
if (/\.(cts|mts|ts)$/.test(file) || importErr.code === "ERR_UNKNOWN_FILE_EXTENSION")

// After: requireErr only thrown when import fails specifically due to unknown extension
if (/\.(cts|mts|ts)$/.test(file) && importErr.code === "ERR_UNKNOWN_FILE_EXTENSION")
Original prompt

This section details on the original issue you should resolve

<issue_title>🐛 Bug: Mocha v11.7.3 and v11.7.4 fails with error ERR_REQUIRE_ESM</issue_title>
<issue_description>### Bug Report Checklist

  • I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
  • I have searched for related issues and issues with the faq label, but none matched my issue.
  • I have 'smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, my usage of Mocha, or Mocha itself.
  • I want to provide a PR to resolve this

Expected

Unit tests runs as expected

Actual

Unit tests fail with error ERR_REQUIRE_ESM

After some investigation, this problem is the same as #5387 which was fixed in 11.7.1 and 11.7.2 but is broken again in 11.7.3 and 11.7.4 .
The problem has been re-introduced with PR #5408 and the issue might be in lib/nodejs/esm-utils.js line 108 in the PR, the condition reads

      if (
        /\.(cts|mts|ts)$/.test(file) || // <- or
        importErr.code === 'ERR_UNKNOWN_FILE_EXTENSION'
      ) {
        throw requireErr;
      }

but in the PR description it is

If the file being imported is a .cts, .mts, or .ts and the caught error from import() is for an unknown file extension, we know it's from a Node.js version that doesn't support those file extensions.

or in the code, and in description.

Note : While making the minimal reproducible example, it appears that if the imported module has no async bit the tests are not run and fail silently.

Minimal, Reproducible Example

// src/main.test.ts
import "./other.js";

describe("the test", function () {
  it("should work", function () {
    console.log("ok");
  });
});
// src/other.ts
export const ok = () => {
  console.log("ok");
};

// there must be an async involved somewhere or the error does not trigger
await Promise.resolve("something");

// simulate an error in a module
throw new Error("stop");
npx mocha '--import=data:text/javascript,import{register}from"node:module";import{pathToFileURL}from"node:url";register("ts-node/esm",pathToFileURL("./"));' --enable-source-maps --inspect 'src/*.test.ts'

Versions

v11.7.3, v11.7.4
Nodejs v24.3.0

Additional Info

No response</issue_description>

<agent_instructions>## Postmortem

I never fully wrapped my head around the exact problem here. Why was Node loading modules in a different way based on whether nyc had been enabled? How can we track that to diagnose future issues?

I'll be updating this comment as I reflect more, I plan to reproduce the issue in excruciating detail so that I can summarize it precisely.

Open questions

  • What is a customization hook?
  • How does Node's module loading system work?
  • How can I get docs for an exact patch version of Node, not just LTS?

References



@copilot draft a report about why this error happened, it's quite complex. Organize sources so that the report is readable.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@mark-wiemer Thanks guys, let me confirm with @JoshuaKGoldberg but changing "or" to "and" looks like a good candidate for a fix @mark-wiemer I'll note this bug only repros if Node is already in "module" mode. For Mocha, since it doesn't have `"type": "module"` in its package.json, that's not the case. The repro commit above does work, and changing the mentioned "or" to an "and" does fix it. This looks like the intended fix, I'll just need to find a way to write an automated test for this within Mocha! @mark-wiemer Ref: - https://github.com//issues/5361#issuecomment-3368708708 - https://nodejs.org/docs/v22.20.0/api/module.html#customization-hooks - https://github.com/mochajs/mocha/compare/fix-5361-esm-loader @JoshuaKGoldberg 👍 Great investigation @mark-wiemer, I concur!

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Mar 24, 2026 •

Copy link
Copy Markdown

CLA Not Signed

…_EXTENSION

Fixes #5494. The condition in requireModule was using || (OR) when it
should use && (AND). This caused any .ts file that failed both require()
and import() to always throw the require() error, even when the import()
error was more informative (e.g., a runtime error vs ERR_REQUIRE_ASYNC_MODULE).

Co-authored-by: mark-wiemer <7833360+mark-wiemer@users.noreply.github.com>
Agent-Logs-Url: https://github.com/mochajs/mocha/sessions/ddee8dff-9212-4d12-9857-f9321ec72463
Copilot AI changed the title [WIP] Fix bug with Mocha v11.7.3 and v11.7.4 ERR_REQUIRE_ESM error fix: change OR to AND in TS file extension check for ERR_UNKNOWN_FILE_EXTENSION Mar 24, 2026
Copilot AI requested a review from mark-wiemer March 24, 2026 15:20
@mark-wiemer

Copy link
Copy Markdown
Member

Meh, bad bot.

@mark-wiemer
mark-wiemer deleted the copilot/fix-mocha-v117-3-4-error branch April 30, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug: Mocha v11.7.3 and v11.7.4 fails with error ERR_REQUIRE_ESM

2 participants