Skip to content

Commit

Permalink
Don't strip out component stack in assertConsole helpers (#30204)
Browse files Browse the repository at this point in the history
Use the same normalizeCodeLocInfo that we use everywhere else.

We should actually test the component stack itself. Not just that it
exists. This was causing false passes.

However, the logic was also wrong before because it wouldn't always
strip out the last line so wouldn't accurately normalize it. Leading to
false failures as well.
  • Loading branch information
sebmarkbage authored Jul 3, 2024
1 parent 6e169fc commit 15ca8b6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 50 deletions.
100 changes: 56 additions & 44 deletions packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,8 @@ describe('ReactInternalTestUtils console assertions', () => {
- Hi
- Wow
- Bye
+ Wow <component stack>
+ Bye <component stack>"
+ Wow in div (at **)
+ Bye in div (at **)"
`);
});

Expand All @@ -1025,8 +1025,8 @@ describe('ReactInternalTestUtils console assertions', () => {
- Hi
- Wow
- Bye
+ Hi <component stack>
+ Bye <component stack>"
+ Hi in div (at **)
+ Bye in div (at **)"
`);
});

Expand All @@ -1048,8 +1048,8 @@ describe('ReactInternalTestUtils console assertions', () => {
- Hi
- Wow
- Bye
+ Hi <component stack>
+ Wow <component stack>"
+ Hi in div (at **)
+ Wow in div (at **)"
`);
});

Expand All @@ -1071,9 +1071,9 @@ describe('ReactInternalTestUtils console assertions', () => {
- Wow
- Bye
+ Hi <component stack>
+ Wow <component stack>
+ Bye <component stack>"
+ Hi in div (at **)
+ Wow in div (at **)
+ Bye in div (at **)"
`);
});

Expand All @@ -1095,9 +1095,9 @@ describe('ReactInternalTestUtils console assertions', () => {
- Hi
- Bye
+ Hi <component stack>
+ Wow <component stack>
+ Bye <component stack>"
+ Hi in div (at **)
+ Wow in div (at **)
+ Bye in div (at **)"
`);
});

Expand All @@ -1119,9 +1119,9 @@ describe('ReactInternalTestUtils console assertions', () => {
- Hi
- Wow
+ Hi <component stack>
+ Wow <component stack>
+ Bye <component stack>"
+ Hi in div (at **)
+ Wow in div (at **)
+ Bye in div (at **)"
`);
});

