Naven Gaddam
Naven Gaddam

Reputation: 1

Mocha testing difference between done() and done as paramter to function

please explain difference between done() method and done keyword passed as a parameter to a function?

it("qwerty",function(done){
  ------
  ------
  done();
});

it('qwerty', function(done){
  ----------
  ----------
  .expect(404, done);
})

Please explain the differences and how many times i can call done() in loop if i am calling like 15 times i am getting an error "multiple times done() called"

Upvotes: 0

Views: 82

Answers (1)

Sergey Lapin
Sergey Lapin

Reputation: 2693

In the first example, you call it explicitly. In the second one, you pass done function as a callback to expect. It checks the response status (I assume you use supertest library) and if it equals to 404, calls done function without parameter (no error). Otherwise it calls done with something like an instance of assertation error, so mocha knows that this is a failed test.

As a regular callback, done is not supposed to be called multiple times. It should be called only once, denoting the end of some action, the test in your case. If you are looking for the ability to fail the test, just throw it.

Upvotes: 1

Related Questions