Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--bail keep executing before/after hooks for remaining suites #937

Closed
arboleya opened this issue Jul 25, 2013 · 6 comments
Closed

--bail keep executing before/after hooks for remaining suites #937

arboleya opened this issue Jul 25, 2013 · 6 comments

Comments

@arboleya
Copy link

When using --bail option I expect all my subsequent suites to be skipped, including all it's before/after hooks.

The beforeEach/afterEach hooks is properly skipped, but the before/after ones keep being executed.

Test

var assert = require('assert');

describe('first suite', function() {
  it('should just fail', function() {
    assert.equal(1, 2);
  });
});

describe('next suite', function() {

  before(function() {
    console.log('before hook: should never be called!');
  });

  after(function() {
    console.log('after hook: should never be called!');
  });

  describe('should never be executed', function() {
    it('should never reach here', function() {
      throw new Error('bang');
    });
  });
});

Running

mocha --ui bdd --reporter spec --bail tests/test.js

Output

  first suite
    1) should just fail

  next suite
before hook: should never be called!
    should never be executed
after hook: should never be called!


  ✖ 1 of 2 tests failed:

  1) first suite should just fail:
     AssertionError: 1 == 2
@tj
Copy link
Contributor

tj commented Jul 25, 2013

there's a bit of debate about how this should behave, running afterEach and after would probably make the most sense

@arboleya
Copy link
Author

Opps, now I see I was outdated (1.9.x instead of 1.12.x).

I updated it now, this behavior has changed completely, now all hooks are ignored after the fail, even the beforeAfter / after related to the failed suite, which is surely bad.

Agree with you on this, afterEach and after should be run. But only for the failed suite, which the before/beforeEach may be already executed.

Is that right?

@park9140
Copy link
Contributor

park9140 commented Aug 2, 2013

Running afterEach and after for all tests which have already been set up with beforeEach and before functions seems like a good idea to me. This would ensure that any persistent data you have generated and clean up in your after/afterEach functions gets cleaned up allowing the tests to be rerun immediately.

I like this because it helps performance of my tests if I didn't have to run a full database rebuild to ensure any bailed tests data is cleaned up.

I haven't seen a pull request for this, I could take a swing at it if no one else is interested.

@vvo
Copy link

vvo commented Sep 11, 2013

Hello,

It seems after() afterAll are not called when using bail() and yes it is bad.

We should fix it.

@metamatt
Copy link

I just ran into this. Using 1.9 and the behavior is exactly as described in the opening post here.

What I would expect/wish to see when using --bail is:

  • no more before/beforeEach hooks fire
  • the after/afterEach hooks for the current suite do fire
  • but the after/afterEach hooks for other suites do not fire

The logic here is: no more tests are going to run (that's what --bail means). before/after/beforeEach/afterEach are tied to tests (beforeEach and afterEach explicitly so; before and after are tied to suites but let's ignore suites which will be entirely skipped by --bail). So skip all before, after, beforeEach, afterEach hooks that are for tests and test suites that will be skipped by --bail. That means you skip both before and after for test suites after the current one, but you still run the after/afterEach hooks for the current suite because you did run the current test and its before/beforeEach hooks.

@travisjeffery
Copy link
Contributor

the op's original issue is fixed by #1043.

though currently the after/afterEach hooks for the suite that failed aren't ran atm. I made an issue for that: #1093

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants