当使用Mocha测试异步代码并且我的一个断言失败时,所有Mocha都会报告超时错误.有没有办法改善这个?如何知道断言失败的原因以及原因?
mocha
Contact
#getContacts()
1) should return at least 1 contact
0 passing (3s)
1 failing
1) Contact #getContacts() should return at least 1 contact:
Error: timeout of 3000ms exceeded. Ensure the done() callback is being called in this test.
Run Code Online (Sandbox Code Playgroud)
码:
var assert = require("assert");
var contact = require("../lib/contact.js");
var chai = require('chai');
var should = chai.should();
describe('Contact', function() {
describe('#getContacts()', function() {
it('should return at least 1 contact', function(done) {
contact.getContacts().then(function(contacts) {
assert.equal(4,2)
done()
});
})
})
});
Run Code Online (Sandbox Code Playgroud)
问题是断言失败,抛出异常.这导致承诺被拒绝,但没有人注意到.您的代码仅检查承诺是否成功.如果您返回承诺,那么如果承诺被拒绝,mocha将检查它并且未通过测试.
所以你要
it('should return at least 1 contact', function() {
return contact.getContacts().then(function(contacts) {
assert.equal(4,2);
});
});
Run Code Online (Sandbox Code Playgroud)