Skip to content

Commit b86d932

Browse files
rogeliogcpojer
authored andcommitted
Add type checks for asymmetric matchers (#5069)
* Add type checks for asymmetric matchers * Expect other to only be a string
1 parent a9a3df0 commit b86d932

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

packages/expect/src/__tests__/asymmetric_matchers.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ test('ObjectContaining throws for non-objects', () => {
142142
test('StringContaining matches string against string', () => {
143143
jestExpect(stringContaining('en*').asymmetricMatch('queen*')).toBe(true);
144144
jestExpect(stringContaining('en').asymmetricMatch('queue')).toBe(false);
145+
jestExpect(stringContaining('en').asymmetricMatch({})).toBe(false);
145146
});
146147

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

158160
test('StringMatching matches string against string', () => {
159161
jestExpect(stringMatching('en').asymmetricMatch('queen')).toBe(true);
160162
jestExpect(stringMatching('en').asymmetricMatch('queue')).toBe(false);
163+
jestExpect(stringMatching('en').asymmetricMatch({})).toBe(false);
161164
});
162165

163166
test('StringMatching throws for non-strings and non-regexps', () => {

packages/expect/src/asymmetric_matchers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ class StringContaining extends AsymmetricMatcher {
193193
}
194194

195195
asymmetricMatch(other: string) {
196+
if (!isA('String', other)) {
197+
return false;
198+
}
199+
196200
return other.includes(this.sample);
197201
}
198202

@@ -218,6 +222,10 @@ class StringMatching extends AsymmetricMatcher {
218222
}
219223

220224
asymmetricMatch(other: string) {
225+
if (!isA('String', other)) {
226+
return false;
227+
}
228+
221229
return this.sample.test(other);
222230
}
223231

0 commit comments

Comments
 (0)