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
expect: Add matcher toBeInRange()
to validate if array elements are within the specified range
#461
Merged
keeganwitt
merged 21 commits into
jest-community:main
from
mayankshukla94:ft-add-toBeWithinRange-matcher
Oct 20, 2022
Merged
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
75011ff
Add matcher export
mayankshukla94 c6c9623
Add toBeWithinRange function
mayankshukla94 43b0235
Add unit test for toBeWithinRange
mayankshukla94 037287c
Add snapshot
mayankshukla94 da4245c
define type declaration
mayankshukla94 4c166d8
Update documentation
mayankshukla94 9e618be
Update README.md to document both the numbers are inclusive in .toBeW…
mayankshukla94 200cd6c
Replace ampersand with "and"
keeganwitt 3e7907c
Merge branch 'jest-community:main' into ft-add-toBeWithinRange-matcher
mayankshukla94 81e7ea9
Update snapshot
mayankshukla94 cfaa81d
Replace any with Result
mayankshukla94 08c4b16
Make max exclusive
mayankshukla94 c08fcc1
rename toBeWithinRange to toBeInRange
mayankshukla94 cb29c4f
Merge branch 'main' into ft-add-toBeWithinRange-matcher
mayankshukla94 ea0ca08
Update README.md
mayankshukla94 f6640ea
Merge branch 'jest-community:main' into ft-add-toBeWithinRange-matcher
mayankshukla94 783c9f1
add toBeInRange() matcher in the matchers interface
mayankshukla94 5b70235
add toBeInRange() matcher
mayankshukla94 77c0849
add TestFile for toBeInRange()
mayankshukla94 6179002
Merge branch 'ft-add-toBeWithinRange-matcher' of https://github.com/m…
mayankshukla94 2d7adc5
update description
mayankshukla94 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export function toBeInRange(actual, min, max) { | ||
const { printReceived, printExpected, matcherHint } = this.utils; | ||
|
||
const element = actual.find(option => option < min || option >= max); | ||
|
||
const pass = element === undefined; | ||
|
||
const passMessage = | ||
matcherHint('.not.toBeInRange') + | ||
'\n\n' + | ||
`Expected Array to not be in range ${printExpected(min)}, ${printExpected(max)}\n` + | ||
'Received:\n' + | ||
` ${printReceived(actual)}`; | ||
|
||
const failMessage = | ||
matcherHint('.toBeInRange') + | ||
'\n\n' + | ||
`Expected: Array elements to be in range (${printExpected(min)}, ${printExpected(max)})\n` + | ||
`Received: Array element out of range ${printReceived(element)}`; | ||
|
||
return { pass, message: () => (pass ? passMessage : failMessage) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.not.toBeInRange fails when given array is in the given range 1`] = ` | ||
"<dim>expect(</intensity><red>received</color><dim>).not.toBeInRange(</intensity><green>expected</color><dim>)</intensity> | ||
|
||
Expected Array to not be in range <green>4</color>, <green>10</color> | ||
Received: | ||
<red>[4, 5, 7, 9]</color>" | ||
`; | ||
|
||
exports[`.toBeInRange fails when given array is not in a given range 1`] = ` | ||
"<dim>expect(</intensity><red>received</color><dim>).toBeInRange(</intensity><green>expected</color><dim>)</intensity> | ||
|
||
Expected: Array elements to be in range (<green>4</color>, <green>8</color>) | ||
Received: Array element out of range <red>9</color>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as matcher from 'src/matchers/toBeInRange'; | ||
|
||
expect.extend(matcher); | ||
|
||
describe('.toBeInRange', () => { | ||
test('passes when given array is in range', () => { | ||
expect([4, 5, 7, 9]).toBeInRange(4, 10); | ||
}); | ||
|
||
test('fails when given array is not in a given range', () => { | ||
expect(() => expect([4, 5, 7, 9]).toBeInRange(4, 8)).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('.not.toBeInRange', () => { | ||
test('passes when given array is not in the given range', () => { | ||
expect([12, 13, 15, 17]).not.toBeInRange(4, 9); | ||
}); | ||
|
||
test('fails when given array is in the given range', () => { | ||
expect(() => expect([4, 5, 7, 9]).not.toBeInRange(4, 10)).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd mention that min is exclusive, and max is inclusive here. Other than that, it looks good.
@mattphillips were you happy with this new name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if @mattphillips has been notified because this comment is edited.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An example of how to word the inclusive/exclusive behavior is the toBeWithin documentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keeganwitt I have updated the description now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keeganwitt I'm not able to request a review from @mattphillips. Could you please help here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to wait another day-ish, and if he doesn't respond, I'll just merge it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keeganwitt Could you please merge this PR to main branch if all looks good to you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea. We can always rename before cutting the next release if there's a better name that we should have used.