diff --git a/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js b/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js index ad3a7cdce7cae..befbdc617d361 100644 --- a/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js +++ b/packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js @@ -866,16 +866,40 @@ describe('ReactInternalTestUtils console assertions', () => { const message = expectToThrowFailure(() => { expect(root).toMatchRenderedOutput(
foobarbaz
); }); - expect(message).toMatchInlineSnapshot(` - "asserConsoleLogsCleared(expected) + if (!__DEV__) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) - console.log was called without assertConsoleLogDev: - + Not asserted - + Not asserted - + Not asserted + console.log was called without assertConsoleLogDev: + + Not asserted + + Not asserted + + Not asserted - You must call one of the assertConsoleDev helpers between each act call." - `); + You must call one of the assertConsoleDev helpers between each act call." + `); + } else if (gate(flags => flags.enableOwnerStacks)) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.log was called without assertConsoleLogDev: + + Not asserted + + Not asserted + + Not asserted + + You must call one of the assertConsoleDev helpers between each act call." + `); + } else { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.log was called without assertConsoleLogDev: + + Not asserted + + Not asserted + + Not asserted + + You must call one of the assertConsoleDev helpers between each act call." + `); + } expect(root).toMatchRenderedOutput(
foobarbaz
); }); @@ -922,16 +946,52 @@ describe('ReactInternalTestUtils console assertions', () => { }); }); - expect(message).toMatchInlineSnapshot(` - "asserConsoleLogsCleared(expected) + if (!__DEV__) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) - console.warn was called without assertConsoleWarnDev: - + A - + B - + C + console.warn was called without assertConsoleWarnDev: + + A + + B + + C - You must call one of the assertConsoleDev helpers between each act call." - `); + You must call one of the assertConsoleDev helpers between each act call." + `); + } else if (gate(flags => flags.enableOwnerStacks)) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.warn was called without assertConsoleWarnDev: + + 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." + `); + } else { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.warn was called without assertConsoleWarnDev: + + A%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + B%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + C%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + You must call one of the assertConsoleDev helpers between each act call." + `); + } }); it('fails if act is called without any assertConsoleDev helpers', async () => { @@ -962,26 +1022,94 @@ describe('ReactInternalTestUtils console assertions', () => { }); }); - expect(message).toMatchInlineSnapshot(` - "asserConsoleLogsCleared(expected) + if (!__DEV__) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) - console.log was called without assertConsoleLogDev: - + A - + B - + C + console.log was called without assertConsoleLogDev: + + A + + B + + C - console.warn was called without assertConsoleWarnDev: - + A - + B - + C + console.warn was called without assertConsoleWarnDev: + + A + + B + + C - console.error was called without assertConsoleErrorDev: - + A - + B - + C + console.error was called without assertConsoleErrorDev: + + A + + B + + C - You must call one of the assertConsoleDev helpers between each act call." - `); + You must call one of the assertConsoleDev helpers between each act call." + `); + } else if (gate(flags => flags.enableOwnerStacks)) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.log was called without assertConsoleLogDev: + + A + + B + + C + + console.warn was called without assertConsoleWarnDev: + + A%s, + + in App (at **) + + B%s, + + in App (at **) + + C%s, + + in App (at **) + + console.error was called without assertConsoleErrorDev: + + 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." + `); + } else { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.log was called without assertConsoleLogDev: + + A + + B + + C + + console.warn was called without assertConsoleWarnDev: + + A%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + B%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + C%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + console.error was called without assertConsoleErrorDev: + + A%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + B%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + C%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + You must call one of the assertConsoleDev helpers between each act call." + `); + } }); // @gate __DEV__ @@ -1804,16 +1932,49 @@ describe('ReactInternalTestUtils console assertions', () => { const message = expectToThrowFailure(() => { expect(root).toMatchRenderedOutput(
foobarbaz
); }); - expect(message).toMatchInlineSnapshot(` - "asserConsoleLogsCleared(expected) + if (!__DEV__) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) - console.warn was called without assertConsoleWarnDev: - + Not asserted - + Not asserted - + Not asserted + console.warn was called without assertConsoleWarnDev: + + Not asserted + + Not asserted + + Not asserted - You must call one of the assertConsoleDev helpers between each act call." - `); + You must call one of the assertConsoleDev helpers between each act call." + `); + } else if (gate(flags => flags.enableOwnerStacks)) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.warn was called without assertConsoleWarnDev: + + 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." + `); + } else { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.warn was called without assertConsoleWarnDev: + + Not asserted%s, + + in Yield (at **) + + in div (at **) + + Not asserted%s, + + in Yield (at **) + + in div (at **) + + Not asserted%s, + + in Yield (at **) + + in div (at **) + + You must call one of the assertConsoleDev helpers between each act call." + `); + } expect(root).toMatchRenderedOutput(
foobarbaz
); }); @@ -1860,16 +2021,52 @@ describe('ReactInternalTestUtils console assertions', () => { }); }); - expect(message).toMatchInlineSnapshot(` - "asserConsoleLogsCleared(expected) + if (!__DEV__) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) - console.error was called without assertConsoleErrorDev: - + A - + B - + C + console.error was called without assertConsoleErrorDev: + + A + + B + + C - You must call one of the assertConsoleDev helpers between each act call." - `); + You must call one of the assertConsoleDev helpers between each act call." + `); + } else if (gate(flags => flags.enableOwnerStacks)) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.error was called without assertConsoleErrorDev: + + 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." + `); + } else { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.error was called without assertConsoleErrorDev: + + A%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + B%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + C%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + You must call one of the assertConsoleDev helpers between each act call." + `); + } }); it('fails if act is called without any assertConsoleDev helpers', async () => { @@ -1900,26 +2097,94 @@ describe('ReactInternalTestUtils console assertions', () => { }); }); - expect(message).toMatchInlineSnapshot(` - "asserConsoleLogsCleared(expected) + if (!__DEV__) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) - console.log was called without assertConsoleLogDev: - + A - + B - + C + console.log was called without assertConsoleLogDev: + + A + + B + + C - console.warn was called without assertConsoleWarnDev: - + A - + B - + C + console.warn was called without assertConsoleWarnDev: + + A + + B + + C - console.error was called without assertConsoleErrorDev: - + A - + B - + C + console.error was called without assertConsoleErrorDev: + + A + + B + + C - You must call one of the assertConsoleDev helpers between each act call." - `); + You must call one of the assertConsoleDev helpers between each act call." + `); + } else if (gate(flags => flags.enableOwnerStacks)) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.log was called without assertConsoleLogDev: + + A + + B + + C + + console.warn was called without assertConsoleWarnDev: + + A%s, + + in App (at **) + + B%s, + + in App (at **) + + C%s, + + in App (at **) + + console.error was called without assertConsoleErrorDev: + + 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." + `); + } else { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.log was called without assertConsoleLogDev: + + A + + B + + C + + console.warn was called without assertConsoleWarnDev: + + A%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + B%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + C%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + console.error was called without assertConsoleErrorDev: + + A%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + B%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + C%s, + + in Yield (at **) + + in div (at **) + + in App (at **) + + You must call one of the assertConsoleDev helpers between each act call." + `); + } }); // @gate __DEV__ @@ -2786,16 +3051,49 @@ describe('ReactInternalTestUtils console assertions', () => { const message = expectToThrowFailure(() => { expect(root).toMatchRenderedOutput(
foobarbaz
); }); - expect(message).toMatchInlineSnapshot(` - "asserConsoleLogsCleared(expected) + if (!__DEV__) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) - console.error was called without assertConsoleErrorDev: - + Not asserted - + Not asserted - + Not asserted + console.error was called without assertConsoleErrorDev: + + Not asserted + + Not asserted + + Not asserted - You must call one of the assertConsoleDev helpers between each act call." - `); + You must call one of the assertConsoleDev helpers between each act call." + `); + } else if (gate(flags => flags.enableOwnerStacks)) { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.error was called without assertConsoleErrorDev: + + 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." + `); + } else { + expect(message).toMatchInlineSnapshot(` + "asserConsoleLogsCleared(expected) + + console.error was called without assertConsoleErrorDev: + + Not asserted%s, + + in Yield (at **) + + in div (at **) + + Not asserted%s, + + in Yield (at **) + + in div (at **) + + Not asserted%s, + + in Yield (at **) + + in div (at **) + + You must call one of the assertConsoleDev helpers between each act call." + `); + } expect(root).toMatchRenderedOutput(
foobarbaz
); }); diff --git a/packages/internal-test-utils/consoleMock.js b/packages/internal-test-utils/consoleMock.js index 3d8cee8ce8693..c14a52d0544d7 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,19 @@ 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 ||