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

chai, mocha.js

Solution

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.

Problem

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?

Original source