-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crashes that happen after test completion are reported with success exit code #3917
Comments
Quite possible Mocha was in process of terminating by time your test threw. |
Process should certainly exit with other code than Problem is that this scenario ends as success in CI, it shouldn't be the case. |
It's not like I don't understand the issue here -- I'm saying it's a race condition... one that you lose! Modify this function from "lib/cli/run-helpers.js" as such and rerun your test. const exitMochaLater = code => {
process.on('exit', () => {
process.exitCode = Math.min(code, 255);
console.log('exitCode set');
});
}; Mocha finishes execution prior to your timeout, so the exit code is already set. $ npm test
> testfailafter@1.0.0 test /var/tmp/testfailafter
> mocha
test
✓ test
1 passing (8ms)
exitCode set
/var/tmp/testfailafter/test.js:4
throw new Error('Unexpected crash');
^
Error: Unexpected crash
at Timeout.setTimeout [as _onTimeout] (/var/tmp/testfailafter/test.js:4:13)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
$ |
My suggestion would be to migrate your specification to "test" directory and $ mkdir test
$ mv test.js test/my.spec.js
$ cat << EOF >> "test/zzz.spec.js"
describe('delay in case of async test failures', function() {
const delay = 5000; // 5 secs
it('should delay Mocha exit in case of poorly written tests', function(done) {
setTimeout(done, delay);
});
});
EOF
$ npm test Mocha will now implode when run, and its |
@plroebuck This is about how specific programmer errors (of which developer is not aware of) are handled by Mocha (and not about tests written specifically like that for some reason). Such error may happen due to missed to be returned promise that in later turn rejects (with Currently Mocha hides such error from CI, and IMO It's a clear bug on Mocha side. To me it seems that can be easily fixed by reconfiguring the const exitMochaLater = code => {
if (!code) process.on('uncaughtException', () => code = 1);
process.on('exit', () => {
process.exitCode = Math.min(code, 255);
});
}; After that, process ends with non success code, which is highly desirable in such case. Currently I workaround this issue with: // Workaround for: https://github.com/mochajs/mocha/issues/3917
process.on('uncaughtException', err => {
if (!process.listenerCount('exit')) return;
// Mocha done it's report, and registered exit code which is not updated in face of a crash
// Ensure we exit with non-success code
process.removeAllListeners('exit');
throw err;
}); |
You rather radically changed the subject at hand from deferred async to Feel free to send PR your other issues. |
Since beginning it's all about uncaught exception that happens after mocha wraps report (and that could be only the result of orphaned async execution). It's exactly what my initial example exposes.
I will, thank you. In a meantime it'll be great if you reopen and relabel it, as I hope we agree now, it's a bug, that can easily be fixed. |
I am a bot that watches issues for inactivity. |
I am a bot that watches issues for inactivity. |
any update on this? if mocha has import error before running the tests, it exit with 0 exit code, our CI reported succses for 2 weeks before we realize something is broke |
Description
Sort of follow up to #3226 which was fixed but it seems not fully.
Steps to Reproduce
Install locall mocha v6.1.4
Create
test.js
test file:npx mocha test.js
Output is valid:
But exit code is not:
This makes such crashes not detected as fails in CI
Versions
mocha --version
andnode node_modules/.bin/mocha --version
: v6.1.4node --version
: v12.2.0The text was updated successfully, but these errors were encountered: