Skip to content

Commit

Permalink
Implement toThrow(message) with a specific error message string (#47700)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47700

Changelog: [internal]

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

Reviewed By: sammy-SC

Differential Revision: D66118001

fbshipit-source-id: 8c04cd20d4ad17163ec0c7bf943c429507a97985
  • Loading branch information
rubennorte authored and facebook-github-bot committed Nov 19, 2024
1 parent 47882f0 commit 05874c8
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 @@ -215,17 +215,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 05874c8

Please sign in to comment.