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

Truncated Stack Trace #815

Closed
machineghost opened this issue Apr 15, 2013 · 15 comments
Closed

Truncated Stack Trace #815

machineghost opened this issue Apr 15, 2013 · 15 comments

Comments

@machineghost
Copy link

When I run tests through the web-based runner, Mocha will take a stack trace like this:

Uncaught Error: expected undefined to equal 'base:Sample' expect.js:99
Assertion.assert expect.js:99
Assertion.be.Assertion.equal expect.js:200
Assertion.(anonymous function) expect.js:73
(anonymous function) ConstraintSelectorTests.js:88
jQuery.event.dispatch jquery.js:3332
elemData.handle.eventHandle jquery.js:2941
jQuery.event.trigger jquery.js:3210
jQuery.fn.extend.triggerHandler jquery.js:3874
func.apply.$.extend.__callback jquery.jstree.js:237
$.jstree.plugin._fn.loaded jquery.jstree.js:539
$.jstree._fn.(anonymous function) jquery.jstree.js:234
(anonymous function) jquery.jstree.js:410
(anonymous function) jquery.jstree.js:1653
$.jstree.plugin._fn.load_node_json jquery.jstree.js:1724
$.jstree._fn.(anonymous function) jquery.jstree.js:234
$.jstree.plugin._fn.load_node jquery.jstree.js:1653
$.jstree._fn.(anonymous function) jquery.jstree.js:234
$.jstree._fn.(anonymous function) jquery.jstree.js:234
$.jstree.plugin._fn.init jquery.jstree.js:410
$.jstree._fn.(anonymous function) jquery.jstree.js:234
(anonymous function) jquery.jstree.js:162

and reduce it to:

