Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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