Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get mocha with chai assert to report file/line number?

Tags:

mocha.js

chai

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?

like image 939
edA-qa mort-ora-y Avatar asked Dec 21 '13 08:12

edA-qa mort-ora-y


People also ask

What are the 3 interfaces of the Chai assertion library?

Chai supports 3 different assertion styles: expect , should , and assert . expect is most common, because should modifies Object.

What is difference between Mocha and chai?

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.

Should I assert vs vs expect?

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').


2 Answers

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.

like image 142
Louis Avatar answered Oct 17 '22 00:10

Louis


chai.Assertion.includeStack is now deprecated. Use chai.config instead

var chai = require("chai");
chai.config.includeStack = true;
var assert = chai.assert;
like image 42
Brandon Horst Avatar answered Oct 16 '22 23:10

Brandon Horst