Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error stack should be displayed for uncaught exceptions in async tests #958

Closed
janotav opened this issue Aug 21, 2013 · 2 comments
Closed

Comments

@janotav
Copy link

janotav commented Aug 21, 2013

Currently exceptions in async tests lose their stack and display unfriendly message like:

Chrome 28.0.1500 (Linux) simple test FAILED
    Error: Uncaught AssertionError: expected 1 to equal 2 (/home/janotav/anywhere/web-ide/client/core/components/chai/chai.js:505)
        at global.onerror (/home/janotav/anywhere/web-ide/server/node_modules/mocha/mocha.js:5343:23)

This has been addressed recently in the HTML5 spec and can produce nice output like:

Chrome 31.0.1605 (Windows 7) simple test FAILED
    AssertionError: expected 1 to equal 2
        at Assertion.assertEqual (/home/janotav/anywhere/web-ide/client/core/components/chai/chai.js:907:12)
        at Assertion.ctx.(anonymous function) [as equal] (/home/janotav/anywhere/web-ide/client/core/components/chai/chai.js:3113:25)
        at /home/janotav/anywhere/web-ide/client/tests/spec/idb-filesystem-spec.js:43:34

If the global handler in mocha re-uses the existing Error object like this:

    global.onerror = function(err, url, line, colno, erroObj){
        fn(erroObj || new Error(err + ' (' + url + ':' + line + ')'));
    };

Currently this extra arguments work in Canary, other browsers are likely to follow. Related blog post:

https://mikewest.org/2013/08/debugging-runtime-errors-with-window-onerror

@boneskull
Copy link
Contributor

see #815

@mgiuffrida
Copy link

mgiuffrida commented Jul 30, 2016

This wasn't resolved with #815. We should still include the full stack trace of uncaught exceptions if provided, rather than throw out the error and create a new one with no context.

Repro: https://jsfiddle.net/mgiuffrida/koLzuy6s/

function doSomething() {
  chai.assert.equal('foo', 'bar');
}
describe('mocha', function() {
  it('throws synchronously', function() {
    doSomething();  // Stack trace OK
  });
  it('throws async', function(done) {
    setTimeout(function() {
      doSomething();  // Truncated stack trace
      done();
    });
  });
});

Unhelpful error (no function stack or real line/col numbers):

Error: Uncaught AssertionError: expected 'foo' to equal 'bar' (https://fiddle.jshell.net/mgiuffrida/koLzuy6s/show/:278)

Helpful error (with https://github.com/mgiuffrida/mocha/tree/patch-1):

AssertionError: expected 'foo' to equal 'bar'
    at doSomething (:52:17)
    at :61:9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants