-
-
Notifications
You must be signed in to change notification settings - Fork 770
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
Stack trace for throws(new Error(...)) starts at constructor invocation #1507
Comments
Stack traces look very different depending on the environment. The way I did it for Mochify was to remove lines with paths containing specific module names (see here: https://github.com/mantoni/mochify.js/blob/master/lib/trace.js). However, this is only possible on node or if something like Browserify is used with sourcemaps. So this solution doesn't work if scripts are pulled into a browser directly. TBH I think this is out of scope for Sinon itself. Instead, you could try to write a test report processor that filters the output of your test runner to remove Sinon internal trace lines. |
Sorry if I was confusing. The issue isn't that I want to filter out pieces of the stack trace, it's that I want the stack trace to start from the point where the exception is thrown, rather than the point the exception object was created. I wrote this test which I put in
The output is:
The top line is the point where the
And rerun the unit test, the output becomes:
The |
Aaah, ok. Now I get it. There is a way to create the error using the type of the error as a string, like this: var stub = sinon.stub().throws('Error') Unfortunately this has the same behavior because the error object is constructed immediately. I was thinking it should be possible to construct the error object lazily when the stub is invoked. This way the error object has a stack showing the actual call path. If you'd like to look into this, have a look at the default behaviors. |
Yes that seems like a good solution to me. I'll work on a PR for |
When I pass an error object to
stub.throws(...)
I noticed that it shows the stack trace of the constructor in mocha and Chrome, not sure yet about Firefox. It would be nice to have the stack trace start from the point that the exception is thrown so I could identify what pieces of code interacted with the stubbed function. I was going to file a PR to attach a new stack trace to the exception but I noticed that there's a comment warning against interacting with the stack trace here. Can someone give me guidance on whether replacing the stack trace is desirable, or if there's a workaround from the end user's perspective I should use instead? ThanksThe text was updated successfully, but these errors were encountered: