Skip to content

Commit 22988a6

Browse files
author
Josh Goldberg
authored
fix: add explicit error message for null or undefined matchers (#718)
* Added explicit error message for null or undefined matchers * Updated message * Whoops, no quotes
1 parent f854cde commit 22988a6

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/__tests__/matches.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,16 @@ test('matchers return false if text to match is not a string', () => {
2626
expect(matches(null, node, 'ABC', normalizer)).toBe(false)
2727
expect(fuzzyMatches(null, node, 'ABC', normalizer)).toBe(false)
2828
})
29+
30+
test('matchers throw on invalid matcher inputs', () => {
31+
expect(() =>
32+
matches('ABC', node, null, normalizer),
33+
).toThrowErrorMatchingInlineSnapshot(
34+
`"It looks like null was passed instead of a matcher. Did you do something like getByText(null)?"`,
35+
)
36+
expect(() =>
37+
fuzzyMatches('ABC', node, undefined, normalizer),
38+
).toThrowErrorMatchingInlineSnapshot(
39+
`"It looks like undefined was passed instead of a matcher. Did you do something like getByText(undefined)?"`,
40+
)
41+
})

src/matches.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
function assertNotNullOrUndefined(matcher) {
2+
if (matcher == null) {
3+
throw new Error(
4+
`It looks like ${matcher} was passed instead of a matcher. Did you do something like getByText(${matcher})?`,
5+
)
6+
}
7+
}
8+
19
function fuzzyMatches(textToMatch, node, matcher, normalizer) {
210
if (typeof textToMatch !== 'string') {
311
return false
412
}
513

14+
assertNotNullOrUndefined(matcher)
15+
616
const normalizedText = normalizer(textToMatch)
717
if (typeof matcher === 'string') {
818
return normalizedText.toLowerCase().includes(matcher.toLowerCase())
@@ -18,6 +28,8 @@ function matches(textToMatch, node, matcher, normalizer) {
1828
return false
1929
}
2030

31+
assertNotNullOrUndefined(matcher)
32+
2133
const normalizedText = normalizer(textToMatch)
2234
if (typeof matcher === 'string') {
2335
return normalizedText === matcher

0 commit comments

Comments
 (0)