Implement toThrow(message) with a specific error message string (#47700)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Rubén Norte 2024-11-19 07:56:49 -08:00 committed by Facebook GitHub Bot
parent 47882f0000
commit 05874c831b

View File

@ -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(