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

Should if (err) throw err be if (err) throw done(err)? #1670

Closed
freewind opened this issue Apr 25, 2015 · 3 comments
Closed

Should if (err) throw err be if (err) throw done(err)? #1670

freewind opened this issue Apr 25, 2015 · 3 comments

Comments

@freewind
Copy link

http://mochajs.org/#asynchronous-code

Asynchronous code

Testing asynchronous code with Mocha could not be simpler! Simply invoke the callback when your test is complete. By adding a callback (usually named done) to it() Mocha will know that it should wait for completion.

describe('User', function(){
  describe('#save()', function(){
    it('should save without error', function(done){
      var user = new User('Luna');
      user.save(function(err){
        if (err) throw err;
        done();
      });
    })
  })
})

Just thinking if (err) throw err; should be if (err) throw done(err);, right?

(PS: Can't find the correct file to create a pull request)

@dasilvacontin
Copy link
Contributor

It should work either way: you can let Mocha catch the error that you are throwing asynchronously, or you can pass the error directly to the done callback.

@freewind
Copy link
Author

Cool, great to know that

@dasilvacontin
Copy link
Contributor

No worries, let us know if you bump into any issue. :)

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