Skip to content

Commit

Permalink
Improve error messages in expect().toThrow(message) (#48430)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #48430

Changelog: [internal]

This expectation currently prints a very generic message that is hard to parse, so this change improves it a bit to account for more cases. E.g.:
* Before: "Expected <function> to throw"
* After: "Expected <function> to throw with message 'foo', but threw with message 'bar'"

Reviewed By: rshest

Differential Revision: D67738146

fbshipit-source-id: 690f15971cec0e8a7b038eeacc9302c9f3edc323
  • Loading branch information
rubennorte authored and facebook-github-bot committed Jan 3, 2025
1 parent 7e029b0 commit f20486c
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions packages/react-native-fantom/runtime/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,36 @@ class Expect {
).blameToPreviousFrame();
}

let pass = false;
let thrownError;
try {
// $FlowExpectedError[not-a-function]
this.#received();
} catch (error) {
pass = expected != null ? error.message === expected : true;
thrownError = error;
}
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to throw`,
).blameToPreviousFrame();

if (this.#isNot) {
if (expected != null) {
if (thrownError != null && thrownError.message === expected) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)} not to throw with message ${expected}"`,
).blameToPreviousFrame();
}
} else if (thrownError != null) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)} not to throw, but threw ${String(thrownError)}`,
).blameToPreviousFrame();
}
} else {
if (thrownError == null) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)} to throw`,
).blameToPreviousFrame();
} else if (expected != null && thrownError.message !== expected) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)} to throw with message "${expected}", but threw with message "${thrownError.message}"`,
).blameToPreviousFrame();
}
}
}

Expand Down

0 comments on commit f20486c

Please sign in to comment.