Skip to content

Commit 22ab39b

Browse files
author
Brian Vaughn
authored
DevTools console patching should handle Symbols without erroring (#21368)
1 parent 2182563 commit 22ab39b

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

packages/react-devtools-shared/src/__tests__/console-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,4 +453,16 @@ describe('console', () => {
453453
'\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',
454454
);
455455
});
456+
457+
it('should correctly log Symbols', () => {
458+
const Component = ({children}) => {
459+
fakeConsole.warn('Symbol:', Symbol(''));
460+
return null;
461+
};
462+
463+
act(() => ReactDOM.render(<Component />, document.createElement('div')));
464+
465+
expect(mockWarn).toHaveBeenCalledTimes(1);
466+
expect(mockWarn.mock.calls[0][0]).toBe('Symbol:');
467+
});
456468
});

packages/react-devtools-shared/src/backend/console.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export function patch({
144144
if (consoleSettingsRef.appendComponentStack) {
145145
const lastArg = args.length > 0 ? args[args.length - 1] : null;
146146
const alreadyHasComponentStack =
147-
lastArg !== null && isStringComponentStack(lastArg);
147+
typeof lastArg === 'string' && isStringComponentStack(lastArg);
148148

149149
// If we are ever called with a string that already has a component stack,
150150
// e.g. a React error/warning, don't append a second stack.

packages/react-devtools-shell/src/app/InlineWarnings/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ function ComponentWithMissingKey({children}) {
149149
return [<div />];
150150
}
151151

152+
function ComponentWithSymbolWarning() {
153+
console.warn('this is a symbol', Symbol('foo'));
154+
console.error('this is a symbol', Symbol.for('bar'));
155+
return null;
156+
}
157+
152158
export default function ErrorsAndWarnings() {
153159
const [count, setCount] = useState(0);
154160
const handleClick = () => setCount(count + 1);
@@ -176,6 +182,7 @@ export default function ErrorsAndWarnings() {
176182
<ReallyLongErrorMessageThatWillCauseTextToBeTruncated />
177183
<DuplicateWarningsAndErrors />
178184
<MultipleWarningsAndErrors />
185+
<ComponentWithSymbolWarning />
179186
</Fragment>
180187
);
181188
}

0 commit comments

Comments
 (0)