Expand Down Expand Up @@ -1297,7 +1297,8 @@ describe('ReactInternalTestUtils console assertions', () => {
"assertConsoleWarnDev(expected)
Unexpected component stack for:
"Hello <component stack>"
"Hello
in div (at **)"
If this warning should include a component stack, remove {withoutStack: true} from this warning.
If all warnings should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleWarnDev call."
Expand All @@ -1318,10 +1319,12 @@ describe('ReactInternalTestUtils console assertions', () => {
"assertConsoleWarnDev(expected)
Unexpected component stack for:
"Hello <component stack>"
"Hello
in div (at **)"
Unexpected component stack for:
"Bye <component stack>"
"Bye
in div (at **)"
If this warning should include a component stack, remove {withoutStack: true} from this warning.
If all warnings should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleWarnDev call."
Expand Down Expand Up @@ -1444,7 +1447,8 @@ describe('ReactInternalTestUtils console assertions', () => {
"assertConsoleWarnDev(expected)
Unexpected component stack for:
"Hello <component stack>"
"Hello
in div (at **)"
If this warning should include a component stack, remove {withoutStack: true} from this warning.
If all warnings should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleWarnDev call."
Expand Down Expand Up @@ -1477,10 +1481,12 @@ describe('ReactInternalTestUtils console assertions', () => {
"assertConsoleWarnDev(expected)
Unexpected component stack for:
"Hello <component stack>"
"Hello
in div (at **)"
Unexpected component stack for:
"Bye <component stack>"
"Bye
in div (at **)"
If this warning should include a component stack, remove {withoutStack: true} from this warning.
If all warnings should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleWarnDev call."
Expand Down Expand Up @@ -1934,8 +1940,8 @@ describe('ReactInternalTestUtils console assertions', () => {
- Hi
- Wow
- Bye
+ Wow <component stack>
+ Bye <component stack>"
+ Wow in div (at **)
+ Bye in div (at **)"
`);
});

Expand All @@ -1957,8 +1963,8 @@ describe('ReactInternalTestUtils console assertions', () => {
- Hi
- Wow
- Bye
+ Hi <component stack>
+ Bye <component stack>"
+ Hi in div (at **)
+ Bye in div (at **)"
`);
});

Expand All @@ -1980,8 +1986,8 @@ describe('ReactInternalTestUtils console assertions', () => {
- Hi
- Wow
- Bye
+ Hi <component stack>
+ Wow <component stack>"
+ Hi in div (at **)
+ Wow in div (at **)"
`);
});

Expand All @@ -2003,9 +2009,9 @@ describe('ReactInternalTestUtils console assertions', () => {
- Wow
- Bye
+ Hi <component stack>
+ Wow <component stack>
+ Bye <component stack>"
+ Hi in div (at **)
+ Wow in div (at **)
+ Bye in div (at **)"
`);
});

Expand All @@ -2027,9 +2033,9 @@ describe('ReactInternalTestUtils console assertions', () => {
- Hi
- Bye
+ Hi <component stack>
+ Wow <component stack>
+ Bye <component stack>"
+ Hi in div (at **)
+ Wow in div (at **)
+ Bye in div (at **)"
`);
});

Expand All @@ -2051,9 +2057,9 @@ describe('ReactInternalTestUtils console assertions', () => {
- Hi
- Wow
+ Hi <component stack>
+ Wow <component stack>
+ Bye <component stack>"
+ Hi in div (at **)
+ Wow in div (at **)
+ Bye in div (at **)"
`);
});
// @gate __DEV__
Expand Down Expand Up @@ -2170,7 +2176,7 @@ describe('ReactInternalTestUtils console assertions', () => {
+ Received errors
- This is a completely different message that happens to start with "T"
+ Message that happens to contain a "T" <component stack>"
+ Message that happens to contain a "T" in div (at **)"
`);
});

Expand Down Expand Up @@ -2247,7 +2253,8 @@ describe('ReactInternalTestUtils console assertions', () => {
"assertConsoleErrorDev(expected)
Unexpected component stack for:
"Hello <component stack>"
"Hello
in div (at **)"
If this error should include a component stack, remove {withoutStack: true} from this error.
If all errors should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleErrorDev call."
Expand All @@ -2268,10 +2275,12 @@ describe('ReactInternalTestUtils console assertions', () => {
"assertConsoleErrorDev(expected)
Unexpected component stack for:
"Hello <component stack>"
"Hello
in div (at **)"
Unexpected component stack for:
"Bye <component stack>"
"Bye
in div (at **)"
If this error should include a component stack, remove {withoutStack: true} from this error.
If all errors should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleErrorDev call."
Expand Down Expand Up @@ -2394,7 +2403,8 @@ describe('ReactInternalTestUtils console assertions', () => {
"assertConsoleErrorDev(expected)
Unexpected component stack for:
"Hello <component stack>"
"Hello
in div (at **)"
If this error should include a component stack, remove {withoutStack: true} from this error.
If all errors should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleErrorDev call."
Expand Down Expand Up @@ -2427,10 +2437,12 @@ describe('ReactInternalTestUtils console assertions', () => {
"assertConsoleErrorDev(expected)
Unexpected component stack for:
"Hello <component stack>"
"Hello
in div (at **)"
Unexpected component stack for:
"Bye <component stack>"
"Bye
in div (at **)"
If this error should include a component stack, remove {withoutStack: true} from this error.
If all errors should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleErrorDev call."
Expand Down Expand Up @@ -2459,7 +2471,7 @@ describe('ReactInternalTestUtils console assertions', () => {
+ Received errors
- Hello
+ Bye <component stack>"
+ Bye in div (at **)"
`);
});
});
Expand Down
17 changes: 11 additions & 6 deletions packages/internal-test-utils/consoleMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export function assertConsoleLogsCleared() {
}
}

function replaceComponentStack(str) {
function normalizeCodeLocInfo(str) {
if (typeof str !== 'string') {
return str;
}
Expand All @@ -239,8 +239,13 @@ function replaceComponentStack(str) {
// at Component (/path/filename.js:123:45)
// React format:
// in Component (at filename.js:123)
return str.replace(/\n +(?:at|in) ([\S]+)[^\n]*.*/, function (m, name) {
return chalk.dim(' <component stack>');
return str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function (m, name) {
if (name.endsWith('.render')) {
// Class components will have the `render` method as part of their stack trace.
// We strip that out in our normalization to make it look more like component stacks.
name = name.slice(0, name.length - 7);
}
return '\n in ' + name + ' (at **)';
});
}

Expand Down Expand Up @@ -382,11 +387,11 @@ export function createLogAssertion(
);
}

expectedMessage = replaceComponentStack(currentExpectedMessage);
expectedMessage = normalizeCodeLocInfo(currentExpectedMessage);
expectedWithoutStack = expectedMessageOrArray[1].withoutStack;
} else if (typeof expectedMessageOrArray === 'string') {
// Should be in the form assert(['log']) or assert(['log'], {withoutStack: true})
expectedMessage = replaceComponentStack(expectedMessageOrArray);
expectedMessage = normalizeCodeLocInfo(expectedMessageOrArray);
if (consoleMethod === 'log') {
expectedWithoutStack = true;
} else {
Expand All @@ -410,7 +415,7 @@ export function createLogAssertion(
);
}

const normalizedMessage = replaceComponentStack(message);
const normalizedMessage = normalizeCodeLocInfo(message);
receivedLogs.push(normalizedMessage);

// Check the number of %s interpolations.
Expand Down

0 comments on commit 15ca8b6

Please sign in to comment.