Skip to content

Commit

Permalink
Updated toWarnDev matcher so that ReactFiberScheduler won't suppress …
Browse files Browse the repository at this point in the history
…its errors (#11958)
  • Loading branch information
bvaughn committed Jan 3, 2018
1 parent 9f848f8 commit bb881f2
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions scripts/jest/matchers/toWarnDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const createMatcherFor = consoleMethod =>
);
}

const unexpectedWarnings = [];

const consoleSpy = message => {
const normalizedMessage = normalizeCodeLocInfo(message);

Expand All @@ -31,14 +33,19 @@ const createMatcherFor = consoleMethod =>
}
}

// Fail early for unexpected warnings to preserve the call stack.
throw Error(
`Unexpected warning recorded:\n ${this.utils.printReceived(
message
)}\n\nThe following expected warnings were not yet seen:\n ${this.utils.printExpected(
expectedMessages.join('\n')
)}`
);
let errorMessage = `Unexpected warning recorded:\n${this.utils.printReceived(
message
)}`;
if (expectedMessages.length > 0) {
errorMessage += `\n\nThe following expected warnings were not yet seen:\n${expectedMessages
.map(unformatted => this.utils.printExpected(unformatted))
.join('\n')}`;
}

// Record the call stack for unexpected warnings.
// We don't throw an Error here though,
// Because it might be suppressed by ReactFiberScheduler.
unexpectedWarnings.push(new Error(errorMessage));
};

// TODO Decide whether we need to support nested toWarn* expectations.
Expand All @@ -52,6 +59,14 @@ const createMatcherFor = consoleMethod =>
try {
callback();

// Any unexpected warnings should be treated as a failure.
if (unexpectedWarnings.length > 0) {
return {
message: () => unexpectedWarnings[0].stack,
pass: false,
};
}

// Any remaining messages indicate a failed expectations.
if (expectedMessages.length > 0) {
return {
Expand Down

0 comments on commit bb881f2

Please sign in to comment.