diff --git a/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js b/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js index ad3a7cdce7cae..51ff964773395 100644 --- a/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js +++ b/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js @@ -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." `); @@ -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." `); @@ -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." `); @@ -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." `); @@ -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." `); @@ -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." `); diff --git a/packages/internal-test-utils/consoleMock.js b/packages/internal-test-utils/consoleMock.js index 3d8cee8ce8693..1c667b766f88a 100644 --- a/packages/internal-test-utils/consoleMock.js +++ b/packages/internal-test-utils/consoleMock.js @@ -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, }, @@ -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, }, @@ -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('') > -1 ||