MCPcopy
hub / github.com/facebook/react / patchConsoleMethod

Function patchConsoleMethod

packages/internal-test-utils/consoleMock.js:23–61  ·  view source on GitHub ↗
(methodName, logged)

Source from the content-addressed store, hash-verified

21const loggedLogs = (global.__loggedLogs = global.__loggedLogs || []);
22
23const patchConsoleMethod = (methodName, logged) => {
24 const newMethod = function (format, ...args) {
25 // Ignore uncaught errors reported by jsdom
26 // and React addendums because they're too noisy.
27 if (shouldIgnoreConsoleError(format, args)) {
28 return;
29 }
30
31 // Ignore certain React warnings causing test failures
32 if (methodName === 'warn' && shouldIgnoreConsoleWarn(format)) {
33 return;
34 }
35
36 // Append Component Stacks. Simulates a framework or DevTools appending them.
37 if (
38 typeof format === 'string' &&
39 (methodName === 'error' || methodName === 'warn')
40 ) {
41 const React = require('react');
42
43 // Ideally we could remove this check, but we have some tests like
44 // useSyncExternalStoreShared-test that tests against React 17,
45 // which doesn't have the captureOwnerStack method.
46 if (React.captureOwnerStack) {
47 const stack = React.captureOwnerStack();
48 if (stack) {
49 format += '%s';
50 args.push(stack);
51 }
52 }
53 }
54
55 logged.push([format, ...args]);
56 };
57
58 console[methodName] = newMethod;
59
60 return newMethod;
61};
62
63let logMethod;
64export function patchConsoleMethods({includeLog} = {includeLog: false}) {

Callers 1

patchConsoleMethodsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected