From accbf230fd5b9159c458753146945a4cddcbbd40 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 25 Jul 2020 14:08:22 -0400 Subject: [PATCH 1/3] Added explicit error message for null or undefined matchers --- src/__tests__/matches.js | 13 +++++++++++++ src/matches.js | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/__tests__/matches.js b/src/__tests__/matches.js index 5e4d7243..9be46a6b 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 you passed null instead of a matcher. Did you do something like getByText(\\"null\\")?"`, + ) + expect(() => + fuzzyMatches('ABC', node, undefined, normalizer), + ).toThrowErrorMatchingInlineSnapshot( + `"It looks like you passed undefined instead of a matcher. Did you do something like getByText(\\"undefined\\")?"`, + ) +}) diff --git a/src/matches.js b/src/matches.js index 3241e680..ba9d1c77 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 you passed ${matcher} 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 From 30bfa7984f8a5131e01b389a4c17adbefc6efaac Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 25 Jul 2020 14:41:15 -0400 Subject: [PATCH 2/3] Updated message --- src/__tests__/matches.js | 4 ++-- src/matches.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/__tests__/matches.js b/src/__tests__/matches.js index 9be46a6b..86ca2be1 100644 --- a/src/__tests__/matches.js +++ b/src/__tests__/matches.js @@ -31,11 +31,11 @@ test('matchers throw on invalid matcher inputs', () => { expect(() => matches('ABC', node, null, normalizer), ).toThrowErrorMatchingInlineSnapshot( - `"It looks like you passed null instead of a matcher. Did you do something like getByText(\\"null\\")?"`, + `"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 you passed undefined instead of a matcher. Did you do something like getByText(\\"undefined\\")?"`, + `"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 ba9d1c77..14ff6e50 100644 --- a/src/matches.js +++ b/src/matches.js @@ -1,7 +1,7 @@ function assertNotNullOrUndefined(matcher) { if (matcher == null) { throw new Error( - `It looks like you passed ${matcher} instead of a matcher. Did you do something like getByText("${matcher}")?`, + `It looks like ${matcher} was passed instead of a matcher. Did you do something like getByText("${matcher}")?`, ) } } From 35799ca93755450488d7cb5c9e8ccb603a49c24f Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 25 Jul 2020 14:58:35 -0400 Subject: [PATCH 3/3] Whoops, no quotes --- src/__tests__/matches.js | 4 ++-- src/matches.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/__tests__/matches.js b/src/__tests__/matches.js index 86ca2be1..3f5e6b3e 100644 --- a/src/__tests__/matches.js +++ b/src/__tests__/matches.js @@ -31,11 +31,11 @@ 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\\")?"`, + `"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\\")?"`, + `"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 14ff6e50..baad25bb 100644 --- a/src/matches.js +++ b/src/matches.js @@ -1,7 +1,7 @@ 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}")?`, + `It looks like ${matcher} was passed instead of a matcher. Did you do something like getByText(${matcher})?`, ) } }