Repository: facebook/react
Pull request: Suppress console output in unit tests

## Summary

Changes all unit tests that were spewing console output via `console.log` to no longer do so.

This will help reduce logspew and simplify parsing the reported Jest results.

## How did you test this change?

See no more logspew when running unit tests.

```
$ yarn test
```

Changed files:
filename: packages/react-client/src/__tests__/ReactFlight-test.js
patch: @@ -2108,6 +2108,13 @@ describe('ReactFlight', () => {
       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 +2127,18 @@ describe('ReactFlight', () => {
       transport = ReactNoopFlightServer.render({root: <ServerComponent />});
     }).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());
   });
 });

filename: packages/react-dom/src/__tests__/ReactDOMComponent-test.js
patch: @@ -1611,7 +1611,7 @@ describe('ReactDOMComponent', () => {
     });
 
     it('should work error event on <source> 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 <image> element in SVG', async () => {
-      spyOnDevAndProd(console, 'log');
+      spyOnDevAndProd(console, 'log').mockImplementation(() => {});
       const container = document.createElement('div');
       const root = ReactDOMClient.createRoot(container);
       await act(() => {

filename: packages/react-dom/src/__tests__/ReactDOMServerIntegrationInput-test.js
patch: @@ -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(<input value={5n} onChange={() => {}} />);
     expect(e.value).toBe(
       gate(flags => flags.enableBigIntSupport) ||

filename: packages/react/src/__tests__/ReactStrictMode-test.js
patch: @@ -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() {