-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
[Discussion] Expect only overrides error stack for built-in matchers #5162
Merged
cpojer
merged 23 commits into
jestjs:master
from
bvaughn:override-error-stack-only-for-internal-matchers
Jan 5, 2018
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f39d750
Expect only overrides error stack for built-in matchers
bvaughn e7d9bbd
Fixed naggy lint issue
bvaughn 9c10921
Replaced for...of with Object.keys().forEach()
bvaughn eb5d29c
Merged master
bvaughn ea088cf
Removed JestAssertionError export (as it is no longer needed)
bvaughn f2e7335
Added custom matcher test
bvaughn 3e00753
Adjusted custom_matcher test to use prettified, non-absolute stack
bvaughn 9e0cfae
test: refactor integration test
SimenB 31f8e58
update with correct snapshot
SimenB c388a49
test: skip on windows
SimenB 0c5c78c
Clarified comment
bvaughn 5843ab5
Further clarified comment
bvaughn f902b4b
test: fix test for node 6
SimenB bd21922
test: move custom matcher stack to separate file to be able to skip i…
SimenB 3ef09ea
Revert "test: fix test for node 6"
SimenB f88bf07
test: really fix node 6
SimenB ff50df6
Merge branch 'master' into override-error-stack-only-for-internal-mat…
SimenB 2da0eee
docs: add package prefix to changelog
SimenB 6f6dd91
Merge branch 'master' into override-error-stack-only-for-internal-mat…
bvaughn e8ab539
Reverted unrelated Markdown changes that had made their way into the …
bvaughn 7dad466
Replaced '__jestInternal' string attribute with a Symbol
bvaughn 179093c
Merge branch 'master' into override-error-stack-only-for-internal-mat…
bvaughn b8c6bfe
Updated snapshot
bvaughn 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
29 changes: 29 additions & 0 deletions
29
integration_tests/__tests__/__snapshots__/custom_matcher_stack_trace.test.js.snap
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,29 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`works with custom matchers 1`] = ` | ||
"FAIL __tests__/custom_matcher.test.js | ||
Custom matcher | ||
✓ passes | ||
✓ fails | ||
✕ preserves error stack | ||
|
||
● Custom matcher › preserves error stack | ||
|
||
qux | ||
|
||
43 | const bar = () => baz(); | ||
44 | const baz = () => { | ||
> 45 | throw Error('qux'); | ||
46 | }; | ||
47 | | ||
48 | // This expecation fails due to an error we throw (intentionally) | ||
|
||
at __tests__/custom_matcher.test.js:45:13 | ||
at __tests__/custom_matcher.test.js:43:23 | ||
at __tests__/custom_matcher.test.js:42:23 | ||
at __tests__/custom_matcher.test.js:52:7 | ||
at __tests__/custom_matcher.test.js:11:18 | ||
at __tests__/custom_matcher.test.js:53:8 | ||
|
||
" | ||
`; |
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
28 changes: 28 additions & 0 deletions
28
integration_tests/__tests__/custom_matcher_stack_trace.test.js
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,28 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
const runJest = require('../runJest'); | ||
const {extractSummary} = require('../utils'); | ||
const skipOnWindows = require('../../scripts/skip_on_windows'); | ||
|
||
skipOnWindows.suite(); | ||
|
||
test('works with custom matchers', () => { | ||
const {stderr} = runJest('custom_matcher_stack_trace'); | ||
|
||
let {rest} = extractSummary(stderr); | ||
|
||
rest = rest | ||
.split('\n') | ||
.filter(line => line.indexOf('at Error (native)') < 0) | ||
.join('\n'); | ||
|
||
expect(rest).toMatchSnapshot(); | ||
}); |
55 changes: 55 additions & 0 deletions
55
integration_tests/custom_matcher_stack_trace/__tests__/custom_matcher.test.js
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,55 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
'use strict'; | ||
|
||
function toCustomMatch(callback, expectation) { | ||
const actual = callback(); | ||
|
||
if (actual !== expectation) { | ||
return { | ||
message: () => `Expected "${expectation}" but got "${actual}"`, | ||
pass: false, | ||
}; | ||
} else { | ||
return {pass: true}; | ||
} | ||
} | ||
|
||
expect.extend({ | ||
toCustomMatch, | ||
}); | ||
|
||
describe('Custom matcher', () => { | ||
it('passes', () => { | ||
// This expectation should pass | ||
expect(() => 'foo').toCustomMatch('foo'); | ||
}); | ||
|
||
it('fails', () => { | ||
expect(() => { | ||
// This expectation should fail, | ||
// Which is why it's wrapped in a .toThrow() block. | ||
expect(() => 'foo').toCustomMatch('bar'); | ||
}).toThrow(); | ||
}); | ||
|
||
it('preserves error stack', () => { | ||
const foo = () => bar(); | ||
const bar = () => baz(); | ||
const baz = () => { | ||
throw Error('qux'); | ||
}; | ||
|
||
// This expecation fails due to an error we throw (intentionally) | ||
// The stack trace should point to the line that throws the error though, | ||
// Not to the line that calls the matcher. | ||
expect(() => { | ||
foo(); | ||
}).toCustomMatch('test'); | ||
}); | ||
}); |
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,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
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
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.
Should this be
throw new Error
? I'm not sure if there's a semantical difference...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.
No difference that I'm aware of.
source: http://ecma-international.org/ecma-262/5.1/#sec-15.11.1