Skip to content

Commit 098745b

Browse files
authored
Improved toWarnDev matcher to avoid swallowing errors (#12081)
While writing tests for unsafe async warnings, I noticed that in certain cases, errors were swallowed by the toWarnDev matcher and resulted in confusing test failures. For example, if an error prevented the code being tested from logging an expected warning- the test would fail saying that the warning hadn't been logged rather than reporting the unexpected error. I think a better approach for this is to always treat caught errors as the highest-priority reason for failing a test. I reran all of the test cases for this matcher that I originally ran with PR #11786 and ensured they all still pass.
1 parent cba51ba commit 098745b

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

scripts/jest/matchers/toWarnDev.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ const createMatcherFor = consoleMethod =>
7070
// Restore the unspied method so that unexpected errors fail tests.
7171
console[consoleMethod] = originalMethod;
7272

73+
// Any unexpected Errors thrown by the callback should fail the test.
74+
// This should take precedence since unexpected errors could block warnings.
75+
if (caughtError) {
76+
throw caughtError;
77+
}
78+
7379
// Any unexpected warnings should be treated as a failure.
7480
if (unexpectedWarnings.length > 0) {
7581
return {
@@ -89,11 +95,6 @@ const createMatcherFor = consoleMethod =>
8995
};
9096
}
9197

92-
// Any unexpected Errors thrown by the callback should fail the test.
93-
if (caughtError) {
94-
throw caughtError;
95-
}
96-
9798
return {pass: true};
9899
}
99100
} else {

0 commit comments

Comments
 (0)