Mocha timeout exceeded no matter what Mocha timeout exceeded no matter what mongoose mongoose

Mocha timeout exceeded no matter what


Inside your wrapper, you have this:

// Call the actual test suite, pass it the auth credentials.describe("Test Suite", function() {    it("should run the test suite", function(done) {        // No matter what the timeout is set to it still exceeds it        this.timeout(5000);        test_suite({            email: email,            password: password,            token: token,            admin: admin        }, done);    });});

And the test_suite function contains more calls to describe and it. Mocha won't raise any error if you do this but it will not work the way you'd expect. Mocha executes tests like this:

  1. Mocha discovers the tests. describe calls register new suites with Mocha. Their callbacks are executed right away. it calls register new tests with Mocha. Their callbacks are executed when Mocha runs the tests. Hooks calls (before, after, etc.) are also registering hooks with Mocha, to be executed later, when Mocha runs the tests.

  2. Mocha runs the tests that were registered.

When you put describe inside it there's a problem: this describe will be executed and Mocha will register a new suite, but by the time it is registered, the flow of execution is outside all of your describe callbacks. So this new suite is registered on an anonymous top-level suite (that Mocha creates automatically) and inherits its timeout value from that top-level suite. Check out this example:

describe("top", function () {    it("test", function () {        this.timeout(5000);        describe("inner", function () {            it("inner test", function (done) {                setTimeout(function () {                    done();                }, 6000);            });        });    });    describe("inner 2", function () {        it("inner test 2", function () {});    });});describe("top 2", function (){    it("test 3", function () {});});

If you run it, you get:

  top    ✓ test     inner 2      ✓ inner test 2   top 2    ✓ test 3   inner    1) inner test  3 passing (2s)  1 failing  1) inner inner test:     Error: timeout of 2000ms exceeded     [... etc ...]

Note how the inner suite, even though it appears inside top in the JavaScript code is shown outside of it in Mocha's report. (inner 2, on the other hand, appears exactly where it should.) This is what I was explaining above: by the time Mocha registers this suite, the flow of execution is outside the top and top 2 describe calls. Note also how the timeout call is useless.

If you run the same code above but with mocha --timeout 7000, the test will pass because the default timeout value, including for the anonymous suite Mocha creates is now 7000.

Also, your suite currently requires a certain order between the tests. Mocha is not designed for this. Setting up the fixtures for your tests should be done in before or beforeEach hooks, and tearing them down should be done in after and afterEach. So it is not just a matter of taking the describe out of the it call.