Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expect: Add matcher
toBeInRange()
to validate if array elements are…
… within the specified range (#461) * Add matcher export * Add toBeWithinRange function * Add unit test for toBeWithinRange * Add snapshot * define type declaration * Update documentation * Update README.md to document both the numbers are inclusive in .toBeWithinRange * Replace ampersand with "and" * Update snapshot * Replace any with Result * Make max exclusive * rename toBeWithinRange to toBeInRange * Update README.md * add toBeInRange() matcher in the matchers interface * add toBeInRange() matcher * add TestFile for toBeInRange() * update description Co-authored-by: Keegan Witt <keeganwitt@gmail.com>
- Loading branch information
1 parent
bd824cd
commit 5e85fb0
Showing
7 changed files
with
90 additions
and
0 deletions.
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