Skip to content

mocha TDD --bail --exit does not call suiteTeardown() after failed test #3598

@stheine

Description

@stheine

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

Using mocha TDD ( https://mochajs.org/#tdd ), and calling mocha with --bail and --exit. on a failed test, mocha stops and does not execute the suiteTeardown().
My assumption would have been that the suiteTeardown() is always called, even after a failed test.

Steps to Reproduce

suite('suite', () => { 
  suiteSetup(() => { 
    console.log('suiteSetup');
  });

  suiteTeardown(() => { 
    console.log('suiteTeardown');
  });

  test('mytest', done => {
    console.log('mytest');

    done(new Error('fail'));
  });

  test('mytest async', async() => {
    console.log('mytest async');

    throw new Error('fail');
  });
});

no options:

$ mocha --ui tdd test/units/

  suite
suiteSetup
mytest
    1) mytest
mytest async
    2) mytest async
suiteTeardown

  0 passing (11ms)
  2 failing

  1) suite
       mytest:
     Error: fail
      at Context.done (test/units/testTests.js:15:10)

  2) suite
       mytest async:
     Error: fail
      at Context.test (test/units/testTests.js:21:11)

only --bail:

$ mocha --bail --ui tdd test/units/

  suite
suiteSetup
mytest
    1) mytest

  0 passing (9ms)
  1 failing

  1) suite
       mytest:
     Error: fail
      at Context.done (test/units/testTests.js:15:10)

suiteTeardown

only --exit:

$ mocha --exit --ui tdd test/units/

  suite
suiteSetup
mytest
    1) mytest
mytest async
    2) mytest async
suiteTeardown


  0 passing (11ms)
  2 failing

  1) suite
       mytest:
     Error: fail
      at Context.done (test/units/testTests.js:15:10)

  2) suite
       mytest async:
     Error: fail
      at Context.test (test/units/testTests.js:21:11)

both, --bail and --exit:

$ mocha --bail --exit --ui tdd test/units/

  suite
suiteSetup
mytest
    1) mytest

  0 passing (9ms)
  1 failing

  1) suite
       mytest:
     Error: fail
      at Context.done (test/units/testTests.js:15:10)

Expected behavior: I expect that in ALL cases, the suiteTeardown() is executed.

Actual behavior: If I give both, --bail and --exit, the suiteTeardown() is not executed.

Reproduces how often: 100%

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 5.2.0
  • The output of node --version: v10.13.0
  • The version and architecture of your operating system: ubuntu 18.04
  • Your shell (bash, zsh, PowerShell, cmd, etc.): bash
  • Your browser and version (if running browser tests): n/a
  • Any other third party Mocha related modules (with versions): n/a
  • The code transpiler being used: n/a

Additional Information

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions