diff --git a/src/__tests__/matches.js b/src/__tests__/matches.js index 5e4d7243..3f5e6b3e 100644 --- a/src/__tests__/matches.js +++ b/src/__tests__/matches.js @@ -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)?"`, + ) +}) diff --git a/src/matches.js b/src/matches.js index 3241e680..baad25bb 100644 --- a/src/matches.js +++ b/src/matches.js @@ -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()) @@ -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