Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha: Error Timeout of 2000ms exceeded

I am trying to seed the database for unit test.

Below is the seed.js file:

.......
const app = require('./app')
const db = app.get('db')

const saveUsersToDB = (done) => {
    db.User.bulkCreate(users)
         .then(() => (done))
}

module.exports = {saveUsersToDB};

My app.test.js file:

.......
const expect = require('expect')
const request = require('supertest')
const {saveUsersToDB} = require('./seed/seed');

before(saveUsersToDB)

When I run the test below is the error I get:

Express listening on port 3000!
  1) "before all" hook: saveUsersToDB

  0 passing (2s)
  1 failing

  1)  "before all" hook: saveUsersToDB:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

npm ERR! Test failed.  See above for more details.

I thought returning .then(() => (done)) was enough? What am I doing wrong?

like image 270
user1107173 Avatar asked Dec 01 '16 04:12

user1107173


People also ask

What is Mocha timeout?

mocha my-spec.js. The default timeout is 2000 milliseconds. A command-line parameter can be used to overwrite it for partial testing: mocha my-spec.js --timeout 5000.

How do I change the default timeout on my mocha?

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 done?

Asynchronous Mocha Tests To correct this, Mocha can provide a “done” callback method that you can call from the beforeEach call, afterEach call, it calls, and more. This “done” parameter, when present in your callback function, tells Mocha that you are writing an asynchronous test.


1 Answers

By default, Mocha tests have a 2 second timeout (which means that the test needs to be completed in 2 seconds).

You can increase it (in milliseconds) as follows:

this.timeout(5000); // this test can take up to 5 seconds

https://mochajs.org/#timeouts

like image 189
Kalman Avatar answered Sep 22 '22 12:09

Kalman