@samitouri / QOS-React / commits / 098745b2d1

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.

Brian Vaughn committed Jan 23, 2018 at 14:46 UTC 098745b2d16b3fc6b205ba8541530aaa48eedf77
1 file changed +6 -5
scripts/jest/matchers/toWarnDev.js
+6 -5
@@ -70,6 +70,12 @@ const createMatcherFor = consoleMethod =>
70 // Restore the unspied method so that unexpected errors fail tests.
71 console[consoleMethod] = originalMethod;
72
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 +
79 // Any unexpected warnings should be treated as a failure.
80 if (unexpectedWarnings.length > 0) {
81 return {
@@ -89,11 +95,6 @@ const createMatcherFor = consoleMethod =>
95 };
96 }
97
92 - // Any unexpected Errors thrown by the callback should fail the test.
93 - if (caughtError) {
94 - throw caughtError;
95 - }
96 -
98 return {pass: true};
99 }
100 } else {