Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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) && !isA('RegExp', other)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although sample can be either string or regexp, it seems like other can only be a string.

return false;
}

return this.sample.test(other);
}

Expand Down