-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Better StackTraces with filtering and mapping lines #2119
Comments
I need these exact features. I don't see where the APIs would allow it, short of writing a custom reporter. I think a simple solution would be to expose a -var createErrorFormatter = function (basePath, emitter, SourceMapConsumer) {
+var createErrorFormatter = function (config, emitter, SourceMapConsumer) {
+ var basePath = config.basePath
...
// indent every line
if (indentation) {
msg = indentation + msg.replace(/\n/g, '\n' + indentation)
}
+ if (config.formatError) {
+ msg = config.formatError(msg)
+ }
return msg + '\n'
}
}
var createReporters = function (names, config, emitter, injector) {
- var errorFormatter = createErrorFormatter(config.basePath, emitter, SourceMapConsumer)
+ var errorFormatter = createErrorFormatter(config, emitter, SourceMapConsumer)
var reporters = [] Then in config.set({
formatError(msg) {
return msg
.split('\n')
.reduce((list, line) => {
// filter out node_modules
if (new RegExp('/~/').test(line)) return list
// remove left hand side of "compiled <- source" string
const indent = line.slice(0, line.indexOf('/'))
const sliceEnd = ' <- webpack:///'
const original = line.slice(line.indexOf(sliceEnd) + sliceEnd.length)
return list.concat(indent + original)
}, [])
.join('\n')
},
}) Turning this stack: AssertionError@/Users/levithomason/src/stardust/test/tests.bundle.js:296
:25 <- webpack:///~/assertion-error/index.js:74:0
assert@/Users/levithomason/src/stardust/test/tests.bundle.js:4397:32 <-
webpack:///~/chai/lib/chai/assertion.js:107:0
/Users/levithomason/src/stardust/test/tests.bundle.js:8978:20 <- webpack
:///~/chai-enzyme/build/assertions/descendants.js:22:0
descendants@/Users/levithomason/src/stardust/test/tests.bundle.js:8983:8
<- webpack:///~/chai-enzyme/build/assertions/descendants.js:27:0
/Users/levithomason/src/stardust/test/tests.bundle.js:9525:24 <- webpack
:///~/chai-enzyme/build/ChaiWrapper.js:193:0
/Users/levithomason/src/stardust/test/tests.bundle.js:3984:31 <- webpack
:///~/chai/lib/chai/utils/addMethod.js:41:0
/Users/levithomason/src/stardust/test/tests.bundle.js:72950:31 <- webpac
k:///~/dirty-chai/lib/dirty-chai.js:114:0
/Users/levithomason/src/stardust/test/tests.bundle.js:4103:39 <- webpack
:///~/chai/lib/chai/utils/overwriteMethod.js:49:0
/Users/levithomason/src/stardust/test/tests.bundle.js:184887:116 <- webp
ack:///test/specs/views/Feed/Feed-test.js:20:0 Into this: test/specs/views/Feed/Feed-test.js:20:0 It seems like a simple enough solution to allow users to parse the output themselves as a workaround. @vojtajina @dignifiedquire any input on whether or not a PR would be accepted for this? It would be a huge productivity boost to be able to see concise output and not have to scroll to read the errors. |
I like this idea. I can see that this can be very useful to customise these easily. |
Great, I'll put up a PR in a bit. Can you clarify what you mean when you say:
? |
Allows a `formatError` config function. The function receives the entire error message as its only argument. It returns the new error message. Fixes #2119.
Thank you all, this is great. |
It looks like |
Hm, not my experience. You can see our implementation with before/after results here: https://github.com/TechnologyAdvice/stardust/pull/490 |
I should clarify, we do only get the error after the failure message and PhantomJS/OS version line. But it is a single string including the stack and line breaks. |
Very strange. I wonder why my setup behaves differently. Because I get every single line for I use mocha and except. Maybe someone else has the same problem... My output:
The two empty lines shouldn't be there... Also very strange: my pattern is |
Perhaps a difference in versions? We're on the karma, webpack, mocha stack. Regarding the source / compiled, I do know the choice of Good luck! |
We also ran into the issue with only receiving each line to the The big problem is, you then can't filter out individual lines as the formatter always returns at the minimum a line break: If we simply return the |
Note, fix posted here: #2627 |
Thank you! |
Can we get some example configs? I copied your code and it did nothing to the stack trace
User should be given simple options for this, so we can get back to writing tests |
See my response to your question here, rstacruz/mocha-clean#5 (comment). The usage changed slightly due to how the stack is passed. Each line is passed one at a time, rather than the whole stack at once. |
Thanks! So I used your example: formatError(msg) {
// Uncommment this to get rid of the entire stack trace, but then you don't know what happened
// return ''
// filter out empty lines and node_modules
if (!msg.trim() || /~/.test(msg)) return ''
// indent the error beneath the it() message
let newLine = ` ${msg}`
if (newLine.includes('webpack:///')) {
// remove webpack:///
newLine = newLine.replace('webpack:///', '')
// remove bundle location, showing only the source location
newLine = newLine.slice(0, newLine.indexOf(' <- '))
}
return `${newLine}\n`
}, It did clean it up quite a bit, But I'm still getting some weird mode_modules stuff.
So I modified it a little and it works! formatError(msg) {
// Uncommment this to get rid of the entire stack trace, but then you don't know what happened
// return ''
// filter out empty lines and node_modules
if (!msg.trim() || /~/.test(msg) || /node_modules/.test(msg)) return '' // < --- modified this line
// indent the error beneath the it() message
let newLine = ` ${msg}`
if (newLine.includes('webpack:///')) {
// remove webpack:///
newLine = newLine.replace('webpack:///', '')
// remove bundle location, showing only the source location
newLine = newLine.slice(0, newLine.indexOf(' <- '))
}
return `${newLine}\n`
},
Thanks a ton @levithomason! |
Let us say we have the following stack trace:
First I'd like to filter stack traces from
~/except
. Second I want to only see the original source (the part after<-
).I could actually hack the
lib/reporter.js
at line 92 like this to achieve this result:The result will be:
Can I achieve the same behaviour with existing APIs? Or can I add this behaviour with a pull request? I could imagine to add two new fields to the karma config (e.g.
mapStackTraceLine
/filterStackTraceLine
) so everyone can set custom filter/map rules if needed.The text was updated successfully, but these errors were encountered: