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

Jasmine Swallows Error Message when throwing own Exception #638

Closed
despairblue opened this issue Jul 27, 2014 · 3 comments
Closed

Jasmine Swallows Error Message when throwing own Exception #638

despairblue opened this issue Jul 27, 2014 · 3 comments

Comments

@despairblue
Copy link

Doing something like this in the code that's going to be tested (or in an it):

var MyError = function (message) {
  this.message = message
}
MyError.prototype = new Error('foo')
var myerror = new MyError('bar')
console.log(myerror instanceof Error)
console.log(myerror.message)
throw myerror

Jasmine will report this:
screenshot 2014-07-27 23 14 07

You see that jasmine seems to report the prototypes message. Not the one from myerror. Is this me misunderstanding prototypes or is this actually some kind of error in jasmine?

@despairblue
Copy link
Author

By the way, my actual problem is that peg.js constructs it's own SyntaxErrors like this:

function peg$subclass(child, parent) {
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor();
  }

  function SyntaxError(message, expected, found, offset, line, column) {
    this.message  = message;
    this.expected = expected;
    this.found    = found;
    this.offset   = offset;
    this.line     = line;
    this.column   = column;

    this.name     = "SyntaxError";
  }

  peg$subclass(SyntaxError, Error);

And jasmine won't report these errors.
screenshot 2014-07-27 23 53 14

The actual error caught with try catch:
screenshot 2014-07-27 23 54 46

sheelc added a commit that referenced this issue Jul 28, 2014
Previously, was only printing out the stack while the html reporter
would print out the message as well as the stack. Now they should be
more consistent.

As noticed by @despairblue in #638
@sheelc
Copy link
Contributor

sheelc commented Jul 28, 2014

It looks like theres a bit of an inconsistency between the browser reporter and the console reporter. The browser reporter prints out both the message and stack, while the console reporter only prints the stack. I just pushed a commit to fix that.

The message shown in the browser is the message you have overridden ('bar') while the stack contains the offending 'foo', since the stack property hasn't been overridden. As this stack overflow post suggests, you could override the stack property as well to be something like (new Error()).stack to remove the misleading 'Error: foo' and only see 'Error' instead.

Does this help resolve the issue?

@despairblue
Copy link
Author

Yep, that helps. Now I'll can see why the spec failed. Thanks 👍
And overriding the stack property (or actually having one one in the first) place is something I'll suggest to the peg.js maintainer
screenshot from 2014-07-28 12 34 45

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

2 participants