Mocha: Error Timeout of 2000ms exceeded

五迷三道 提交于 2019-12-10 12:37:22

问题


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?


回答1:


Because (done) will actually return the function instead of invoking it. In order to call done, you need to write it this way.

.then(() => done())

However, I don't recommend using done along with promises. You just simply need to return the promise then mocha will handle it automatically.

const saveUsersToDB = () => db.User.bulkCreate(users)



回答2:


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

You can increase (in miliiseconds) as follows:

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

https://mochajs.org/#timeouts




回答3:


I had the same isue. This error promps because the 2 seconds timeout, so if your test needs to connect to ddbb it will most provably surpas it.

What I did was to separate all my tests that needed somme kind of connection to external resources into my integration tests folder and then added the next flag in my package.json test script:

"int-test": "mocha --timeout 15000 tests/integration/**/*.test.js --compilers js:babel-register "

Follow this link for other ways to increase the timeout: mocha timout



来源:https://stackoverflow.com/questions/40902493/mocha-error-timeout-of-2000ms-exceeded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!