Skip to content

Commit

Permalink
Implement toThrow(message) with a specific error message string (face…
Browse files Browse the repository at this point in the history
…book#47700)

Summary:

Changelog: [internal]

Add support for the string parameter for `toThrow` to assert for specific error messages.

Differential Revision: D66118001
  • Loading branch information
rubennorte authored and facebook-github-bot committed Nov 19, 2024
1 parent 6f659e9 commit 15d8c0b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions jest/integration/runtime/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,19 @@ class Expect {
}
}
toThrow(error: mixed): void {
if (error != null) {
throw new Error('toThrow() implementation does not accept arguments.');
toThrow(expected?: string): void {
if (expected != null && typeof expected !== 'string') {
throw new Error(
'toThrow() implementation only accepts strings as arguments.',
);
}

let pass = false;
try {
// $FlowExpectedError[not-a-function]
this.#received();
} catch {
pass = true;
} catch (error) {
pass = expected != null ? error.message === expected : true;
}
if (!this.#isExpectedResult(pass)) {
throw new Error(
Expand Down

0 comments on commit 15d8c0b

Please sign in to comment.