When testing async code with Mocha and one of my asserts fails, all Mocha does is to report a timeout error. Is there a way to improve this? How to know what asserts failed and why?
当使用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.
Code:
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()
});
})
})
});
3 个解决方案
#1
6
The issue is that the assertion fails, which throws an exception. This causes the promise to be rejected, but there isn't anyone to notice. Your code only checks if the promise succeeds. If you return the promise, then mocha will check for it and fail the test if the promise is rejected.
问题是断言失败,抛出异常。这导致承诺被拒绝,但没有人注意到。您的代码仅检查承诺是否成功。如果您返回承诺,那么如果承诺被拒绝,mocha将检查它并且未通过测试。
So you want
所以你要
it('should return at least 1 contact', function() {
return contact.getContacts().then(function(contacts) {
assert.equal(4,2);
});
});
#2
2
You should return the promise like this:
你应该像这样回复承诺:
it('should return at least 1 contact', function() {
return contact.getContacts().then(function(contacts) {
assert.equal(4,2);
});
});
#3
1
It seems like when the assert throws an error that error is swallowed and never shown and also the code after assert throws is skipped.
似乎当assert抛出错误时,错误被吞下并且从未显示,并且跳过断言抛出后的代码也是如此。
Try like this (catching the reject):
尝试这样(捕获拒绝):
it('should return at least 1 contact', function(done) {
contact.getContacts().then(function(contacts) {
assert.equal(4,2)
done()
}).then(null, function (err) {
console.error(err);
done(err);
});
})
Or instead of then(null, rejectFunc) use catch(rejectFunc) with libs like bluebird.
或者代替那时(null,rejectFunc)使用catch(rejectFunc)和像bluebird这样的库。
Also the answer by idbehold is great. I didn't know yet that mocha supports promises directly and I always use the done param knowing if I have a timeout without a stack trace there was a swallowed error in this test.
idbehold的答案也很棒。我还不知道mocha直接支持promises并且我总是使用done param知道如果我有一个没有堆栈跟踪的超时,那么在这个测试中有一个吞下的错误。
#1
6
The issue is that the assertion fails, which throws an exception. This causes the promise to be rejected, but there isn't anyone to notice. Your code only checks if the promise succeeds. If you return the promise, then mocha will check for it and fail the test if the promise is rejected.
问题是断言失败,抛出异常。这导致承诺被拒绝,但没有人注意到。您的代码仅检查承诺是否成功。如果您返回承诺,那么如果承诺被拒绝,mocha将检查它并且未通过测试。
So you want
所以你要
it('should return at least 1 contact', function() {
return contact.getContacts().then(function(contacts) {
assert.equal(4,2);
});
});
#2
2
You should return the promise like this:
你应该像这样回复承诺:
it('should return at least 1 contact', function() {
return contact.getContacts().then(function(contacts) {
assert.equal(4,2);
});
});
#3
1
It seems like when the assert throws an error that error is swallowed and never shown and also the code after assert throws is skipped.
似乎当assert抛出错误时,错误被吞下并且从未显示,并且跳过断言抛出后的代码也是如此。
Try like this (catching the reject):
尝试这样(捕获拒绝):
it('should return at least 1 contact', function(done) {
contact.getContacts().then(function(contacts) {
assert.equal(4,2)
done()
}).then(null, function (err) {
console.error(err);
done(err);
});
})
Or instead of then(null, rejectFunc) use catch(rejectFunc) with libs like bluebird.
或者代替那时(null,rejectFunc)使用catch(rejectFunc)和像bluebird这样的库。
Also the answer by idbehold is great. I didn't know yet that mocha supports promises directly and I always use the done param knowing if I have a timeout without a stack trace there was a swallowed error in this test.
idbehold的答案也很棒。我还不知道mocha直接支持promises并且我总是使用done param知道如果我有一个没有堆栈跟踪的超时,那么在这个测试中有一个吞下的错误。