Skip to content

Commit a0f3777

Browse files
committed
Don't strip out component stack in assertConsole helpers
Use the same normalizeCodeLocInfo that we use everywhere else.
1 parent 6e169fc commit a0f3777

File tree

2 files changed

+67
-50
lines changed

2 files changed

+67
-50
lines changed

packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,8 @@ describe('ReactInternalTestUtils console assertions', () => {
10021002
- Hi
10031003
- Wow
10041004
- Bye
1005-
+ Wow <component stack>
1006-
+ Bye <component stack>"
1005+
+ Wow in div (at **)
1006+
+ Bye in div (at **)"
10071007
`);
10081008
});
10091009

@@ -1025,8 +1025,8 @@ describe('ReactInternalTestUtils console assertions', () => {
10251025
- Hi
10261026
- Wow
10271027
- Bye
1028-
+ Hi <component stack>
1029-
+ Bye <component stack>"
1028+
+ Hi in div (at **)
1029+
+ Bye in div (at **)"
10301030
`);
10311031
});
10321032

@@ -1048,8 +1048,8 @@ describe('ReactInternalTestUtils console assertions', () => {
10481048
- Hi
10491049
- Wow
10501050
- Bye
1051-
+ Hi <component stack>
1052-
+ Wow <component stack>"
1051+
+ Hi in div (at **)
1052+
+ Wow in div (at **)"
10531053
`);
10541054
});
10551055

@@ -1071,9 +1071,9 @@ describe('ReactInternalTestUtils console assertions', () => {
10711071
10721072
- Wow
10731073
- Bye
1074-
+ Hi <component stack>
1075-
+ Wow <component stack>
1076-
+ Bye <component stack>"
1074+
+ Hi in div (at **)
1075+
+ Wow in div (at **)
1076+
+ Bye in div (at **)"
10771077
`);
10781078
});
10791079

@@ -1095,9 +1095,9 @@ describe('ReactInternalTestUtils console assertions', () => {
10951095
10961096
- Hi
10971097
- Bye
1098-
+ Hi <component stack>
1099-
+ Wow <component stack>
1100-
+ Bye <component stack>"
1098+
+ Hi in div (at **)
1099+
+ Wow in div (at **)
1100+
+ Bye in div (at **)"
11011101
`);
11021102
});
11031103

@@ -1119,9 +1119,9 @@ describe('ReactInternalTestUtils console assertions', () => {
11191119
11201120
- Hi
11211121
- Wow
1122-
+ Hi <component stack>
1123-
+ Wow <component stack>
1124-
+ Bye <component stack>"
1122+
+ Hi in div (at **)
1123+
+ Wow in div (at **)
1124+
+ Bye in div (at **)"
11251125
`);
11261126
});
11271127

