摩卡:超过2000ms的错误超时 [英] Mocha: Error Timeout of 2000ms exceeded

查看:2481
本文介绍了摩卡:超过2000ms的错误超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是 seed.js 文件:

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

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

module.exports = {saveUsersToDB};

我的 app.test.js 文件:

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

before(saveUsersToDB)

当我运行下面的测试是错误我得到:

 快速侦听端口3000! 
1)before all钩子:saveUsersToDB

0传递(2s)
1失败

1)之前钩子:saveUsersToDB:
错误:超出2000ms超时。对于异步测试和钩子,请确保done()被调用;如果返回承诺,确保解决。

npm ERR!测试失败。详见上文。

我以为返回 .then(()=>(done)) 够了吗?我做错了什么?

解决方案

因为(done)返回函数而不是调用它。为了调用完成,你需要这样写。

  .then(()=> done())

但是,我不建议使用完成以及承诺。您只需要返回承诺,然后 mocha 将自动处理。

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


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?

解决方案

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)

这篇关于摩卡:超过2000ms的错误超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