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?
Chai supports 3 different assertion styles: expect , should , and assert . expect is most common, because should modifies Object.
Mocha is a JavaScript test framework running on Node. js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.
Note expect and should uses chainable language to construct assertions, but they differ in the way an assertion is initially constructed. In the case of should , there are also some caveats and additional tools to overcome the caveats. var expect = require('chai').
From version 1.9.1 onwards, if you set the includeStack
flag to true
, you'll get a stack trace on assertion failures:
var chai = require("chai");
chai.config.includeStack = true;
var assert = chai.assert;
describe("test", function () {
it("blah", function () {
assert.isTrue(false);
});
});
In versions prior to 1.9.1 you had to set chai.Assertion.includeStack = true
. From 1.9.1 onwards this method of getting stack traces is deprecated. It is still available in 1.10.0 but may be removed in 1.11.0 or 2.0.0. (See here for details.)
The example above will show a stack trace where assert.isTrue
fails. Like this:
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 ...]
(I've truncated the trace to what is only relevant and truncated the paths.) The last frame shown in what I've included above is the one where the error happened (.../test.js:7:16
). I do not think that chai allows having only the file name and line number of the assertion call.
chai.Assertion.includeStack
is now deprecated. Use chai.config
instead
var chai = require("chai");
chai.config.includeStack = true;
var assert = chai.assert;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With