Skip to content

When using bail flag, before hook failures do not exit as failed  #3303

@marcusmotill

Description

@marcusmotill

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

When using the bail flag before hook failures do not exit the test as a failure.

Steps to Reproduce

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%

Versions

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

Additional Information

Related to: #3096

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: accepting prsMocha can use your help with this one!type: buga defect, confirmed by a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions