Mocha: Promise.finally(done) never fails tests

Created on 7 Nov 2018  Â·  2Comments  Â·  Source: mochajs/mocha

Prerequisites

  • [x] Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • [x] Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • [x] 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • [x] Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.

Description

Steps to Reproduce

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

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: , 5.2.0
  • The output of node --version: v11.1.0
  • The version and architecture of your operating system: macOS High Sierra 10.13.2
  • Your shell (bash, zsh, PowerShell, cmd, etc.): default macOS Terminal.app
  • Your browser and version (if running browser tests): no
  • Any other third party Mocha related modules (with versions): chai 4.2.0
  • The code transpiler being used: pure ES6

Additional Information


This issue seems related https://github.com/mochajs/mocha/issues/1283 but it does not cover current problem

invalid

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EdvinOlofsson picture EdvinOlofsson  Â·  3Comments

jamietre picture jamietre  Â·  3Comments

robertherber picture robertherber  Â·  3Comments

luoxi001713 picture luoxi001713  Â·  3Comments

3p3r picture 3p3r  Â·  3Comments