diff --git a/packages/react-client/src/__tests__/ReactFlight-test.js b/packages/react-client/src/__tests__/ReactFlight-test.js index 681b88f83db02..dcb3eee0b10c3 100644 --- a/packages/react-client/src/__tests__/ReactFlight-test.js +++ b/packages/react-client/src/__tests__/ReactFlight-test.js @@ -2103,11 +2103,23 @@ describe('ReactFlight', () => { function foo() { return 'hello'; } + Object.defineProperty(foo, 'name', { + get() { + process.stderr.write(new Error('sauce(foo)').stack + '\n'); + return 'sauce(foo)'; + }, + }); function ServerComponent() { console.log('hi', {prop: 123, fn: foo}); throw new Error('err'); } + // Assign to `mockConsoleLog` so we can still inspect it when `console.log` + // is overridden by the test modules. The original function will be restored + // after this test finishes by `jest.restoreAllMocks()`. + const mockConsoleLog = spyOnDevAndProd(console, 'log') + .mockImplementation(() => {}); + let transport; expect(() => { // Reset the modules so that we get a new overridden console on top of the @@ -2120,22 +2132,18 @@ describe('ReactFlight', () => { transport = ReactNoopFlightServer.render({root: }); }).toErrorDev('err'); - const log = console.log; - try { - console.log = jest.fn(); - // The error should not actually get logged because we're not awaiting the root - // so it's not thrown but the server log also shouldn't be replayed. - await ReactNoopFlightClient.read(transport); - - expect(console.log).toHaveBeenCalledTimes(1); - expect(console.log.mock.calls[0][0]).toBe('hi'); - expect(console.log.mock.calls[0][1].prop).toBe(123); - const loggedFn = console.log.mock.calls[0][1].fn; - expect(typeof loggedFn).toBe('function'); - expect(loggedFn).not.toBe(foo); - expect(loggedFn.toString()).toBe(foo.toString()); - } finally { - console.log = log; - } + mockConsoleLog.mockClear(); + + // The error should not actually get logged because we're not awaiting the root + // so it's not thrown but the server log also shouldn't be replayed. + await ReactNoopFlightClient.read(transport); + + expect(mockConsoleLog).toHaveBeenCalledTimes(1); + expect(mockConsoleLog.mock.calls[0][0]).toBe('hi'); + expect(mockConsoleLog.mock.calls[0][1].prop).toBe(123); + const loggedFn = mockConsoleLog.mock.calls[0][1].fn; + expect(typeof loggedFn).toBe('function'); + expect(loggedFn).not.toBe(foo); + expect(loggedFn.toString()).toBe(foo.toString()); }); }); diff --git a/packages/react-dom/src/__tests__/ReactDOMComponent-test.js b/packages/react-dom/src/__tests__/ReactDOMComponent-test.js index 5f2c3d14c80e0..4f31ee906d7af 100644 --- a/packages/react-dom/src/__tests__/ReactDOMComponent-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMComponent-test.js @@ -1611,7 +1611,7 @@ describe('ReactDOMComponent', () => { }); it('should work error event on element', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); const container = document.createElement('div'); const root = ReactDOMClient.createRoot(container); await act(() => { @@ -1921,7 +1921,7 @@ describe('ReactDOMComponent', () => { }); it('should work load and error events on element in SVG', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); const container = document.createElement('div'); const root = ReactDOMClient.createRoot(container); await act(() => { diff --git a/packages/react-dom/src/__tests__/ReactDOMServerIntegrationInput-test.js b/packages/react-dom/src/__tests__/ReactDOMServerIntegrationInput-test.js index 3ad7abf91a7df..9f16ed58a7617 100644 --- a/packages/react-dom/src/__tests__/ReactDOMServerIntegrationInput-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMServerIntegrationInput-test.js @@ -48,7 +48,6 @@ desc('ReactDOMServerIntegrationInput', () => { }); itRenders('an input with a bigint value and an onChange', async render => { - console.log(gate(flags => flags.enableBigIntSupport)); const e = await render( {}} />); expect(e.value).toBe( gate(flags => flags.enableBigIntSupport) || diff --git a/packages/react/src/__tests__/ReactStrictMode-test.js b/packages/react/src/__tests__/ReactStrictMode-test.js index 59351fe6a0079..cce952e39e222 100644 --- a/packages/react/src/__tests__/ReactStrictMode-test.js +++ b/packages/react/src/__tests__/ReactStrictMode-test.js @@ -1150,7 +1150,7 @@ describe('context legacy', () => { if (ReactFeatureFlags.consoleManagedByDevToolsDuringStrictMode) { it('does not disable logs for class double render', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let count = 0; class Foo extends React.Component { @@ -1179,7 +1179,7 @@ describe('context legacy', () => { }); it('does not disable logs for class double ctor', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let count = 0; class Foo extends React.Component { @@ -1211,7 +1211,7 @@ describe('context legacy', () => { }); it('does not disable logs for class double getDerivedStateFromProps', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let count = 0; class Foo extends React.Component { @@ -1244,7 +1244,7 @@ describe('context legacy', () => { }); it('does not disable logs for class double shouldComponentUpdate', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let count = 0; class Foo extends React.Component { @@ -1285,7 +1285,7 @@ describe('context legacy', () => { }); it('does not disable logs for class state updaters', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let inst; let count = 0; @@ -1323,7 +1323,7 @@ describe('context legacy', () => { }); it('does not disable logs for function double render', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let count = 0; function Foo() { @@ -1350,7 +1350,7 @@ describe('context legacy', () => { }); } else { it('disable logs for class double render', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let count = 0; class Foo extends React.Component { @@ -1379,7 +1379,7 @@ describe('context legacy', () => { }); it('disables logs for class double ctor', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let count = 0; class Foo extends React.Component { @@ -1411,7 +1411,7 @@ describe('context legacy', () => { }); it('disable logs for class double getDerivedStateFromProps', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let count = 0; class Foo extends React.Component { @@ -1444,7 +1444,7 @@ describe('context legacy', () => { }); it('disable logs for class double shouldComponentUpdate', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let count = 0; class Foo extends React.Component { @@ -1484,7 +1484,7 @@ describe('context legacy', () => { }); it('disable logs for class state updaters', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let inst; let count = 0; @@ -1522,7 +1522,7 @@ describe('context legacy', () => { }); it('disable logs for function double render', async () => { - spyOnDevAndProd(console, 'log'); + spyOnDevAndProd(console, 'log').mockImplementation(() => {}); let count = 0; function Foo() {