Skip to content

🐛 Bug: '.only' in parallel, typscript test gives confusing error #5579

@sam-super

Description

@sam-super

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

Given a test file written in typescript which has .only on a test, it should show you the intended error:

  1) Uncaught error outside test suite:
     Uncaught Error: `.only` is not supported in parallel mode
      at Suite.<anonymous> (test/whatever.test.ts:580:6)
      at Object.<anonymous> (test/whatever.test.ts:112:1)
      at Module._compile (node:internal/modules/cjs/loader:1761:14)
      at Module.m._compile (node_modules/ts-node/src/index.ts:1618:23)
      at node:internal/modules/cjs/loader:1893:10

Actual

(node:16351) Warning: Failed to load the ES module: [...]/test/has-dot-only.test.ts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

  1) Uncaught error outside test suite

  0 passing (230ms)
  1 failing

  1) Uncaught error outside test suite:
     Uncaught [...]/test/has-dot-only.test.ts:2
import 'mocha';
^^^^^^

SyntaxError: Cannot use import statement outside a module
      at loadCJSModule (node:internal/modules/esm/translators:127:25)
      at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:239:7)
      at ModuleJob.run (node:internal/modules/esm/module_job:377:25)
      at processTicksAndRejections (node:internal/process/task_queues:103:5)
      at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:671:26)

Minimal, Reproducible Example

https://github.com/sam-super/mocha-ts-only-bug

Versions

mocha 11.7.5
node v24.11.1

Additional Info

demonstrates issues with reporting error when .only is used.

Conditions:

  1. In parallel mode
  2. Typescript via ts-node
  3. Using CJS (not ESM)
  4. Node run with option --no-experimental-detect-module
  5. have import or other non-CJS in the test file
  6. have .only on a test

The cause of the issue is similar to this: #5572

The mocha error thrown when parsing the module triggers a fallback to import() which bypasses ts-node CJS loader and so buries the actual error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: in triagea maintainer should (re-)triage (review) this issuetype: buga defect, confirmed by a maintainer

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions