0

I have this code:

const mocha = require("mocha");
const assert = require("assert");


describe("saving records", function (done) {
 
  it("Saves a record to the database", function (done) {
    
    let mx = false;

    setTimeout(() => {
      mx = false; //#### note this line
    }, 6000);
    done();

    assert(mx === true);
  });
});

I am not sure why, but mocha gives a test pass in the terminal that says:

saving records ✓ Saves a record to the database
1 passing (30ms)

Question no.1 : Why is the test passing at this point

The next weird thing is: It doesn't even wait for 6 seconds seconds before the test passes. Since, I've used done parameter, Question no. 2: shouldn't it wait until the execution is done?

Have I misinterpreted some information?

juztcode
  • 1,196
  • 2
  • 21
  • 46

1 Answers1

1
const mocha = require("mocha");
const assert = require("assert");

describe("saving records", function (done) {
  it("Saves a record to the database", function (done) {
    let mx = false;

    setTimeout(() => {
      mx = false; //#### note this line
      assert(mx === true);
      done();
    }, 6000);
  });
});

In your variant test passes because assert is after done(); Working with async tests described here

A Ralkov
  • 1,046
  • 10
  • 19
  • it seems there's a default for 2 seconds for waiting for the done() to be reached, can we increase this? So, the test won't stand for 6sec in above case, after 2 seconds it's gonna timeout, how do we increase that? – juztcode Apr 04 '21 at 06:00
  • 1
    Timeout can be any, but you chould set maximum value for test. See docs here https://mochajs.org/#timeouts . Use `this.timeout(ms);` in code or `mocha --timeout 5000` in command. Also good answer https://stackoverflow.com/questions/23492043/change-default-timeout-for-mocha – A Ralkov Apr 04 '21 at 12:08