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 flag causes before() hooks to be run even after a failure #1812

Closed
dpatti opened this issue Jul 25, 2015 · 5 comments · Fixed by #1852
Closed

Bail flag causes before() hooks to be run even after a failure #1812

dpatti opened this issue Jul 25, 2015 · 5 comments · Fixed by #1852
Labels
type: bug a defect, confirmed by a maintainer

Comments

@dpatti
Copy link

dpatti commented Jul 25, 2015

Example file:

describe('one', function(){
  it('fails', function(){
    throw Error("failed");
  });
});

var slowHook = function(next) {
  console.log("hook");
  setTimeout(next, 1000);
};

describe('two', function(){
  before(slowHook);
  it('never runs', function(){ });
});

describe('three', function(){
  before(slowHook);
  it('never runs', function(){ });
});

describe('four', function(){
  before(slowHook);
  it('never runs', function(){ });
});

So what I'm seeing is that when you run this with --bail and the first test fails, it keeps going and runs all the other hooks before actually quitting. The output looks like this:

$ mocha --version
2.2.5
$ mocha timeout.js -b


  one
    1) fails

  two
hook

  three
hook

  four
hook


  0 passing (3s)
  1 failing

  1) one fails:
     Error: failed
      at Error (<anonymous>)
      at Context.<anonymous> (timeout.js:3:11)

In our actual code base we have hundreds of test suites each with sometimes lengthy hooks that set up and tear down fixtures in the db. When you run the whole suite with -b, you might see one red dot, and then it appears to hang for over a minute while it is running all the other hooks. Finally it reports with the failure. I can't think of why this would be desirable, so my best guess is that it is a bug.

@okv
Copy link

okv commented Aug 20, 2015

+1 (really looks like bug)

@okv
Copy link

okv commented Aug 20, 2015

Can it be fixed in some way?

@dasilvacontin
Copy link
Contributor

Bug confirmed, thanks for reporting. I couldn't really look at the cause so far.

@dasilvacontin dasilvacontin added type: bug a defect, confirmed by a maintainer status: accepting prs Mocha can use your help with this one! labels Aug 20, 2015
@aaron-j-krause
Copy link
Contributor

I've found a fix for this that passes the tests and I can submit a PR. I'm not exactly sure how to simulate the multiple it and before blocks for the purposes of adding a test for the fix though.

@dasilvacontin
Copy link
Contributor

@aaroncrows This can be helpful: https://github.com/mochajs/mocha/blob/master/test/integration/helpers.js

Usage example: https://github.com/mochajs/mocha/blob/master/test/integration/multiple.done.js

Let us know if you need further help/pointers. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants