@samitouri / QOS-React / commits / bb881f2de7

Updated toWarnDev matcher so that ReactFiberScheduler won't suppress its errors (#11958)

Brian Vaughn committed Jan 3, 2018 at 15:23 UTC bb881f2de794a5d4990741af5fb1397b0f3003e8
1 file changed +23 -8
scripts/jest/matchers/toWarnDev.js
+23 -8
@@ -17,6 +17,8 @@ const createMatcherFor = consoleMethod =>
17 );
18 }
19
20 + const unexpectedWarnings = [];
21 +
22 const consoleSpy = message => {
23 const normalizedMessage = normalizeCodeLocInfo(message);
24
@@ -31,14 +33,19 @@ const createMatcherFor = consoleMethod =>
33 }
34 }
35
34 - // Fail early for unexpected warnings to preserve the call stack.
35 - throw Error(
36 - `Unexpected warning recorded:\n ${this.utils.printReceived(
37 - message
38 - )}\n\nThe following expected warnings were not yet seen:\n ${this.utils.printExpected(
39 - expectedMessages.join('\n')
40 - )}`
41 - );
36 + let errorMessage = `Unexpected warning recorded:\n${this.utils.printReceived(
37 + message
38 + )}`;
39 + if (expectedMessages.length > 0) {
40 + errorMessage += `\n\nThe following expected warnings were not yet seen:\n${expectedMessages
41 + .map(unformatted => this.utils.printExpected(unformatted))
42 + .join('\n')}`;
43 + }
44 +
45 + // Record the call stack for unexpected warnings.
46 + // We don't throw an Error here though,
47 + // Because it might be suppressed by ReactFiberScheduler.
48 + unexpectedWarnings.push(new Error(errorMessage));
49 };
50
51 // TODO Decide whether we need to support nested toWarn* expectations.
@@ -52,6 +59,14 @@ const createMatcherFor = consoleMethod =>
59 try {
60 callback();
61
62 + // Any unexpected warnings should be treated as a failure.
63 + if (unexpectedWarnings.length > 0) {
64 + return {
65 + message: () => unexpectedWarnings[0].stack,
66 + pass: false,
67 + };
68 + }
69 +
70 // Any remaining messages indicate a failed expectations.
71 if (expectedMessages.length > 0) {
72 return {