我不断收到超时错误,它不断告诉我是否已调用done(),即使我已经调用了。
const mocha = require('mocha');
const assert = require('assert');
const Student = require('../models/student.js');
describe('CRUD Tests',function(){
it('Create Record',function(done){
var s = new Student({
name: "Yash"
});
s.save().then(function(){
assert(s.isNew === false);
done();
});
});
});
结果是 -
CRUD Tests 1) Create Record
0 passing (2s) 1 failing
1) CRUD Tests Create Record: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/yash/Documents/Development/Node/MongoCRUD/test/CRUD_test.js)
最佳答案
请注意,正如所写,您的单元测试忽略了 save()
可能拒绝而不是解析的事实。每当您使用此 done
构造时,请确保您的单元测试能够处理错误场景,如下所示:
s.save().then(function() {
assert(s.isNew === false);
done();
}).catch(error => {
done(error);
});
或者,由于 Mocha 内置了对 Promise 的支持,您可以删除 done
参数并直接返回 Promise,如下所示:
it('Create Record', function() {
// ...
return s.save().then(function() {
assert(s.isNew === false);
});
});
这种方法的优点是拒绝 promise 将自动使测试失败,并且您不需要任何 done()
调用。
关于javascript - 为什么我一直面对摩卡 "timeout error"; Node 还一直告诉我兑现我的 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54125817/