Skip to content

Added explicit error message for null or undefined matchers #718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/__tests__/matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ test('matchers return false if text to match is not a string', () => {
expect(matches(null, node, 'ABC', normalizer)).toBe(false)
expect(fuzzyMatches(null, node, 'ABC', normalizer)).toBe(false)
})

test('matchers throw on invalid matcher inputs', () => {
expect(() =>
matches('ABC', node, null, normalizer),
).toThrowErrorMatchingInlineSnapshot(
`"It looks like null was passed instead of a matcher. Did you do something like getByText(null)?"`,
)
expect(() =>
fuzzyMatches('ABC', node, undefined, normalizer),
).toThrowErrorMatchingInlineSnapshot(
`"It looks like undefined was passed instead of a matcher. Did you do something like getByText(undefined)?"`,
)
})
12 changes: 12 additions & 0 deletions src/matches.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
function assertNotNullOrUndefined(matcher) {
if (matcher == null) {
throw new Error(
`It looks like ${matcher} was passed instead of a matcher. Did you do something like getByText(${matcher})?`,
)
}
}

function fuzzyMatches(textToMatch, node, matcher, normalizer) {
if (typeof textToMatch !== 'string') {
return false
}

assertNotNullOrUndefined(matcher)

const normalizedText = normalizer(textToMatch)
if (typeof matcher === 'string') {
return normalizedText.toLowerCase().includes(matcher.toLowerCase())
Expand All @@ -18,6 +28,8 @@ function matches(textToMatch, node, matcher, normalizer) {
return false
}

assertNotNullOrUndefined(matcher)

const normalizedText = normalizer(textToMatch)
if (typeof matcher === 'string') {
return normalizedText === matcher
Expand Down