Skip to content

Commit

Permalink
Add type checks for asymmetric matchers (#5069)
Browse files Browse the repository at this point in the history
* Add type checks for asymmetric matchers

* Expect other to only be a string
  • Loading branch information
rogeliog authored and cpojer committed Jan 7, 2018
1 parent a9a3df0 commit b86d932
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/expect/src/__tests__/asymmetric_matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ test('ObjectContaining throws for non-objects', () => {
test('StringContaining matches string against string', () => {
jestExpect(stringContaining('en*').asymmetricMatch('queen*')).toBe(true);
jestExpect(stringContaining('en').asymmetricMatch('queue')).toBe(false);
jestExpect(stringContaining('en').asymmetricMatch({})).toBe(false);
});

test('StringContaining throws for non-strings', () => {
Expand All @@ -153,11 +154,13 @@ test('StringContaining throws for non-strings', () => {
test('StringMatching matches string against regexp', () => {
jestExpect(stringMatching(/en/).asymmetricMatch('queen')).toBe(true);
jestExpect(stringMatching(/en/).asymmetricMatch('queue')).toBe(false);
jestExpect(stringMatching(/en/).asymmetricMatch({})).toBe(false);
});

test('StringMatching matches string against string', () => {
jestExpect(stringMatching('en').asymmetricMatch('queen')).toBe(true);
jestExpect(stringMatching('en').asymmetricMatch('queue')).toBe(false);
jestExpect(stringMatching('en').asymmetricMatch({})).toBe(false);
});

test('StringMatching throws for non-strings and non-regexps', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/expect/src/asymmetric_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ class StringContaining extends AsymmetricMatcher {
}

asymmetricMatch(other: string) {
if (!isA('String', other)) {
return false;
}

return other.includes(this.sample);
}

Expand All @@ -218,6 +222,10 @@ class StringMatching extends AsymmetricMatcher {
}

asymmetricMatch(other: string) {
if (!isA('String', other)) {
return false;
}

return this.sample.test(other);
}

Expand Down

0 comments on commit b86d932

Please sign in to comment.