Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type checks for asymmetric matchers #5069

Merged
merged 3 commits into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

According to our docs, stringMatching() accepts a regexp. Mind changing the type and runtime check?

Copy link
Member

Choose a reason for hiding this comment

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

cc @rogeliog can you take a look at this?

Copy link
Contributor

Choose a reason for hiding this comment

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

@thymikee The StringMatching constructor does indeed take either string or RegExp, but argument of the asymmetricMatch function takes only string as received value, true?

That is, expect.stringMatching(/^Alic/) returns true when the received value is 'Alicia' or false when the received value is 'Bob' but should report an error if the received value is a RegExp, because how to decide if a received RegExp matches an expected RegExp?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@pedrottimark you're right, I mistaken other with sample

return false;
}

return this.sample.test(other);
}

Expand Down