( consoleMethod, matcherName, clearObservedErrors, )
| 188 | message.includes('\n at ')); |
| 189 | |
| 190 | export function createLogAssertion( |
| 191 | consoleMethod, |
| 192 | matcherName, |
| 193 | clearObservedErrors, |
| 194 | ) { |
| 195 | function logName() { |
| 196 | switch (consoleMethod) { |
| 197 | case 'log': |
| 198 | return 'log'; |
| 199 | case 'error': |
| 200 | return 'error'; |
| 201 | case 'warn': |
| 202 | return 'warning'; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return function assertConsoleLog(expectedMessages, options = {}) { |
| 207 | if (__DEV__) { |
| 208 | // eslint-disable-next-line no-inner-declarations |
| 209 | function throwFormattedError(message) { |
| 210 | const error = new Error( |
| 211 | `${chalk.dim(matcherName)}(${chalk.red( |
| 212 | 'expected', |
| 213 | )})\n\n${message.trim()}`, |
| 214 | ); |
| 215 | Error.captureStackTrace(error, assertConsoleLog); |
| 216 | throw error; |
| 217 | } |
| 218 | |
| 219 | // Warn about incorrect usage first arg. |
| 220 | if (!Array.isArray(expectedMessages)) { |
| 221 | throwFormattedError( |
| 222 | `Expected messages should be an array of strings ` + |
| 223 | `but was given type "${typeof expectedMessages}".`, |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | // Warn about incorrect usage second arg. |
| 228 | if (options != null) { |
| 229 | if (typeof options !== 'object' || Array.isArray(options)) { |
| 230 | throwFormattedError( |
| 231 | `The second argument should be an object. ` + |
| 232 | 'Did you forget to wrap the messages into an array?', |
| 233 | ); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | const withoutStack = options.withoutStack; |
| 238 | |
| 239 | // Warn about invalid global withoutStack values. |
| 240 | if (consoleMethod === 'log' && withoutStack !== undefined) { |
| 241 | throwFormattedError( |
| 242 | `Do not pass withoutStack to assertConsoleLogDev, console.log does not have component stacks.`, |
| 243 | ); |
| 244 | } else if (withoutStack !== undefined && withoutStack !== true) { |
| 245 | // withoutStack can only have a value true. |
| 246 | throwFormattedError( |
| 247 | `The second argument must be {withoutStack: true}.` + |
no test coverage detected