-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
status: in triagea maintainer should (re-)triage (review) this issuea maintainer should (re-)triage (review) this issuetype: buga defect, confirmed by a maintainera defect, confirmed by a maintainer
Milestone
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
faqlabel, 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:
- In parallel mode
- Typescript via ts-node
- Using CJS (not ESM)
- Node run with option
--no-experimental-detect-module - have
importor other non-CJS in the test file - have
.onlyon 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
Labels
status: in triagea maintainer should (re-)triage (review) this issuea maintainer should (re-)triage (review) this issuetype: buga defect, confirmed by a maintainera defect, confirmed by a maintainer
Type
Projects
Status
No status