-
Notifications
You must be signed in to change notification settings - Fork 54
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
feat: simplify mocha error stack #59
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const mocha = require('mocha'); | ||
const internal = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的规则要仔细看看。 |
||
'(timers.js:', | ||
'(node.js:', | ||
'(module.js:', | ||
'(domain.js:', | ||
'GeneratorFunctionPrototype.next (native)', | ||
'at Generator.next', | ||
// node 8.x | ||
'at Promise (<anonymous>)', | ||
'at next (native)', | ||
'__mocha_internal__', | ||
/node_modules\/.*empower-core\//, | ||
/node_modules\/.*mocha\//, | ||
/node_modules\/.*co\//, | ||
/node_modules\/.*co-mocha\//, | ||
]; | ||
|
||
// monkey-patch `Runner#fail` to modify stack | ||
const originFn = mocha.Runner.prototype.fail; | ||
mocha.Runner.prototype.fail = function(test, err) { | ||
/* istanbul ignore else */ | ||
if (err.stack) { | ||
const stack = err.stack.split('\n').filter(line => { | ||
line = line.replace(/\\\\?/g, '/'); | ||
return !internal.some(rule => match(line, rule)); | ||
}); | ||
stack.push(' [use `--full-trace` to display the full stack trace]'); | ||
err.stack = stack.join('\n'); | ||
} | ||
return originFn.call(this, test, err); | ||
}; | ||
|
||
function match(line, rule) { | ||
if (rule instanceof RegExp) return rule.test(line); | ||
return line.includes(rule); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test/fixtures/test-files-stack/node_modules/my-sleep/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "test-files-stack" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
describe('assert.test.js', () => { | ||
it('should fail with simplify stack', () => { | ||
[ 1 ].forEach(() => { | ||
assert(1 === 0); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const co = require('co'); | ||
|
||
describe('promise.test.js', () => { | ||
it('should fail with simplify stack', function* () { | ||
yield co(function* () { | ||
return yield new Promise((resolve, reject) => { | ||
reject(new Error('this is an error')); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const sleep = require('my-sleep'); | ||
|
||
describe('sleep.test.js', () => { | ||
it('should fail with simplify stack', done => { | ||
sleep(() => { | ||
done(new Error('this is an error')); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加个一个参数来显示全部,这个参数也是 mocha 自己的参数,会透传过去。(不过传递给 mocha 好像没啥区别)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mochajs/mocha#545 感觉是在做同一件事情
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那个没动静了似乎
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/mochajs/mocha/blob/master/lib/utils.js#L738
发现 mocha 里面实际已经有了 mocha-clean 的代码的,但它的匹配方式不兼容我们的 npminstall 格式。
我们这边先自己实现吧,
回头我去提个 PR 到 mocha。(我们的一些规则没办法提到 PR 那边的,如 co-mocha, power-assert,还是直接在这边实现好了。)cc @fengmk2