Skip to content

Commit 1dff578

Browse files
yungstersAndyPengc12
authored andcommitted
Suppress console output in unit tests (facebook#28680)
1 parent 545dd3f commit 1dff578

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

packages/react-client/src/__tests__/ReactFlight-test.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,13 @@ describe('ReactFlight', () => {
21082108
throw new Error('err');
21092109
}
21102110

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+
21112118
let transport;
21122119
expect(() => {
21132120
// Reset the modules so that we get a new overridden console on top of the
@@ -2120,22 +2127,18 @@ describe('ReactFlight', () => {
21202127
transport = ReactNoopFlightServer.render({root: <ServerComponent />});
21212128
}).toErrorDev('err');
21222129

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());
21402143
});
21412144
});

packages/react-dom/src/__tests__/ReactDOMComponent-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ describe('ReactDOMComponent', () => {
16111611
});
16121612

16131613
it('should work error event on <source> element', async () => {
1614-
spyOnDevAndProd(console, 'log');
1614+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
16151615
const container = document.createElement('div');
16161616
const root = ReactDOMClient.createRoot(container);
16171617
await act(() => {
@@ -1921,7 +1921,7 @@ describe('ReactDOMComponent', () => {
19211921
});
19221922

19231923
it('should work load and error events on <image> element in SVG', async () => {
1924-
spyOnDevAndProd(console, 'log');
1924+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
19251925
const container = document.createElement('div');
19261926
const root = ReactDOMClient.createRoot(container);
19271927
await act(() => {

packages/react-dom/src/__tests__/ReactDOMServerIntegrationInput-test.js

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ desc('ReactDOMServerIntegrationInput', () => {
4848
});
4949

5050
itRenders('an input with a bigint value and an onChange', async render => {
51-
console.log(gate(flags => flags.enableBigIntSupport));
5251
const e = await render(<input value={5n} onChange={() => {}} />);
5352
expect(e.value).toBe(
5453
gate(flags => flags.enableBigIntSupport) ||

packages/react/src/__tests__/ReactStrictMode-test.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ describe('context legacy', () => {
11501150

11511151
if (ReactFeatureFlags.consoleManagedByDevToolsDuringStrictMode) {
11521152
it('does not disable logs for class double render', async () => {
1153-
spyOnDevAndProd(console, 'log');
1153+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
11541154

11551155
let count = 0;
11561156
class Foo extends React.Component {
@@ -1179,7 +1179,7 @@ describe('context legacy', () => {
11791179
});
11801180

11811181
it('does not disable logs for class double ctor', async () => {
1182-
spyOnDevAndProd(console, 'log');
1182+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
11831183

11841184
let count = 0;
11851185
class Foo extends React.Component {
@@ -1211,7 +1211,7 @@ describe('context legacy', () => {
12111211
});
12121212

12131213
it('does not disable logs for class double getDerivedStateFromProps', async () => {
1214-
spyOnDevAndProd(console, 'log');
1214+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
12151215

12161216
let count = 0;
12171217
class Foo extends React.Component {
@@ -1244,7 +1244,7 @@ describe('context legacy', () => {
12441244
});
12451245

12461246
it('does not disable logs for class double shouldComponentUpdate', async () => {
1247-
spyOnDevAndProd(console, 'log');
1247+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
12481248

12491249
let count = 0;
12501250
class Foo extends React.Component {
@@ -1285,7 +1285,7 @@ describe('context legacy', () => {
12851285
});
12861286

12871287
it('does not disable logs for class state updaters', async () => {
1288-
spyOnDevAndProd(console, 'log');
1288+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
12891289

12901290
let inst;
12911291
let count = 0;
@@ -1323,7 +1323,7 @@ describe('context legacy', () => {
13231323
});
13241324

13251325
it('does not disable logs for function double render', async () => {
1326-
spyOnDevAndProd(console, 'log');
1326+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
13271327

13281328
let count = 0;
13291329
function Foo() {
@@ -1350,7 +1350,7 @@ describe('context legacy', () => {
13501350
});
13511351
} else {
13521352
it('disable logs for class double render', async () => {
1353-
spyOnDevAndProd(console, 'log');
1353+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
13541354

13551355
let count = 0;
13561356
class Foo extends React.Component {
@@ -1379,7 +1379,7 @@ describe('context legacy', () => {
13791379
});
13801380

13811381
it('disables logs for class double ctor', async () => {
1382-
spyOnDevAndProd(console, 'log');
1382+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
13831383

13841384
let count = 0;
13851385
class Foo extends React.Component {
@@ -1411,7 +1411,7 @@ describe('context legacy', () => {
14111411
});
14121412

14131413
it('disable logs for class double getDerivedStateFromProps', async () => {
1414-
spyOnDevAndProd(console, 'log');
1414+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
14151415

14161416
let count = 0;
14171417
class Foo extends React.Component {
@@ -1444,7 +1444,7 @@ describe('context legacy', () => {
14441444
});
14451445

14461446
it('disable logs for class double shouldComponentUpdate', async () => {
1447-
spyOnDevAndProd(console, 'log');
1447+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
14481448

14491449
let count = 0;
14501450
class Foo extends React.Component {
@@ -1484,7 +1484,7 @@ describe('context legacy', () => {
14841484
});
14851485

14861486
it('disable logs for class state updaters', async () => {
1487-
spyOnDevAndProd(console, 'log');
1487+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
14881488

14891489
let inst;
14901490
let count = 0;
@@ -1522,7 +1522,7 @@ describe('context legacy', () => {
15221522
});
15231523

15241524
it('disable logs for function double render', async () => {
1525-
spyOnDevAndProd(console, 'log');
1525+
spyOnDevAndProd(console, 'log').mockImplementation(() => {});
15261526

15271527
let count = 0;
15281528
function Foo() {

0 commit comments

Comments
 (0)