MCPcopy Create free account
hub / github.com/testing-library/react-testing-library / consoleSpy

Function consoleSpy

tests/toWarnDev.js:100–174  ·  view source on GitHub ↗
(format, ...args)

Source from the content-addressed store, hash-verified

98 (message.includes('\n in ') || message.includes('\n at '))
99
100 const consoleSpy = (format, ...args) => {
101 // Ignore uncaught errors reported by jsdom
102 // and React addendums because they're too noisy.
103 if (
104 !logAllErrors &&
105 consoleMethod === 'error' &&
106 shouldIgnoreConsoleError(format, args)
107 ) {
108 return
109 }
110
111 const message = util.format(format, ...args)
112 const normalizedMessage = normalizeCodeLocInfo(message)
113
114 // Remember if the number of %s interpolations
115 // doesn't match the number of arguments.
116 // We'll fail the test if it happens.
117 let argIndex = 0
118 String(format).replace(/%s/g, () => argIndex++)
119 if (argIndex !== args.length) {
120 lastWarningWithMismatchingFormat = {
121 format,
122 args,
123 expectedArgCount: argIndex,
124 }
125 }
126
127 // Protect against accidentally passing a component stack
128 // to warning() which already injects the component stack.
129 if (
130 args.length >= 2 &&
131 isLikelyAComponentStack(args[args.length - 1]) &&
132 isLikelyAComponentStack(args[args.length - 2])
133 ) {
134 lastWarningWithExtraComponentStack = {
135 format,
136 }
137 }
138
139 for (let index = 0; index < expectedMessages.length; index++) {
140 const expectedMessage = expectedMessages[index]
141 if (
142 normalizedMessage === expectedMessage ||
143 normalizedMessage.includes(expectedMessage)
144 ) {
145 if (isLikelyAComponentStack(normalizedMessage)) {
146 warningsWithComponentStack.push(normalizedMessage)
147 } else {
148 warningsWithoutComponentStack.push(normalizedMessage)
149 }
150 expectedMessages.splice(index, 1)
151 return
152 }
153 }
154
155 let errorMessage
156 if (expectedMessages.length === 0) {
157 errorMessage =

Callers

nothing calls this directly

Calls 2

normalizeCodeLocInfoFunction · 0.85
isLikelyAComponentStackFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…