Skip to content

🐛 Bug: Top level ESM errors get swallowed and do not fail the suite #5396

Open
@samuelhnrq

Description

@samuelhnrq

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

ESM errors thrown at top level gets silently swallowed (hidden) and does not fail the suite (as I assume is expected). Mocha ignores the error and keeps going. Makes it really annoying to debug whats wrong. Any it tests defined before the error do get "registered" normally though.

Actual

Nothing, simply ignores the error and keeps going

Minimal, Reproducible Example

given a test test/add.test.ts suite like this

import assert from 'node:assert/strict'

throw new Error()

it('should add stuff', () => {
    const b = 1 + 1;
    assert.equal(2, b)
})

Then mocha --extension ts will simply output

(node:64751) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)


  0 passing (0ms)

instead of exposing the error.

Versions

Mocha 11.7.1
Node v24.3.0

Additional Info

Conversely if you change it to

import assert from 'node:assert/strict'

it('should add stuff', () => {
    const b = 1 + 1;
    assert.equal(2, b)
})

throw new Error()

it will then output

Running main test suite...
(node:64877) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)


  ✔ should add stuff

  1 passing (1ms)

Still no mention of the error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: blockedWaiting for something else to be resolvedtype: buga defect, confirmed by a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions