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

Exception within async before hook prevent other test suite to be run #1465

Closed
thibaut-sticky opened this issue Dec 12, 2014 · 2 comments
Closed

Comments

@thibaut-sticky
Copy link

Hi all,

This issue is simular to #581 but in the async world. I'm using [email protected].

Here a simple test to reproduce my issue:

describe('first', function(){
  before(function(done){
    process.nextTick(function(){
      throw new Error('failed')
    });
  })

  it('should do something', function(done){
    done()
  })
})

describe('second', function(){
  it('should something', function(done){
    done()
  })
})

As you can see, I have a first test suite with a before hook throwing an exception. I'm expecting this first test suite to failed.
Then, I have an other test suite, outside the first one. This one should passed.

Now, here what happen:

first
    1) "before all" hook

  0 passing (3ms)
  1 failing

  1) first "before all" hook:
     Uncaught Error: failed
      at /home/tetienne/test.js:4:13
      at process._tickCallback (node.js:419:13)

The second test suite is not run at all.

In the sync world, it works as expected:

describe('first', function(){
  before(function(){
      throw new Error('failed');
  })

  it('should do something', function(){

  })
})

describe('second', function(){
  it('should something', function(){
  })
})
  first
    1) "before all" hook

  second
    ✓ should something 


  1 passing (4ms)
  1 failing

  1) first "before all" hook:
     Error: failed
      at Context.<anonymous> (/home/tetienne/test2.js:3:13)
      at callFn (/home/tetienne/.node_modules/lib/node_modules/mocha/lib/runnable.js:250:21)
      at Hook.Runnable.run (/home/tetienne/.node_modules/lib/node_modules/mocha/lib/runnable.js:243:7)
      at next (/home/tetienne/.node_modules/lib/node_modules/mocha/lib/runner.js:258:10)
      at Object._onImmediate (/home/tetienne/.node_modules/lib/node_modules/mocha/lib/runner.js:275:5)
      at processImmediate [as _immediateCallback] (timers.js:345:15)

So to sum up, as soon as an async before hook catch an error, all the next tests, even unrelated, failed.

Have you any idea on what is wrong here?

@hallas
Copy link

hallas commented Dec 12, 2014

You throw an error that is unhandled. Read the error message. "Uncaught Error" means that something fucked up and there was no one to clean up. The program has no choice but to exit.

In the async world, you need to manage errors by using the callback parameter when creating async test. This just means before(function (done) { done(new Error('failed')); }).

@hallas hallas closed this as completed Dec 12, 2014
@thibaut-sticky
Copy link
Author

Thx for your explanation. I thougth the exception was catch by the it function.

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

2 participants