@samitouri / QOS-React / commits / 9f835e69ab

Suppress console output in unit tests (#28680)

Timothy Yung committed Mar 30, 2024 at 09:36 UTC 9f835e69ab1c87192b5da4421519b69785c12f69
4 files changed +34 -32
packages/react-client/src/__tests__/ReactFlight-test.js
+20 -17
@@ -2108,6 +2108,13 @@ describe('ReactFlight', () => {
2108 throw new Error('err');
2109 }
2110
2111 + // Assign to `mockConsoleLog` so we can still inspect it when `console.log`
2112 + // is overridden by the test modules. The original function will be restored
2113 + // after this test finishes by `jest.restoreAllMocks()`.
2114 + const mockConsoleLog = spyOnDevAndProd(console, 'log').mockImplementation(
2115 + () => {},
2116 + );
2117 +
2118 let transport;
2119 expect(() => {
2120 // Reset the modules so that we get a new overridden console on top of the
@@ -2120,22 +2127,18 @@ describe('ReactFlight', () => {
2127 transport = ReactNoopFlightServer.render({root: <ServerComponent />});
2128 }).toErrorDev('err');
2129
2123 - const log = console.log;
2124 - try {
2125 - console.log = jest.fn();
2126 - // The error should not actually get logged because we're not awaiting the root
2127 - // so it's not thrown but the server log also shouldn't be replayed.
2128 - await ReactNoopFlightClient.read(transport);
2129 -
2130 - expect(console.log).toHaveBeenCalledTimes(1);
2131 - expect(console.log.mock.calls[0][0]).toBe('hi');
2132 - expect(console.log.mock.calls[0][1].prop).toBe(123);
2133 - const loggedFn = console.log.mock.calls[0][1].fn;
2134 - expect(typeof loggedFn).toBe('function');
2135 - expect(loggedFn).not.toBe(foo);
2136 - expect(loggedFn.toString()).toBe(foo.toString());
2137 - } finally {
2138 - console.log = log;
2139 - }
2130 + mockConsoleLog.mockClear();
2131 +
2132 + // The error should not actually get logged because we're not awaiting the root
2133 + // so it's not thrown but the server log also shouldn't be replayed.
2134 + await ReactNoopFlightClient.read(transport);
2135 +
2136 + expect(mockConsoleLog).toHaveBeenCalledTimes(1);
2137 + expect(mockConsoleLog.mock.calls[0][0]).toBe('hi');
2138 + expect(mockConsoleLog.mock.calls[0][1].prop).toBe(123);
2139 + const loggedFn = mockConsoleLog.mock.calls[0][1].fn;
2140 + expect(typeof loggedFn).toBe('function');
2141 + expect(loggedFn).not.toBe(foo);
2142 + expect(loggedFn.toString()).toBe(foo.toString());
2143 });
2144 });
packages/react-dom/src/__tests__/ReactDOMComponent-test.js
+2 -2
@@ -1611,7 +1611,7 @@ describe('ReactDOMComponent', () => {
1611 });
1612
1613 it('should work error event on <source> element', async () => {
1614 - spyOnDevAndProd(console, 'log');
1614 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1615 const container = document.createElement('div');
1616 const root = ReactDOMClient.createRoot(container);
1617 await act(() => {
@@ -1921,7 +1921,7 @@ describe('ReactDOMComponent', () => {
1921 });
1922
1923 it('should work load and error events on <image> element in SVG', async () => {
1924 - spyOnDevAndProd(console, 'log');
1924 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1925 const container = document.createElement('div');
1926 const root = ReactDOMClient.createRoot(container);
1927 await act(() => {
packages/react-dom/src/__tests__/ReactDOMServerIntegrationInput-test.js
-1
@@ -48,7 +48,6 @@ desc('ReactDOMServerIntegrationInput', () => {
48 });
49
50 itRenders('an input with a bigint value and an onChange', async render => {
51 - console.log(gate(flags => flags.enableBigIntSupport));
51 const e = await render(<input value={5n} onChange={() => {}} />);
52 expect(e.value).toBe(
53 gate(flags => flags.enableBigIntSupport) ||
packages/react/src/__tests__/ReactStrictMode-test.js
+12 -12
@@ -1150,7 +1150,7 @@ describe('context legacy', () => {
1150
1151 if (ReactFeatureFlags.consoleManagedByDevToolsDuringStrictMode) {
1152 it('does not disable logs for class double render', async () => {
1153 - spyOnDevAndProd(console, 'log');
1153 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1154
1155 let count = 0;
1156 class Foo extends React.Component {
@@ -1179,7 +1179,7 @@ describe('context legacy', () => {
1179 });
1180
1181 it('does not disable logs for class double ctor', async () => {
1182 - spyOnDevAndProd(console, 'log');
1182 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1183
1184 let count = 0;
1185 class Foo extends React.Component {
@@ -1211,7 +1211,7 @@ describe('context legacy', () => {
1211 });
1212
1213 it('does not disable logs for class double getDerivedStateFromProps', async () => {
1214 - spyOnDevAndProd(console, 'log');
1214 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1215
1216 let count = 0;
1217 class Foo extends React.Component {
@@ -1244,7 +1244,7 @@ describe('context legacy', () => {
1244 });
1245
1246 it('does not disable logs for class double shouldComponentUpdate', async () => {
1247 - spyOnDevAndProd(console, 'log');
1247 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1248
1249 let count = 0;
1250 class Foo extends React.Component {
@@ -1285,7 +1285,7 @@ describe('context legacy', () => {
1285 });
1286
1287 it('does not disable logs for class state updaters', async () => {
1288 - spyOnDevAndProd(console, 'log');
1288 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1289
1290 let inst;
1291 let count = 0;
@@ -1323,7 +1323,7 @@ describe('context legacy', () => {
1323 });
1324
1325 it('does not disable logs for function double render', async () => {
1326 - spyOnDevAndProd(console, 'log');
1326 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1327
1328 let count = 0;
1329 function Foo() {
@@ -1350,7 +1350,7 @@ describe('context legacy', () => {
1350 });
1351 } else {
1352 it('disable logs for class double render', async () => {
1353 - spyOnDevAndProd(console, 'log');
1353 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1354
1355 let count = 0;
1356 class Foo extends React.Component {
@@ -1379,7 +1379,7 @@ describe('context legacy', () => {
1379 });
1380
1381 it('disables logs for class double ctor', async () => {
1382 - spyOnDevAndProd(console, 'log');
1382 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1383
1384 let count = 0;
1385 class Foo extends React.Component {
@@ -1411,7 +1411,7 @@ describe('context legacy', () => {
1411 });
1412
1413 it('disable logs for class double getDerivedStateFromProps', async () => {
1414 - spyOnDevAndProd(console, 'log');
1414 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1415
1416 let count = 0;
1417 class Foo extends React.Component {
@@ -1444,7 +1444,7 @@ describe('context legacy', () => {
1444 });
1445
1446 it('disable logs for class double shouldComponentUpdate', async () => {
1447 - spyOnDevAndProd(console, 'log');
1447 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1448
1449 let count = 0;
1450 class Foo extends React.Component {
@@ -1484,7 +1484,7 @@ describe('context legacy', () => {
1484 });
1485
1486 it('disable logs for class state updaters', async () => {
1487 - spyOnDevAndProd(console, 'log');
1487 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1488
1489 let inst;
1490 let count = 0;
@@ -1522,7 +1522,7 @@ describe('context legacy', () => {
1522 });
1523
1524 it('disable logs for function double render', async () => {
1525 - spyOnDevAndProd(console, 'log');
1525 + spyOnDevAndProd(console, 'log').mockImplementation(() => {});
1526
1527 let count = 0;
1528 function Foo() {