Error: Uncaught Error: expected undefined to equal 'base:Sample' (file:///home/jeremy/workspace/syapse/src/syapse/static/js/ext/expect.js:99)
at window.onerror (file:///home/jeremy/workspace/syapse/src/syapse/static/js/ext/mocha/mocha.js:5286:10)

Is this just an inherent limit of the fact that Mocha uses onerror to catch exceptions, or is there any way that Mocha could display the real stack trace instead? Because while I can get the real trace from the console, having it in the browser window would be awfully convenient.

@tj
Copy link
Contributor

tj commented Apr 22, 2013

do you get a better .stack if you dont run with file:// ? try npm install -g serve and serve . &

@machineghost
Copy link
Author

Thanks, I didn't know node had a built-in webserver, so I just learned a neat trick. However, it didn't help the stacktrace; I still get stacktraces like:

Error: Uncaught Error: expected '' to contain 'End' (http://localhost:3000/js/ext/expect.js:99)
    at process.on.window.onerror (http://localhost:3000/js/ext/mocha/mocha.js:5286:10)

(instead of the full/real stacktrace).

@Calamari
Copy link

It's an old issue, but I just seen it and will answer non the less.

The reason for this shortened stack is, that window.onerror only gets the message, fileUrl and lineNumber and no Error object, when catching uncaughtExceptions.

Unless you use the current version of Chrome – where also column of the error and an Error object will be passed in as well – and you write something like mentioned in #958.

If this still is a problem. If not, please say and tj could close this issue. :)

@machineghost
Copy link
Author

Yeah, I suspected it was just an onError limitation, but I thought maybe Mocha might be able to get around that by using a (very high-level) try/catch instead of relying on onError. But if not, it can't be helped :-)

@andrew-luhring
Copy link

I desperately want the inverse of this-- I ONLY want the single line that applies to the code that I've written to be returned... currently it's:

Running "Mocha" task
 1   -_-_,------,
 1   -_-_|   /\_/\ 
 0   -_-^|__( x .x) 
     -_-  ""  "" 

  1 passing (15ms)
  1 failing

  1) mocha will fail:
     Error: expected 1 to equal 0
      at Assertion.assert (/Users/worker_bee/Sites/TextMateArduino/node_modules/expect.js/expect.js:99:13)
      at Assertion.be.Assertion.equal (/Users/worker_bee/Sites/TextMateArduino/node_modules/expect.js/expect.js:200:10)
      at Context.<anonymous> (/Users/worker_bee/Sites/TextMateArduino/public/tests/_prettyfy.js:10:25)
      at Test.Runnable.run (/Users/worker_bee/Sites/TextMateArduino/node_modules/mocha/lib/runnable.js:221:32)
      at Runner.runTest (/Users/worker_bee/Sites/TextMateArduino/node_modules/mocha/lib/runner.js:374:10)
      at /Users/worker_bee/Sites/TextMateArduino/node_modules/mocha/lib/runner.js:452:12
      at next (/Users/worker_bee/Sites/TextMateArduino/node_modules/mocha/lib/runner.js:299:14)
      at /Users/worker_bee/Sites/TextMateArduino/node_modules/mocha/lib/runner.js:309:7
      at next (/Users/worker_bee/Sites/TextMateArduino/node_modules/mocha/lib/runner.js:247:23)
      at Object._onImmediate (/Users/worker_bee/Sites/TextMateArduino/node_modules/mocha/lib/runner.js:276:5)
      at processImmediate [as _immediateCallback] (timers.js:330:15)



Warning: Tests failed Use --force to continue.

and what I desperately want for it to say is only:

Running "Mocha" task
 1   -_-_,------,
 1   -_-_|   /\_/\ 
 0   -_-^|__( x .x) 
     -_-  ""  "" 

  1 passing (15ms)
  1 failing

  1) mocha will fail:
     Error: expected 1 to equal 0
      at Context.<anonymous> (./TextMateArduino/public/tests/_prettyfy.js:10:25)


Warning: Tests failed Use --force to continue.

All I want in this cold cruel world is a cute cat to tell me the status of my tests and- if they fail- to give me a line number and a reason/error message. There are 10 lines that I totally don't need to know about here.

@rlidwka
Copy link

rlidwka commented Feb 2, 2014

There are 10 lines that I totally don't need to know about here.

I usually get my error line on a 3rd or 4th line of the stack trace due to assertion libraries. 10 lines is a good enough choice.

@andrew-luhring
Copy link

Are you @rlidwka saying that I shouldn't ask for a concise stack error option or are you agreeing with me that anything above a few lines of stack trace most of the time isn't necessary?

@kfatehi
Copy link

kfatehi commented Jul 28, 2014

@andrew-luhring look at my comments on #545 and contribute there if you think you can improve it. i think this is the job of a module, and mocha should give me everything it knows.

@boneskull
Copy link
Member

@andrew-luhring I don't think filtering the stack trace is Mocha's business. However, JetBrains IDE's (and evidently Eclipse as well) have a Grep Console plugin that I use for this. I give it a regex to find .spec.js in the trace, and it will highlight the line I care about.

@danielstjules
Copy link
Contributor

@andrew-luhring @keyvanfatehi Shorter stack traces were added in #1564 :)

@dasilvacontin
Copy link
Contributor

So the only problem left here is being able to log a stack in browser that don't have a stack on their errors. (?)

@jbnicolai
Copy link

I think this is no longer an issue, @dasilvacontin. Please correct me if I'm wrong.

@millerdev
Copy link

I still have this problem. If I'm understanding the article linked in #958 correctly, it looks like the latest version of mocha (2.3.3) does not use the fifth argument of onerror. Incidentally, if I change it like so:

    global.onerror = function(err, url, line, col, errobj){
      fn(errobj || new Error(err + ' (' + url + ':' + line + ')'));
      return !mocha.allowUncaught;
    };

I get a full stack trace from Chrome, but not phantomjs. I'm not sure if it has something to do with the version of phantomjs or node I'm using? Or maybe I have something misconfigured? At any rate, here's a slightly less elegant solution that works:

/**
 * Report full stack trace on async callback error
 *
 * Wrap async callback functions with this function
 * to report the full stack trace if they fail.
 *
 * Usage:
 *
 *  it("will always fail", function (done) {
 *      someAsyncFunction(asyncatch(function (arg) {
 *          doSomethingThatThrowsAnError();
 *          done();
 *      }));
 *  });
 */
function asyncatch(fn) {
    return function () {
        try {
            var args = Array.prototype.slice.call(arguments);
            return fn.apply(this, args);
        } catch (err) {
            throw err && err.stack ? new Error(err.stack) : err;
        }
    };
}

@mgiuffrida
Copy link

I've opened #2403 to address the previous comment. Maybe discussion on this should continue in issue #958 as that one is specific to async errors.

@JoshuaKGoldberg
Copy link
Member

Ah, this is #5106.

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