mochajs / mocha

β˜•οΈ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.6k stars 3.01k forks source link

πŸ› Bug: Unhandled rejections cause parallel tests to succeed when they should fail #5184

Open cinder-fish opened 3 months ago

cinder-fish commented 3 months ago

Bug Report Checklist

Expected

mocha to run the rests and exit with a non-0 exit code

Actual

mocha exits with 0

and prints the following output

causes an unhandled rejection
    βœ” throws after 3000ms

file:///Users/Alex.Brown/projects/scratch/mocha-parallel-bug/test/unhandled.rejection.test.js:4
      throw new Error("rejected after 3000ms");
      ^

Error: rejected after 3000ms
    at Timeout._onTimeout (file:///Users/Alex.Brown/projects/scratch/mocha-parallel-bug/test/unhandled.rejection.test.js:4:13)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)

Node.js v22.2.0

  long running test
    1) fails after 5000ms

which includes the failing test but not the final summary

Minimal, Reproducible Example

https://github.com/cinder-fish/mocha-parallel-bug

Essentially one test file sets up a timeout which will throw an unhandled error

And another has a failing test that delays long enough that this fires

When run with parallel: true this passes despite the failing test

When run with parallel: false this fails as expected

Versions

$ mocha --version
zsh: command not found: mocha

$ node_modules/.bin/mocha --version
10.7.0

$ node --version
v22.2.0

Additional Info

No response

JoshuaKGoldberg commented 3 months ago

See also:

...however, per comments in #2797, it looks like Mocha isn't respecting Node unhandled-rejections anymore?

$ npx mocha -n unhandled-rejections=strict; echo "mocha status: $?"                                 

  causes an unhandled rejection
    βœ” throws after 3000ms

  1 passing (84ms)

mocha status: 0

Perhaps this is a regression? Needs investigation.

JoshuaKGoldberg commented 1 month ago

Ok I played around with this more: yes, this is a bug.

The behavior between the default mode & parallel should be the same. An unhandled rejection should be logged & reported.