使摩卡测试显示实际错误
Make mocha tests show the actual error
我觉得 Mocha 令人沮丧的一件事是,当测试失败时,它们不会给出失败行的实际错误消息,相反,它们只是以 Error: timeout of超过 2000 毫秒。确保在此测试中调用了 done() 回调。
以这个测试为例:
describe("myTest", function() {
it("should return valid JSON.", function(done) {
api.myCall("valid value").then(function(result) {
console.log(result);
var resultObj = JSON.parse(result);
assert.isFalse(resultObj.hasOwnProperty("error"), "result has an error");
done();
});
});
});
输出为:
myTest
{"error":null,"status":403}
1) should return valid JSON.
0 passing (2s)
1 failing
1) myTest should return valid JSON.:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
assert.isFalse 失败,但未显示应显示的消息 ("result has an error")。事实上,处理似乎就此停止,因为 done() 从未被调用过。去掉那一行,测试通过了,因为 done() 被调用了。
所以,我错过了什么?为什么摩卡测试会这样?我使用的实际测试库是:
var assert = require("chai").assert;
有谁知道我做错了什么或者为什么会这样?
您的 API 似乎在使用 promises。在尝试其他任何事情之前,我建议检查 API 的文档中关于承诺的内容以及如何处理未处理的异常,因为这可能就是这里正在发生的事情。某些 promise 实现要求您在调用链的末尾调用 .done()
以确保处理未捕获的异常。有些要求正确配置某些全局承诺设置。 Bluebird 文档提供了很好的 discussion 问题。
Mocha 能够处理 运行 普通代码中未捕获的异常:
var chai = require("chai");
var assert = chai.assert;
chai.config.includeStack = true;
describe("foo", function() {
it("let the exception be caught by Mocha", function(done) {
setTimeout(function () {
assert.isFalse(true, "foo");
done();
}, 1000);
});
});
这将导致输出:
foo
1) let the exception be caught by Mocha
0 passing (1s)
1 failing
1) foo let the exception be caught by Mocha:
Uncaught AssertionError: foo: expected true to be false
at Assertion.<anonymous> (/tmp/t7/node_modules/chai/lib/chai/core/assertions.js:286:10)
at Assertion.Object.defineProperty.get (/tmp/t7/node_modules/chai/lib/chai/utils/addProperty.js:35:29)
at Function.assert.isFalse (/tmp/t7/node_modules/chai/lib/chai/interface/assert.js:297:31)
at null._onTimeout (/tmp/t7/test.js:8:20)
at Timer.listOnTimeout (timers.js:119:15)
我在我的代码中遇到过同样的情况,使用 Q
作为 promise。
发生的事情是:
then
块内的断言失败。
then
块的其余部分,包括 done()
语句,未执行。
- Q 去寻找一个
catch
块,但不在那里。
- 这导致 'hanging' 承诺,从而导致 Mocha 2000 毫秒超时。
我通过这样做解决了这个问题:
describe("myTest", function() {
it("should return valid JSON.", function(done) {
api.myCall("valid value").then(function(result) {
console.log(result);
var resultObj = JSON.parse(result);
assert.isFalse(resultObj.hasOwnProperty("error"), "result has an error");
done();
})
.catch(function(err) {
console.error(err);
done(err);
});
});
});
我觉得 Mocha 令人沮丧的一件事是,当测试失败时,它们不会给出失败行的实际错误消息,相反,它们只是以 Error: timeout of超过 2000 毫秒。确保在此测试中调用了 done() 回调。
以这个测试为例:
describe("myTest", function() {
it("should return valid JSON.", function(done) {
api.myCall("valid value").then(function(result) {
console.log(result);
var resultObj = JSON.parse(result);
assert.isFalse(resultObj.hasOwnProperty("error"), "result has an error");
done();
});
});
});
输出为:
myTest
{"error":null,"status":403}
1) should return valid JSON.
0 passing (2s)
1 failing
1) myTest should return valid JSON.:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
assert.isFalse 失败,但未显示应显示的消息 ("result has an error")。事实上,处理似乎就此停止,因为 done() 从未被调用过。去掉那一行,测试通过了,因为 done() 被调用了。
所以,我错过了什么?为什么摩卡测试会这样?我使用的实际测试库是:
var assert = require("chai").assert;
有谁知道我做错了什么或者为什么会这样?
您的 API 似乎在使用 promises。在尝试其他任何事情之前,我建议检查 API 的文档中关于承诺的内容以及如何处理未处理的异常,因为这可能就是这里正在发生的事情。某些 promise 实现要求您在调用链的末尾调用 .done()
以确保处理未捕获的异常。有些要求正确配置某些全局承诺设置。 Bluebird 文档提供了很好的 discussion 问题。
Mocha 能够处理 运行 普通代码中未捕获的异常:
var chai = require("chai");
var assert = chai.assert;
chai.config.includeStack = true;
describe("foo", function() {
it("let the exception be caught by Mocha", function(done) {
setTimeout(function () {
assert.isFalse(true, "foo");
done();
}, 1000);
});
});
这将导致输出:
foo
1) let the exception be caught by Mocha
0 passing (1s)
1 failing
1) foo let the exception be caught by Mocha:
Uncaught AssertionError: foo: expected true to be false
at Assertion.<anonymous> (/tmp/t7/node_modules/chai/lib/chai/core/assertions.js:286:10)
at Assertion.Object.defineProperty.get (/tmp/t7/node_modules/chai/lib/chai/utils/addProperty.js:35:29)
at Function.assert.isFalse (/tmp/t7/node_modules/chai/lib/chai/interface/assert.js:297:31)
at null._onTimeout (/tmp/t7/test.js:8:20)
at Timer.listOnTimeout (timers.js:119:15)
我在我的代码中遇到过同样的情况,使用 Q
作为 promise。
发生的事情是:
then
块内的断言失败。then
块的其余部分,包括done()
语句,未执行。- Q 去寻找一个
catch
块,但不在那里。 - 这导致 'hanging' 承诺,从而导致 Mocha 2000 毫秒超时。
我通过这样做解决了这个问题:
describe("myTest", function() {
it("should return valid JSON.", function(done) {
api.myCall("valid value").then(function(result) {
console.log(result);
var resultObj = JSON.parse(result);
assert.isFalse(resultObj.hasOwnProperty("error"), "result has an error");
done();
})
.catch(function(err) {
console.error(err);
done(err);
});
});
});