Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress console output in unit tests #28680

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
});
});
4 changes: 2 additions & 2 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
24 changes: 12 additions & 12 deletions packages/react/src/__tests__/ReactStrictMode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down