Why does a Mocha test suite pass locally but fail in CI when using the --exit flag?
0 reputation · 08 Jun 2021, 15:03 UTC
Goal: Ensure that a Mocha test suite exits with the same status code and warning output when run locally on a developer machine and in a CI container.
Constraints: Locally the suite finishes quickly with no warnings, but in CI the same command sometimes fails due to exceeding the default 2000 ms test timeout, lingering asynchronous operations that keep the Node process alive when --exit is used, or because CI’s Node.js configuration treats unhandled promise rejections as failures. Additionally, the effective Mocha options may differ if a mocha.opts file or package.json mocha object is overridden by environment‑specific scripts or MOCHA_OPTS.
Specific questions: Does enabling the --exit flag guarantee immediate process termination in CI when pending async operations remain? Which configuration source (mocha.opts, package.json, CLI) takes precedence when CI scripts set MOCHA_OPTS? How can one verify that unhandled promise rejections are not being converted to failures by CI’s Node.js strict mode?