diff --git a/src/matchers/index.js b/src/matchers/index.js index 4c607c3b..88d9fa2b 100644 --- a/src/matchers/index.js +++ b/src/matchers/index.js @@ -68,3 +68,4 @@ export { toStartWith } from './toStartWith'; export { toThrowWithMessage } from './toThrowWithMessage'; export { toEqualIgnoringWhitespace } from './toEqualIgnoringWhitespace'; export { toPartiallyContain } from './toPartiallyContain'; +export { toBeInRange } from './toBeInRange'; diff --git a/src/matchers/toBeInRange.js b/src/matchers/toBeInRange.js new file mode 100644 index 00000000..53b92bb9 --- /dev/null +++ b/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) }; +} diff --git a/test/matchers/__snapshots__/toBeInRange.test.js.snap b/test/matchers/__snapshots__/toBeInRange.test.js.snap new file mode 100644 index 00000000..c04eec1a --- /dev/null +++ b/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`] = ` +"expect(received).not.toBeInRange(expected) + +Expected Array to not be in range 4, 10 +Received: + [4, 5, 7, 9]" +`; + +exports[`.toBeInRange fails when given array is not in a given range 1`] = ` +"expect(received).toBeInRange(expected) + +Expected: Array elements to be in range (4, 8) +Received: Array element out of range 9" +`; diff --git a/test/matchers/toBeInRange.test.js b/test/matchers/toBeInRange.test.js new file mode 100644 index 00000000..7743cdea --- /dev/null +++ b/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(); + }); +}); diff --git a/types/index.d.ts b/types/index.d.ts index 25a14dcc..33b6e9f5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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. */ @@ -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`. */ diff --git a/website/docs/matchers/Array.mdx b/website/docs/matchers/Array.mdx index 23c5be71..7903a5e6 100644 --- a/website/docs/matchers/Array.mdx +++ b/website/docs/matchers/Array.mdx @@ -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); });`} + +### .toBeInRange(min, max) + +Use `.toBeInRange` when checking if an array has elements in range min (inclusive) and max (inclusive). + + + {`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); +});`} + diff --git a/website/docs/matchers/index.md b/website/docs/matchers/index.md index 7d3abb02..f44e50eb 100644 --- a/website/docs/matchers/index.md +++ b/website/docs/matchers/index.md @@ -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)