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

Function createMatcherFor

tests/toWarnDev.js:52–298  ·  view source on GitHub ↗
(consoleMethod, matcherName)

Source from the content-addressed store, hash-verified

50}
51
52const createMatcherFor = (consoleMethod, matcherName) =>
53 function matcher(callback, expectedMessages, options = {}) {
54 if (process.env.NODE_ENV !== 'production') {
55 // Warn about incorrect usage of matcher.
56 if (typeof expectedMessages === 'string') {
57 expectedMessages = [expectedMessages]
58 } else if (!Array.isArray(expectedMessages)) {
59 throw Error(
60 `${matcherName}() requires a parameter of type string or an array of strings ` +
61 `but was given ${typeof expectedMessages}.`,
62 )
63 }
64 if (
65 options != null &&
66 (typeof options !== 'object' || Array.isArray(options))
67 ) {
68 throw new Error(
69 `${matcherName}() second argument, when present, should be an object. ` +
70 'Did you forget to wrap the messages into an array?',
71 )
72 }
73 if (arguments.length > 3) {
74 // `matcher` comes from Jest, so it's more than 2 in practice
75 throw new Error(
76 `${matcherName}() received more than two arguments. ` +
77 'Did you forget to wrap the messages into an array?',
78 )
79 }
80
81 const withoutStack = options.withoutStack
82 const logAllErrors = options.logAllErrors
83 const warningsWithoutComponentStack = []
84 const warningsWithComponentStack = []
85 const unexpectedWarnings = []
86
87 let lastWarningWithMismatchingFormat = null
88 let lastWarningWithExtraComponentStack = null
89
90 // Catch errors thrown by the callback,
91 // But only rethrow them if all test expectations have been satisfied.
92 // Otherwise an Error in the callback can mask a failed expectation,
93 // and result in a test that passes when it shouldn't.
94 let caughtError
95
96 const isLikelyAComponentStack = message =>
97 typeof message === 'string' &&
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 }

Callers 1

toWarnDev.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…