Skip to content

Mocha.run() callback called twice upon error with bail=true, after() hooks called second time #3457

Closed

Description

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • 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.
  • '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
  • 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

The Mocha.prototype.run() callback is called twice upon error with bail set to true. Also, any remaining after hooks are only called second time. Hopefully the examples below are self explanatory.

This behavior gives issues when the Mocha.prototype.run() callback expects cleanup code to have run, and the callback may only be run once.

Steps to Reproduce

package.json:

{
  "dependencies": {
    "mocha": "^5.2.0"
  }
}

mocha.js

const Mocha = require('mocha')

const mocha = new Mocha({ bail: true })
mocha.addFile(`test.js`)

mocha.run(failures => {
    console.log('done')
})

test.js

describe('test', function() {
    it('fails', function() {
        throw new Error()
    })
    after(function() {
        console.log('after')
    })
})

Expected behavior:

node mocha.js

  test
    1) fails
after
  0 passing (4ms)
  1 failing
  1) test
       fails:
  Error
      at Context.<anonymous> (test.js:3:15)
done

Actual behavior:

node mocha.js

  test
    1) fails
  0 passing (4ms)
  1 failing
  1) test
       fails:
  Error
      at Context.<anonymous> (test.js:3:15)
done
after
done

Reproduces how often: Every time.

Versions

mocha 5.2.0
node-8.9.1
npm-6.0.1

Additional information

Monkey patching Runner.prototype.emit to log calls gets this output:

event: start
event: suite
event: suite
  test
event: test
event: fail
    1) fails
event: end

  0 passing (3ms)
  1 failing
  1) test
       fails:
  Error
      at Context.<anonymous> (test.js:3:15)

done
event: test end
event: hook
after
event: hook end
event: suite end
event: suite end
event: end
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions