-
-
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
Changes from 9 commits
f39d750
e7d9bbd
9c10921
eb5d29c
ea088cf
f2e7335
3e00753
9e0cfae
31f8e58
c388a49
0c5c78c
5843ab5
f902b4b
bd21922
3ef09ea
f88bf07
ff50df6
2da0eee
6f6dd91
e8ab539
7dad466
179093c
b8c6bfe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* 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', () => { | ||
// This test is expected to pass | ||
it('passes', () => { | ||
expect(() => 'foo').toCustomMatch('foo'); | ||
}); | ||
|
||
// This test is expected to fail | ||
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. it passes, is that an issue? 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. Ah, my comment is perhaps confusing. The "test" isn't expected to fail. The custom matcher assertion is expected to fail, which is why it's wrapped in an expect-to-throw check. I'll update the comment. 😄 |
||
it('fails', () => { | ||
expect(() => { | ||
expect(() => 'foo').toCustomMatch('bar'); | ||
}).toThrow(); | ||
}); | ||
|
||
// This test fails due to an unrelated/unexpected error | ||
// It will show a helpful stack trace though | ||
it('preserves error stack', () => { | ||
const foo = () => bar(); | ||
const bar = () => baz(); | ||
const baz = () => { | ||
throw Error('qux'); | ||
}; | ||
|
||
expect(() => { | ||
foo(); | ||
}).toCustomMatch('test'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,13 @@ export const setState = (state: Object) => { | |
|
||
export const getMatchers = () => global[JEST_MATCHERS_OBJECT].matchers; | ||
|
||
export const setMatchers = (matchers: MatchersObject) => { | ||
export const setMatchers = (matchers: MatchersObject, isInternal: boolean) => { | ||
Object.keys(matchers).forEach(key => { | ||
const matcher = matchers[key]; | ||
Object.defineProperty(matcher, '__jestInternal', { | ||
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. Can we not use a Symbol instead to make this actually secret? 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. Done! |
||
value: isInternal, | ||
}); | ||
}); | ||
|
||
Object.assign(global[JEST_MATCHERS_OBJECT].matchers, matchers); | ||
}; |
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.
this is wrong, we have lost the trace
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.
Yes, as of your commit.
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.
Indeed. should be fixed now