Skip to content

Commit

Permalink
Update test of the error messages in the tool itself
Browse files Browse the repository at this point in the history
They now sometimes include a normalized component stack if one was logged.
  • Loading branch information
sebmarkbage committed Jul 10, 2024
1 parent bf9949c commit 500e177
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -926,9 +926,12 @@ describe('ReactInternalTestUtils console assertions', () => {
"asserConsoleLogsCleared(expected)
console.warn was called without assertConsoleWarnDev:
+ A
+ B
+ C
+ A%s,
+ in App (at **)
+ B%s,
+ in App (at **)
+ C%s,
+ in App (at **)
You must call one of the assertConsoleDev helpers between each act call."
`);
Expand Down Expand Up @@ -971,14 +974,20 @@ describe('ReactInternalTestUtils console assertions', () => {
+ C
console.warn was called without assertConsoleWarnDev:
+ A
+ B
+ C
+ A%s,
+ in App (at **)
+ B%s,
+ in App (at **)
+ C%s,
+ in App (at **)
console.error was called without assertConsoleErrorDev:
+ A
+ B
+ C
+ A%s,
+ in App (at **)
+ B%s,
+ in App (at **)
+ C%s,
+ in App (at **)
You must call one of the assertConsoleDev helpers between each act call."
`);
Expand Down Expand Up @@ -1808,9 +1817,12 @@ describe('ReactInternalTestUtils console assertions', () => {
"asserConsoleLogsCleared(expected)
console.warn was called without assertConsoleWarnDev:
+ Not asserted
+ Not asserted
+ Not asserted
+ Not asserted%s,
+ in Yield (at **)
+ Not asserted%s,
+ in Yield (at **)
+ Not asserted%s,
+ in Yield (at **)
You must call one of the assertConsoleDev helpers between each act call."
`);
Expand Down Expand Up @@ -1864,9 +1876,12 @@ describe('ReactInternalTestUtils console assertions', () => {
"asserConsoleLogsCleared(expected)
console.error was called without assertConsoleErrorDev:
+ A
+ B
+ C
+ A%s,
+ in App (at **)
+ B%s,
+ in App (at **)
+ C%s,
+ in App (at **)
You must call one of the assertConsoleDev helpers between each act call."
`);
Expand Down Expand Up @@ -1909,14 +1924,20 @@ describe('ReactInternalTestUtils console assertions', () => {
+ C
console.warn was called without assertConsoleWarnDev:
+ A
+ B
+ C
+ A%s,
+ in App (at **)
+ B%s,
+ in App (at **)
+ C%s,
+ in App (at **)
console.error was called without assertConsoleErrorDev:
+ A
+ B
+ C
+ A%s,
+ in App (at **)
+ B%s,
+ in App (at **)
+ C%s,
+ in App (at **)
You must call one of the assertConsoleDev helpers between each act call."
`);
Expand Down Expand Up @@ -2790,9 +2811,12 @@ describe('ReactInternalTestUtils console assertions', () => {
"asserConsoleLogsCleared(expected)
console.error was called without assertConsoleErrorDev:
+ Not asserted
+ Not asserted
+ Not asserted
+ Not asserted%s,
+ in Yield (at **)
+ Not asserted%s,
+ in Yield (at **)
+ Not asserted%s,
+ in Yield (at **)
You must call one of the assertConsoleDev helpers between each act call."
`);
Expand Down
14 changes: 12 additions & 2 deletions packages/internal-test-utils/consoleMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export function assertConsoleLogsCleared() {
if (warnings.length > 0) {
message += `\nconsole.warn was called without assertConsoleWarnDev:\n${diff(
'',
warnings.join('\n'),
warnings.map(normalizeComponentStack).join('\n'),
{
omitAnnotationLines: true,
},
Expand All @@ -241,7 +241,7 @@ export function assertConsoleLogsCleared() {
if (errors.length > 0) {
message += `\nconsole.error was called without assertConsoleErrorDev:\n${diff(
'',
errors.join('\n'),
errors.map(normalizeComponentStack).join('\n'),
{
omitAnnotationLines: true,
},
Expand Down Expand Up @@ -277,6 +277,16 @@ function normalizeCodeLocInfo(str) {
});
}

function normalizeComponentStack(entry) {
if (typeof entry[0] === 'string' && entry[0].endsWith('%s') &&
isLikelyAComponentStack(entry[entry.length - 1])) {
const clone = entry.slice(0);
clone[clone.length - 1] = normalizeCodeLocInfo(entry[entry.length - 1]);
return clone;
}
return entry;
}

const isLikelyAComponentStack = message =>
typeof message === 'string' &&
(message.indexOf('<component stack>') > -1 ||
Expand Down

0 comments on commit 500e177

Please sign in to comment.