Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha failed assertion causing timeout

I'm getting started with mocha testing framework with NodeJS. Success assertions working fine but if the assertion fails, my test timeouts. For asserting I've tried Should and Expect. For example (async code)

  it('should create new user', function(done){
    userService.create(user).then(function(model){
      expect(model.id).to.be(1); //created user ID
      done();
    }, done)
  });

Here the if model id is not 1 then the test timesout instead of reporting failed assertion. I'm sure I'm doing something wrong. Appreciate your help. Thanks!

like image 537
Amitava Avatar asked Aug 15 '13 02:08

Amitava


People also ask

How do I change my mocha timeout?

Whenever you run Mocha at the command line, it will read this file and set a timeout of 5 seconds by default. Another way which may be better depending on your situation is to set it like this in a top level describe call in your test file: describe("something", function () { this. timeout(5000); // tests... });

What is Mocha test framework?

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub.


1 Answers

Shawn's answer works, but there is a simpler way.

If you return the Promise from your test, Mocha will handle everything for you:

it('should create new user', function() {
  return userService.create(user).then(function(model){
    expect(model.id).to.be(1); //created user ID
  });
});

No done callback needed!

like image 119
danvk Avatar answered Sep 22 '22 18:09

danvk