When testing Promises with Mocha, how can I print the full stack trace when an error occurs

Viewed 766

Suppose I have a small spec like:

describe("feature", () => {
  it("does stuff", () => {
    return myPromiseBasedFn().then(result => {
      expect(result).to.eql(1);
    });
  });
});

Currently, when the promise is rejected, I just see the error message. For example:

12 passing (88ms)
1 failing

1) feature does stuff:
   TypeError: Cannot read property 'method' of undefined

How can I make mocha print the full stack trace of this error? For example, I'd like to see

TypeError: Cannot read property 'method' of undefined
    at SomeFunc (code/file.js:12:32)
    at code/base.js:49:24
1 Answers

From this blog post: https://medium.com/front-end-hacking/stack-traces-for-promises-in-node-js-46bf5f490fe4

You can use:

global.Promise = require("bluebird");

And then set:

BLUEBIRD_LONG_STACK_TRACES=1

When running tests to get complete stack traces:

BLUEBIRD_LONG_STACK_TRACES=1 mocha …

This works with Typescript, and if you're using cls-hooked, you also need to include:

let cls = require('cls-hooked');
let clsNamespace = cls.createNamespace('some-namespace')
let clsBluebird = require('cls-bluebird')
let Promise = require('bluebird')
clsBluebird(clsNamespace, Promise)
Related