@@ -1297,7 +1297,8 @@ describe('ReactInternalTestUtils console assertions', () => {
12971297
"assertConsoleWarnDev(expected)
12981298
12991299
Unexpected component stack for:
1300-
"Hello <component stack>"
1300+
"Hello
1301+
in div (at **)"
13011302
13021303
If this warning should include a component stack, remove {withoutStack: true} from this warning.
13031304
If all warnings should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleWarnDev call."
@@ -1318,10 +1319,12 @@ describe('ReactInternalTestUtils console assertions', () => {
13181319
"assertConsoleWarnDev(expected)
13191320
13201321
Unexpected component stack for:
1321-
"Hello <component stack>"
1322+
"Hello
1323+
in div (at **)"
13221324
13231325
Unexpected component stack for:
1324-
"Bye <component stack>"
1326+
"Bye
1327+
in div (at **)"
13251328
13261329
If this warning should include a component stack, remove {withoutStack: true} from this warning.
13271330
If all warnings should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleWarnDev call."
@@ -1444,7 +1447,8 @@ describe('ReactInternalTestUtils console assertions', () => {
14441447
"assertConsoleWarnDev(expected)
14451448
14461449
Unexpected component stack for:
1447-
"Hello <component stack>"
1450+
"Hello
1451+
in div (at **)"
14481452
14491453
If this warning should include a component stack, remove {withoutStack: true} from this warning.
14501454
If all warnings should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleWarnDev call."
@@ -1477,10 +1481,12 @@ describe('ReactInternalTestUtils console assertions', () => {
14771481
"assertConsoleWarnDev(expected)
14781482
14791483
Unexpected component stack for:
1480-
"Hello <component stack>"
1484+
"Hello
1485+
in div (at **)"
14811486
14821487
Unexpected component stack for:
1483-
"Bye <component stack>"
1488+
"Bye
1489+
in div (at **)"
14841490
14851491
If this warning should include a component stack, remove {withoutStack: true} from this warning.
14861492
If all warnings should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleWarnDev call."
@@ -1934,8 +1940,8 @@ describe('ReactInternalTestUtils console assertions', () => {
19341940
- Hi
19351941
- Wow
19361942
- Bye
1937-
+ Wow <component stack>
1938-
+ Bye <component stack>"
1943+
+ Wow in div (at **)
1944+
+ Bye in div (at **)"
19391945
`);
19401946
});
19411947

@@ -1957,8 +1963,8 @@ describe('ReactInternalTestUtils console assertions', () => {
19571963
- Hi
19581964
- Wow
19591965
- Bye
1960-
+ Hi <component stack>
1961-
+ Bye <component stack>"
1966+
+ Hi in div (at **)
1967+
+ Bye in div (at **)"
19621968
`);
19631969
});
19641970

@@ -1980,8 +1986,8 @@ describe('ReactInternalTestUtils console assertions', () => {
19801986
- Hi
19811987
- Wow
19821988
- Bye
1983-
+ Hi <component stack>
1984-
+ Wow <component stack>"
1989+
+ Hi in div (at **)
1990+
+ Wow in div (at **)"
19851991
`);
19861992
});
19871993

@@ -2003,9 +2009,9 @@ describe('ReactInternalTestUtils console assertions', () => {
20032009
20042010
- Wow
20052011
- Bye
2006-
+ Hi <component stack>
2007-
+ Wow <component stack>
2008-
+ Bye <component stack>"
2012+
+ Hi in div (at **)
2013+
+ Wow in div (at **)
2014+
+ Bye in div (at **)"
20092015
`);
20102016
});
20112017

@@ -2027,9 +2033,9 @@ describe('ReactInternalTestUtils console assertions', () => {
20272033
20282034
- Hi
20292035
- Bye
2030-
+ Hi <component stack>
2031-
+ Wow <component stack>
2032-
+ Bye <component stack>"
2036+
+ Hi in div (at **)
2037+
+ Wow in div (at **)
2038+
+ Bye in div (at **)"
20332039
`);
20342040
});
20352041

@@ -2051,9 +2057,9 @@ describe('ReactInternalTestUtils console assertions', () => {
20512057
20522058
- Hi
20532059
- Wow
2054-
+ Hi <component stack>
2055-
+ Wow <component stack>
2056-
+ Bye <component stack>"
2060+
+ Hi in div (at **)
2061+
+ Wow in div (at **)
2062+
+ Bye in div (at **)"
20572063
`);
20582064
});
20592065
// @gate __DEV__
@@ -2170,7 +2176,7 @@ describe('ReactInternalTestUtils console assertions', () => {
21702176
+ Received errors
21712177
21722178
- This is a completely different message that happens to start with "T"
2173-
+ Message that happens to contain a "T" <component stack>"
2179+
+ Message that happens to contain a "T" in div (at **)"
21742180
`);
21752181
});
21762182

@@ -2247,7 +2253,8 @@ describe('ReactInternalTestUtils console assertions', () => {
22472253
"assertConsoleErrorDev(expected)
22482254
22492255
Unexpected component stack for:
2250-
"Hello <component stack>"
2256+
"Hello
2257+
in div (at **)"
22512258
22522259
If this error should include a component stack, remove {withoutStack: true} from this error.
22532260
If all errors should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleErrorDev call."
@@ -2268,10 +2275,12 @@ describe('ReactInternalTestUtils console assertions', () => {
22682275
"assertConsoleErrorDev(expected)
22692276
22702277
Unexpected component stack for:
2271-
"Hello <component stack>"
2278+
"Hello
2279+
in div (at **)"
22722280
22732281
Unexpected component stack for:
2274-
"Bye <component stack>"
2282+
"Bye
2283+
in div (at **)"
22752284
22762285
If this error should include a component stack, remove {withoutStack: true} from this error.
22772286
If all errors should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleErrorDev call."
@@ -2394,7 +2403,8 @@ describe('ReactInternalTestUtils console assertions', () => {
23942403
"assertConsoleErrorDev(expected)
23952404
23962405
Unexpected component stack for:
2397-
"Hello <component stack>"
2406+
"Hello
2407+
in div (at **)"
23982408
23992409
If this error should include a component stack, remove {withoutStack: true} from this error.
24002410
If all errors should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleErrorDev call."
@@ -2427,10 +2437,12 @@ describe('ReactInternalTestUtils console assertions', () => {
24272437
"assertConsoleErrorDev(expected)
24282438
24292439
Unexpected component stack for:
2430-
"Hello <component stack>"
2440+
"Hello
2441+
in div (at **)"
24312442
24322443
Unexpected component stack for:
2433-
"Bye <component stack>"
2444+
"Bye
2445+
in div (at **)"
24342446
24352447
If this error should include a component stack, remove {withoutStack: true} from this error.
24362448
If all errors should include the component stack, you may need to remove {withoutStack: true} from the assertConsoleErrorDev call."
@@ -2459,7 +2471,7 @@ describe('ReactInternalTestUtils console assertions', () => {
24592471
+ Received errors
24602472
24612473
- Hello
2462-
+ Bye <component stack>"
2474+
+ Bye in div (at **)"
24632475
`);
24642476
});
24652477
});

packages/internal-test-utils/consoleMock.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export function assertConsoleLogsCleared() {
228228
}
229229
}
230230

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

@@ -382,11 +387,11 @@ export function createLogAssertion(
382387
);
383388
}
384389

385-
expectedMessage = replaceComponentStack(currentExpectedMessage);
390+
expectedMessage = normalizeCodeLocInfo(currentExpectedMessage);
386391
expectedWithoutStack = expectedMessageOrArray[1].withoutStack;
387392
} else if (typeof expectedMessageOrArray === 'string') {
388393
// Should be in the form assert(['log']) or assert(['log'], {withoutStack: true})
389-
expectedMessage = replaceComponentStack(expectedMessageOrArray);
394+
expectedMessage = normalizeCodeLocInfo(expectedMessageOrArray);
390395
if (consoleMethod === 'log') {
391396
expectedWithoutStack = true;
392397
} else {
@@ -410,7 +415,7 @@ export function createLogAssertion(
410415
);
411416
}
412417

413-
const normalizedMessage = replaceComponentStack(message);
418+
const normalizedMessage = normalizeCodeLocInfo(message);
414419
receivedLogs.push(normalizedMessage);
415420

416421
// Check the number of %s interpolations.

0 commit comments

Comments
 (0)