确保"done()"称为JS错误



尝试将新元素插入我的mongoDB数据库,当我使用终端进行"npm运行测试"时,它给了我这个错误:

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();
});
});
});

在 mocha 中,对于异步测试,您可以调用done回调或返回 promise。您收到此错误,因为您的测试失败,并且您没有catch块来调用done并显示错误:

describe('Saving records', function() {
it('Saves record to db', function() {
var ppball = new PPB ({
amount: 5
});
return ppball.save().then(function(){
assert(ppball.isNew === false);
return null;
});
});
});

最新更新