faq labelnode node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.When using the bail flag before hook failures do not exit the test as a failure.
sample test:
//index.js
const assert = require('assert');
describe('some test', () => {
before(async () => {
throw new Error('should die');
});
it('should not get here', () => {
assert(false);
});
});
command:
mocha --bail index.js
output:
some test
0 passing (6ms)
1) "before all" hook
Although its hard to see here, the test is exiting with a 0 code. You can see it better by running:
mocha --bail index.js && echo 'hello'
some test
0 passing (9ms)
1) "before all" hook
hello
The echo 'hello' should never get hit but it does.
Similarly, running this without --bail works as expected, the process exits with a non-zero code:
mocha index.js && echo 'hello'
output:
some test
1) "before all" hook
0 passing (7ms)
1 failing
1) some test
"before all" hook:
Error: should die
at Context.<anonymous> (index.js:5:15)
Note: Regardless of before block structure this bug still occurs
const assert = require('assert');
describe('some test', function() {
before(function() {
throw new Error('should die');
});
it('should not get here', function() {
assert(false);
});
});
// still bad
Expected behavior: Test should fail if there is an error in before hook
Actual behavior: Test does not fail
Reproduces how often: 100%
mocha v5.0.5 global and local (tested version 4 through 5 and 5.0.5 seems to be the only affected version)
macOS
zsh
Related to: https://github.com/mochajs/mocha/issues/3096
can confirm this issue is real. we ended up checking in a bunch of failing code because of it. this is the change that caused it:
FYI, issue affects "after" hooks as well.
Most helpful comment
FYI, issue affects "after" hooks as well.