10

I'm using mocha with chai.assert for my tests. Errors are caught and reported, but they don't show a file/line number where they happen. I'm used to having location information with tests in other languages, it's otherwise hard to figure out which assert failed.

Is there some way to get location information with mocha/chai/assert?

4

2 回答 2

17

从版本1.9.1 开始,如果您将includeStack标志设置为true,您将获得断言失败的堆栈跟踪:

var chai = require("chai");
chai.config.includeStack = true;
var assert = chai.assert;

describe("test", function () {
    it("blah", function () {
        assert.isTrue(false);
    });
});

在 1.9.1 之前的版本中,您必须设置chai.Assertion.includeStack = true. 从 1.9.1 开始,这种获取堆栈跟踪的方法已被弃用。它在 1.10.0 中仍然可用,但可能在 1.11.0 或 2.0.0 中被删除。(有关详细信息,请参见此处。)

上面的示例将显示assert.isTrue失败的堆栈跟踪。像这样:

AssertionError: expected false to be true
      at Assertion.<anonymous> (.../node_modules/chai/lib/chai/core/assertions.js:193:10)
      at Assertion.Object.defineProperty.get (.../node_modules/chai/lib/chai/utils/addProperty.js:35:29)
      at Function.assert.isTrue (.../node_modules/chai/lib/chai/interface/assert.js:242:31)
      at Context.<anonymous> (.../test.js:7:16)
      [... etc ...]

(我已将跟踪截断为仅相关的内容并截断路径。)我上面包含的最后一帧是发生错误的那一帧(.../test.js:7:16)。我不认为 chai 允许只有断言调用的文件名和行号。

于 2013-12-21T11:23:46.033 回答
7

chai.Assertion.includeStack现在已弃用。chai.config改为使用

var chai = require("chai");
chai.config.includeStack = true;
var assert = chai.assert;
于 2014-06-01T19:44:14.620 回答