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

browser - exceptions thrown in before/beforeEach are ignored #270

Closed
spmason opened this issue Feb 17, 2012 · 14 comments
Closed

browser - exceptions thrown in before/beforeEach are ignored #270

spmason opened this issue Feb 17, 2012 · 14 comments

Comments

@spmason
Copy link
Contributor

spmason commented Feb 17, 2012

I'm finding that exceptions thrown in my before/beforeEach blocks aren't being handled, it's simple enough to reproduce:

    mocha.setup('bdd');
    describe('testing the tests', function(){
        beforeEach(function(){ // also before(), 
            throw new Error('this should fail');
        });

        it('Works', function(){
        });
    });
    mocha.run();

This is on the most recent version, 0.12.1 on Chrome 17 and FireFox 10

@weepy
Copy link

weepy commented Feb 20, 2012

I get the same in node

@kern
Copy link

kern commented Mar 2, 2012

+1. It appears like uncaught exceptions have their "uncaught" property set only when thrown from the actual test cases, not the hooks.

@grahamscott
Copy link

+1 Seeing this in Chrome 17 with mocha@0.14.0

@talos
Copy link

talos commented Apr 14, 2012

@nonplus 34abc71 fixed the problem for me. Thanks!

@talos
Copy link

talos commented Apr 14, 2012

This was equivalently fixed with commit 701c538, thus this is a duplicate of issue 361.

@tj
Copy link
Contributor

tj commented Jul 25, 2012

hey sorry guys i didnt see this one, are you guys still finding this?

@funston
Copy link

funston commented Dec 7, 2012

hey, yeah we are still seeing this.

var assert = require('assert');
suite("setup fails",function(done){
setup(function(done){
assert.equal(0,1);
done();
});

teardown(function(done){
    console.log("TEARDOWN");
    done();
});

test('foo',function(done){
    assert.equal(1,0);
    done();
});

});

@tj
Copy link
Contributor

tj commented Dec 7, 2012

I get the following as expected:

  ․

  ✖ 1 of 1 test failed:

  1) something "before each" hook:
     Error: boom
      at Context.<anonymous> (/Users/tj/projects/mocha/test.js:4:11)
      at Hook.Runnable.run (/Users/tj/projects/mocha/lib/runnable.js:196:15)
      at next (/Users/tj/projects/mocha/lib/runner.js:235:10)
      at Runner.hook (/Users/tj/projects/mocha/lib/runner.js:246:5)
      at process.startup.processNextTick.process._tickCallback (node.js:244:9)
  beforeEach(function(done){
    throw new Error('boom')
  })

@funston
Copy link

funston commented Dec 8, 2012

Okay, are you seeing the teardown function called after that ?

@bitmage
Copy link

bitmage commented Jan 29, 2013

I have about 140 tests on a certain app. Whenever an error occurs within a before/after hook the entire suite stops running. I think this is related to the error not being handled, as the OP mentions. The output looks fine to me, I would just prefer to keep the rest of the tests running. I am running mocha 1.8.1 and node 0.8.18.

@danielstjules
Copy link
Contributor

Not sure when the issue was fixed, but it's no longer a problem in 2.2.0 :)

// When thrown in beforeEach

$ cat beforeEachError.js
describe('issue-270', function() {
  before(function() {
    console.log('before hook');
  });

  beforeEach(function() {
    console.log('beforeEach hook');
    throw new Error('beforeEach hook error');
  });

  afterEach(function() {
    console.log('afterEach hook');
  });

  after(function() {
    console.log('after hook');
  });

  it('test', function() {});
});

// Output

$ mocha beforeEachError.js

  before hook
beforeEach hook

  ․afterEach hook
after hook


  0 passing (6ms)
  1 failing

  1) issue-270 "before each" hook:
     Error: beforeEach hook error
      at Context.<anonymous> (beforeEachError.js:8:11)

// An example when thrown in before hook

$ cat beforeError.js
describe('issue-270', function() {
  before(function() {
    console.log('before hook');
    throw new Error('before hook error');
  });

  beforeEach(function() {
    console.log('beforeEach hook');
  });

  afterEach(function() {
    console.log('afterEach hook');
    invoked++;
  });

  after(function() {
    console.log('after hook');
  });

  it('test', function() {});
});

// Output

$ mocha beforeError.js

  before hook

  ․after hook


  0 passing (6ms)
  1 failing

  1) issue-270 "before all" hook:
     Error: before hook error
      at Context.<anonymous> (beforeError.js:4:11)

@bitmage
Copy link

bitmage commented Mar 9, 2015

Wonderful, thanks! I just didn't want it to slip under the radar.

On Mar 7, 2015, at 3:39 PM, "Daniel St. Jules" notifications@github.com wrote:

Not sure when the issue was fixed, but it's no longer a problem in 2.2.0 :)

// When thrown in beforeEach

$ cat beforeEachError.js
describe('issue-270', function() {
before(function() {
console.log('before hook');
});

beforeEach(function() {
console.log('beforeEach hook');
throw new Error('beforeEach hook error');
});

afterEach(function() {
console.log('afterEach hook');
});

after(function() {
console.log('after hook');
});

it('test', function() {});
});

// Output

$ mocha beforeEachError.js

before hook
beforeEach hook

․afterEach hook
after hook

0 passing (6ms)
1 failing

  1. issue-270 "before each" hook:
    Error: beforeEach hook error
    at Context. (beforeEachError.js:8:11)

// An example when thrown in before hook

$ cat beforeError.js
describe('issue-270', function() {
before(function() {
console.log('before hook');
throw new Error('before hook error');
});

beforeEach(function() {
console.log('beforeEach hook');
});

afterEach(function() {
console.log('afterEach hook');
invoked++;
});

after(function() {
console.log('after hook');
});

it('test', function() {});
});

// Output

$ mocha beforeError.js

before hook

․after hook

0 passing (6ms)
1 failing

  1. issue-270 "before all" hook:
    Error: before hook error
    at Context. (beforeError.js:4:11)

    Reply to this email directly or view it on GitHub.

@danielstjules
Copy link
Contributor

No problem! Happy it all worked out.

@bitwiseman
Copy link
Contributor

Fantastic.

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

Successfully merging a pull request may close this issue.

10 participants