Skip to content

🛠️ Repo: esm-utils test fails on newer Node 24 because a .ts fixture uses require() #6156

Description

@mostafaNazari702

Tooling Suggestion Checklist

Overview

I was going through some of my older PRs and rebasing them onto latest main and while running the test suite i noticed a failure that had nothing to do with my changes. I checked and it fails on a clean main too so i figured i would report it separately.

One test in test/node-unit/esm-utils.spec.cjs fails:

1) esm-utils
     requireOrImport
       should surface Mocha errors rather than falling back to `import(...)`:

expected Promise (rejected) => ReferenceError('require is not defined in ES module scope,
you can use import instead') to be rejected with error satisfying
{ code: 'ERR_MOCHA_FORBIDDEN_EXCLUSIVITY',
  message: /`\.only` is not supported in parallel mode/ }

The test expects ERR_MOCHA_FORBIDDEN_EXCLUSIVITY but gets a ReferenceError instead.
The fixture it loads, test/node-unit/fixtures/mock-mocha-forbidden-exclusivity-err.ts, is a .ts file that uses CommonJS:
const { createForbiddenExclusivityError } = require('../../../lib/errors.js');

Since the root package.json is now "type": "module", newer Node loads that .ts file as ESM so require is not defined inside it. requireModule in lib/nodejs/esm-utils.cjs then hits the /\.(cts|mts|ts)$/ branch and rethrows the ReferenceError instead of the mocha error the test is looking for.

I copied the fixture to .cts and ran the same call and it gives the expected error:
.cts -> Error | ERR_MOCHA_FORBIDDEN_EXCLUSIVITY | .only is not supported in parallel mode

So renaming the fixture to .cts looks like it fixes it though Im not sure if you would rather keep it as .ts on purpose to cover TS handling.

Additional Info

reproduce on main:
node bin/mocha.js test/node-unit/esm-utils.spec.cjs

Gives 8 passing and 1 failing.
Versions:

mocha: 12.0.0-rc.1 (main: bf76999)
node: v24.17.0
npm: 11.17.0

the reason the CI does not catch it because the test matrix in .github/workflows/mocha.yml pins exact versions (20.19.4, 22.18.0, 24.6.0) as in #5052 and the unit tests run there.
The latest main run is green on 24.6.0. The smoke job does use a floating 24 but it only runs test/smoke/smoke.spec.cjs so it never touches this test, that means this will start failing once the Node 24 pin gets bumped.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: repository toolingconcerning ease of contributionstatus: in triagea maintainer should (re-)triage (review) this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions