确保“done()”被称为JS错误

时间:2017-08-10 17:14:13

标签: javascript node.js mongodb mocha

尝试将新元素插入到我的mongoDB数据库中,当我使用“npm run test”终端时,它给了我这个错误:

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

这是我将元素保存到我的数据库的代码:

const mocha = require('mocha');
const assert = require('assert');
const PPB = require('../Models/Pingpongballs');

describe('Saving records', function() {
  it('Saves record to db', function(done) {
    var ppball = new PPB ({
      amount: 5
    });

    ppball.save().then(function() {
      assert(ppball.isNew === false);
      done();
    });
  });
});

1 个答案:

答案 0 :(得分:3)

在mocha中,对于异步测试,您可以调用[OutputCache(NoStore = true, Duration = 0)] 回调或返回承诺。您收到此错误,因为您的测试失败了,并且您没有done阻止调用catch并出错:

done