|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const util = require('util') |
|
|
const jestDiff = require('jest-diff').diff |
|
|
const shouldIgnoreConsoleError = require('./shouldIgnoreConsoleError') |
|
|
|
|
|
function normalizeCodeLocInfo(str) { |
|
|
if (typeof str !== 'string') { |
|
|
return str |
|
|
} |
|
|
|
|
|
|
|
|
str = str.replace(/Check your code at .+?:\d+/g, 'Check your code at **') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function (m, name) { |
|
|
return '\n in ' + name + ' (at **)' |
|
|
}) |
|
|
} |
|
|
|
|
|
const createMatcherFor = (consoleMethod, matcherName) => |
|
|
function matcher(callback, expectedMessages, options = {}) { |
|
|
if (process.env.NODE_ENV !== 'production') { |
|
|
|
|
|
if (typeof expectedMessages === 'string') { |
|
|
expectedMessages = [expectedMessages] |
|
|
} else if (!Array.isArray(expectedMessages)) { |
|
|
throw Error( |
|
|
`${matcherName}() requires a parameter of type string or an array of strings ` + |
|
|
`but was given ${typeof expectedMessages}.`, |
|
|
) |
|
|
} |
|
|
if ( |
|
|
options != null && |
|
|
(typeof options !== 'object' || Array.isArray(options)) |
|
|
) { |
|
|
throw new Error( |
|
|
`${matcherName}() second argument, when present, should be an object. ` + |
|
|
'Did you forget to wrap the messages into an array?', |
|
|
) |
|
|
} |
|
|
if (arguments.length > 3) { |
|
|
|
|
|
throw new Error( |
|
|
`${matcherName}() received more than two arguments. ` + |
|
|
'Did you forget to wrap the messages into an array?', |
|
|
) |
|
|
} |
|
|
|
|
|
const withoutStack = options.withoutStack |
|
|
const logAllErrors = options.logAllErrors |
|
|
const warningsWithoutComponentStack = [] |
|
|
const warningsWithComponentStack = [] |
|
|
const unexpectedWarnings = [] |
|
|
|
|
|
let lastWarningWithMismatchingFormat = null |
|
|
let lastWarningWithExtraComponentStack = null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let caughtError |
|
|
|
|
|
const isLikelyAComponentStack = message => |
|
|
typeof message === 'string' && |
|
|
(message.includes('\n in ') || message.includes('\n at ')) |
|
|
|
|
|
const consoleSpy = (format, ...args) => { |
|
|
|
|
|
|
|
|
if ( |
|
|
!logAllErrors && |
|
|
consoleMethod === 'error' && |
|
|
shouldIgnoreConsoleError(format, args) |
|
|
) { |
|
|
return |
|
|
} |
|
|
|
|
|
const message = util.format(format, ...args) |
|
|
const normalizedMessage = normalizeCodeLocInfo(message) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let argIndex = 0 |
|
|
String(format).replace(/%s/g, () => argIndex++) |
|
|
if (argIndex !== args.length) { |
|
|
lastWarningWithMismatchingFormat = { |
|
|
format, |
|
|
args, |
|
|
expectedArgCount: argIndex, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
args.length >= 2 && |
|
|
isLikelyAComponentStack(args[args.length - 1]) && |
|
|
isLikelyAComponentStack(args[args.length - 2]) |
|
|
) { |
|
|
lastWarningWithExtraComponentStack = { |
|
|
format, |
|
|
} |
|
|
} |
|
|
|
|
|
for (let index = 0; index < expectedMessages.length; index++) { |
|
|
const expectedMessage = expectedMessages[index] |
|
|
if ( |
|
|
normalizedMessage === expectedMessage || |
|
|
normalizedMessage.includes(expectedMessage) |
|
|
) { |
|
|
if (isLikelyAComponentStack(normalizedMessage)) { |
|
|
warningsWithComponentStack.push(normalizedMessage) |
|
|
} else { |
|
|
warningsWithoutComponentStack.push(normalizedMessage) |
|
|
} |
|
|
expectedMessages.splice(index, 1) |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
let errorMessage |
|
|
if (expectedMessages.length === 0) { |
|
|
errorMessage = |
|
|
'Unexpected warning recorded: ' + |
|
|
this.utils.printReceived(normalizedMessage) |
|
|
} else if (expectedMessages.length === 1) { |
|
|
errorMessage = |
|
|
'Unexpected warning recorded: ' + |
|
|
jestDiff(expectedMessages[0], normalizedMessage) |
|
|
} else { |
|
|
errorMessage = |
|
|
'Unexpected warning recorded: ' + |
|
|
jestDiff(expectedMessages, [normalizedMessage]) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unexpectedWarnings.push(new Error(errorMessage)) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const originalMethod = console[consoleMethod] |
|
|
|
|
|
|
|
|
console[consoleMethod] = consoleSpy |
|
|
|
|
|
try { |
|
|
callback() |
|
|
} catch (error) { |
|
|
caughtError = error |
|
|
} finally { |
|
|
|
|
|
console[consoleMethod] = originalMethod |
|
|
|
|
|
|
|
|
|
|
|
if (caughtError) { |
|
|
throw caughtError |
|
|
} |
|
|
|
|
|
|
|
|
if (unexpectedWarnings.length > 0) { |
|
|
return { |
|
|
message: () => unexpectedWarnings[0].stack, |
|
|
pass: false, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (expectedMessages.length > 0) { |
|
|
return { |
|
|
message: () => |
|
|
`Expected warning was not recorded:\n ${this.utils.printReceived( |
|
|
expectedMessages[0], |
|
|
)}`, |
|
|
pass: false, |
|
|
} |
|
|
} |
|
|
|
|
|
if (typeof withoutStack === 'number') { |
|
|
|
|
|
if (withoutStack !== warningsWithoutComponentStack.length) { |
|
|
return { |
|
|
message: () => |
|
|
`Expected ${withoutStack} warnings without a component stack but received ${warningsWithoutComponentStack.length}:\n` + |
|
|
warningsWithoutComponentStack.map(warning => |
|
|
this.utils.printReceived(warning), |
|
|
), |
|
|
pass: false, |
|
|
} |
|
|
} |
|
|
} else if (withoutStack === true) { |
|
|
|
|
|
|
|
|
if (warningsWithComponentStack.length > 0) { |
|
|
return { |
|
|
message: () => |
|
|
`Received warning unexpectedly includes a component stack:\n ${this.utils.printReceived( |
|
|
warningsWithComponentStack[0], |
|
|
)}\nIf this warning intentionally includes the component stack, remove ` + |
|
|
`{withoutStack: true} from the ${matcherName}() call. If you have a mix of ` + |
|
|
`warnings with and without stack in one ${matcherName}() call, pass ` + |
|
|
`{withoutStack: N} where N is the number of warnings without stacks.`, |
|
|
pass: false, |
|
|
} |
|
|
} |
|
|
} else if (withoutStack === false || withoutStack === undefined) { |
|
|
|
|
|
|
|
|
if (warningsWithoutComponentStack.length > 0) { |
|
|
return { |
|
|
message: () => |
|
|
`Received warning unexpectedly does not include a component stack:\n ${this.utils.printReceived( |
|
|
warningsWithoutComponentStack[0], |
|
|
)}\nIf this warning intentionally omits the component stack, add ` + |
|
|
`{withoutStack: true} to the ${matcherName} call.`, |
|
|
pass: false, |
|
|
} |
|
|
} |
|
|
} else { |
|
|
throw Error( |
|
|
`The second argument for ${matcherName}(), when specified, must be an object. It may have a ` + |
|
|
`property called "withoutStack" whose value may be undefined, boolean, or a number. ` + |
|
|
`Instead received ${typeof withoutStack}.`, |
|
|
) |
|
|
} |
|
|
|
|
|
if (lastWarningWithMismatchingFormat !== null) { |
|
|
return { |
|
|
message: () => |
|
|
`Received ${ |
|
|
lastWarningWithMismatchingFormat.args.length |
|
|
} arguments for a message with ${ |
|
|
lastWarningWithMismatchingFormat.expectedArgCount |
|
|
} placeholders:\n ${this.utils.printReceived( |
|
|
lastWarningWithMismatchingFormat.format, |
|
|
)}`, |
|
|
pass: false, |
|
|
} |
|
|
} |
|
|
|
|
|
if (lastWarningWithExtraComponentStack !== null) { |
|
|
return { |
|
|
message: () => |
|
|
`Received more than one component stack for a warning:\n ${this.utils.printReceived( |
|
|
lastWarningWithExtraComponentStack.format, |
|
|
)}\nDid you accidentally pass a stack to warning() as the last argument? ` + |
|
|
`Don't forget warning() already injects the component stack automatically.`, |
|
|
pass: false, |
|
|
} |
|
|
} |
|
|
|
|
|
return {pass: true} |
|
|
} |
|
|
} else { |
|
|
|
|
|
callback() |
|
|
|
|
|
return {pass: true} |
|
|
} |
|
|
} |
|
|
|
|
|
module.exports = { |
|
|
toWarnDev: createMatcherFor('warn', 'toWarnDev'), |
|
|
toErrorDev: createMatcherFor('error', 'toErrorDev'), |
|
|
} |
|
|
|