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

expect: Add matcher toBeInRange() to validate if array elements are within the specified range #461

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/matchers/index.js
Expand Up @@ -68,3 +68,4 @@ export { toStartWith } from './toStartWith';
export { toThrowWithMessage } from './toThrowWithMessage';
export { toEqualIgnoringWhitespace } from './toEqualIgnoringWhitespace';
export { toPartiallyContain } from './toPartiallyContain';
export { toBeInRange } from './toBeInRange';
22 changes: 22 additions & 0 deletions src/matchers/toBeInRange.js
@@ -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) };
}
16 changes: 16 additions & 0 deletions test/matchers/__snapshots__/toBeInRange.test.js.snap
@@ -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>"
`;
23 changes: 23 additions & 0 deletions test/matchers/toBeInRange.test.js
@@ -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();
});
});
16 changes: 16 additions & 0 deletions types/index.d.ts
Expand Up @@ -219,6 +219,14 @@ declare namespace jest {
*/
toBeWithin(start: number, end: number): R;

/**
* Use `.toBeInRange` when checking if an array has elements in range min (inclusive) and max (inclusive).
*
* @param min
* @param max
*/
toBeInRange(min: number, max: number): R;

/**
* Use `.toBeInteger` when checking if a value is an integer.
*/
Expand Down Expand Up @@ -649,6 +657,14 @@ declare namespace jest {
*/
toBeWithin(start: number, end: number): any;

/**
* Use `.toBeInRange` when checking if an array has elements in range min (inclusive) and max (inclusive).
*
* @param min
* @param max
*/
toBeInRange(min: number, max: number): any;

/**
* Use `.toBeObject` when checking if a value is an `Object`.
*/
Expand Down
11 changes: 11 additions & 0 deletions website/docs/matchers/Array.mdx
Expand Up @@ -105,3 +105,14 @@ Use `.toSatisfyAny` when you want to use a custom matcher by supplying a predica
expect([2, 4, 8, 12]).not.toSatisfyAny(isOdd);
});`}
</TestFile>

### .toBeInRange(min, max)

Use `.toBeInRange` when checking if an array has elements in range min (inclusive) and max (inclusive).

<TestFile name="toBeInRange">
{`test('passes when given array is in range', () => {
expect([4, 5, 7, 9]).toBeInRange(4, 10);
expect([12, 13, 15, 17]).not.toBeInRange(4, 9);
});`}
</TestFile>
1 change: 1 addition & 0 deletions website/docs/matchers/index.md
Expand Up @@ -22,6 +22,7 @@ sidebar_position: 1
- [.toPartiallyContain(member)](/docs/matchers/array/#topartiallycontainmember)
- [.toSatisfyAll(predicate)](/docs/matchers/array/#tosatisfyallpredicate)
- [.toSatisfyAny(predicate)](/docs/matchers/array/#tosatisfyanypredicate)
- [.toBeInRange(min, max)](/docs/matchers/array/#tobeinrangemin-max)

## [Boolean](/docs/matchers/boolean)

Expand Down