faq labelnode node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.describe('promise', ()=>{
it('never fails with `done` in finally', (done)=>{
Promise.resolve().then(assert.fail).catch(assert.fail).finally(done)
})
it('no matter what', (done)=>{
Promise.reject().then(assert.fail).catch(assert.fail).finally(done)
})
it('but properly fails when returning', ()=>{
return Promise.reject().then(assert.fail).catch(assert.fail)
})
})
Output I get:
✓ never fails with `done` in finally
(node:83761) UnhandledPromiseRejectionWarning: AssertionError: AssertionError: assert.fail()
(node:83761) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:83761) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
✓ no matter what
(node:83761) UnhandledPromiseRejectionWarning: AssertionError: assert.fail()
(node:83761) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)
1) but properly fails when returning
2 passing (12ms)
1 failing
1) promise
but properly fails when returning:
AssertionError: AssertionError: assert.fail()
Expected behavior: All three tests should fail
Actual behavior: Only last test fails
Reproduces how often: Always
mocha --version and node node_modules/.bin/mocha --version: node --version: v11.1.0
This issue seems related https://github.com/mochajs/mocha/issues/1283 but it does not cover current problem
If you want to make a test failed, you should pass Error object to done like done(new Error()). finally doesn't receive any argument.
A finally callback will not receive any argument, since there's no reliable means of determining if the promise was fulfilled or rejected.
for future reference, if my finally block returns a promise, then mocha will not fail. for example:
try {
// something that fails
} catch (err) {
// log and rethrow
throw(err)
} finally {
return promisify(unlink)(file.name)
}
in this example mocha (6.1.3, running with spectron 5.0.0) never detects the rethrown error; however if the finally block does not return the unlink promise or if it awaits the unlink promise, then mocha fails with the rethrown error, as expected.