-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Update toThrow() to be able to use Error.cause #13606
Changes from 9 commits
89d160d
b84793c
f696943
8e34799
1d84bd4
95e9c6b
def8b86
a522080
613425b
1e65096
9d57a89
d359f80
0f3282c
4798793
9c1a791
db115e8
de64948
c0328f9
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"extends": "../../../../tsconfig.test.json", | ||
"compilerOptions": { | ||
"lib": ["es2022.error"] | ||
}, | ||
"include": ["./**/*"], | ||
"references": [{"path": "../../"}] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,14 +225,20 @@ const toThrowExpectedObject = ( | |
thrown: Thrown | null, | ||
expected: Error, | ||
): SyncExpectationResult => { | ||
const pass = thrown !== null && thrown.message === expected.message; | ||
const pass = | ||
thrown !== null && | ||
thrown.message === expected.message && | ||
createMessageAndCause(thrown.value) === createMessageAndCause(expected); | ||
|
||
const message = pass | ||
? () => | ||
// eslint-disable-next-line prefer-template | ||
matcherHint(matcherName, undefined, undefined, options) + | ||
'\n\n' + | ||
formatExpected('Expected message: not ', expected.message) + | ||
formatExpected( | ||
`Expected ${messageAndCause(expected)}: not `, | ||
createMessageAndCause(expected), | ||
) + | ||
(thrown !== null && thrown.hasMessage | ||
? formatStack(thrown) | ||
: formatReceived('Received value: ', thrown, 'value')) | ||
|
@@ -242,22 +248,27 @@ const toThrowExpectedObject = ( | |
'\n\n' + | ||
(thrown === null | ||
? // eslint-disable-next-line prefer-template | ||
formatExpected('Expected message: ', expected.message) + | ||
formatExpected( | ||
`Expected ${messageAndCause(expected)}: `, | ||
createMessageAndCause(expected), | ||
) + | ||
'\n' + | ||
DID_NOT_THROW | ||
: thrown.hasMessage | ||
? // eslint-disable-next-line prefer-template | ||
printDiffOrStringify( | ||
expected.message, | ||
thrown.message, | ||
'Expected message', | ||
'Received message', | ||
createMessageAndCause(expected), | ||
createMessageAndCause(thrown.value), | ||
`Expected ${messageAndCause(expected)}`, | ||
`Received ${messageAndCause(thrown.value)}`, | ||
true, | ||
) + | ||
'\n' + | ||
formatStack(thrown) | ||
: formatExpected('Expected message: ', expected.message) + | ||
formatReceived('Received value: ', thrown, 'value')); | ||
: formatExpected( | ||
`Expected ${messageAndCause(expected)}: `, | ||
createMessageAndCause(expected), | ||
) + formatReceived('Received value: ', thrown, 'value')); | ||
|
||
return {message, pass}; | ||
}; | ||
|
@@ -447,4 +458,26 @@ const formatStack = (thrown: Thrown | null) => | |
}, | ||
); | ||
|
||
const _createMessageAndCause = (error: Error): string => { | ||
if (error.cause instanceof Error) { | ||
return `{ message: ${error.message}, cause: ${_createMessageAndCause( | ||
error.cause, | ||
)}}`; | ||
} else { | ||
return `{ message: ${error.message} }`; | ||
} | ||
}; | ||
|
||
const createMessageAndCause = (error: Error) => { | ||
if (error.cause instanceof Error) { | ||
return _createMessageAndCause(error); | ||
} else { | ||
return error.message; | ||
} | ||
}; | ||
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. why are these 2 functions? 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. Currently, if To maintain this current behavior, I separated the functions so that only 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.
|
||
|
||
const messageAndCause = (error: Error) => { | ||
return error.cause === undefined ? 'message' : 'message and cause'; | ||
}; | ||
|
||
export default 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.
can you use the helper? https://github.com/facebook/jest/blob/74776d8b581fabeb5c3993b6499643b8c6ffedf6/packages/test-utils/src/ConditionalTest.ts#L34