From 3fda960988e1f528514e50bd64c250a6af3cc300 Mon Sep 17 00:00:00 2001 From: Gilles De Mey Date: Mon, 30 Jul 2018 16:51:24 +0200 Subject: [PATCH] adds toBeBefore and toBeAfter matchers (#117) (#137) * adds toBeBefore and toBeAfter matchers (#117) * updates documentation * adds gillesdemey to contributors * encode timezone into date constructors --- .all-contributorsrc | 11 +++++++ README.md | 21 ++++++++++++- .../__snapshots__/index.test.js.snap | 15 ++++++++++ src/matchers/toBeAfter/index.js | 26 ++++++++++++++++ src/matchers/toBeAfter/index.test.js | 30 +++++++++++++++++++ src/matchers/toBeAfter/predicate.js | 5 ++++ src/matchers/toBeAfter/predicate.test.js | 14 +++++++++ .../__snapshots__/index.test.js.snap | 15 ++++++++++ src/matchers/toBeBefore/index.js | 26 ++++++++++++++++ src/matchers/toBeBefore/index.test.js | 30 +++++++++++++++++++ src/matchers/toBeBefore/predicate.js | 5 ++++ src/matchers/toBeBefore/predicate.test.js | 14 +++++++++ types/index.d.ts | 12 ++++++++ 13 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 src/matchers/toBeAfter/__snapshots__/index.test.js.snap create mode 100644 src/matchers/toBeAfter/index.js create mode 100644 src/matchers/toBeAfter/index.test.js create mode 100644 src/matchers/toBeAfter/predicate.js create mode 100644 src/matchers/toBeAfter/predicate.test.js create mode 100644 src/matchers/toBeBefore/__snapshots__/index.test.js.snap create mode 100644 src/matchers/toBeBefore/index.js create mode 100644 src/matchers/toBeBefore/index.test.js create mode 100644 src/matchers/toBeBefore/predicate.js create mode 100644 src/matchers/toBeBefore/predicate.test.js diff --git a/.all-contributorsrc b/.all-contributorsrc index 33cf4492..4f3b6504 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -380,6 +380,17 @@ "code", "doc" ] + }, + { + "login": "gillesdemey", + "name": "Gilles De Mey", + "avatar_url": "https://avatars1.githubusercontent.com/u/868844?v=4", + "profile": "https://demey.io", + "contributions": [ + "code", + "doc", + "test" + ] } ] } diff --git a/README.md b/README.md index 78946485..71b5f476 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin - [Date](#date) - [.toBeDate()](#tobedate) - [.toBeValidDate()](#tobevaliddate) + - [.toBeAfter(date)](#tobeafterdate) + - [.toBeBefore(date)](#tobebeforedate) - Further proposals in [#117](https://github.com/jest-community/jest-extended/issues/117) PRs welcome - [Function](#function) - [.toBeFunction()](#tobefunction) @@ -350,6 +352,23 @@ test('passes when Date is valid', () => { }); ``` +### .toBeAfter(date) + Use `.toBeAfter` when checking if a date occurs after `date`. + ```js +test('passes when input is after date', () => { + expect(new Date('01/01/2019')).toBeAfter(new Date('01/01/2018')); + expect('01/01/2018').not.toBeAfter(new Date('01/01/2019')); +}); +``` + ### .toBeBefore(date) + Use `.toBeBefore` when checking if a date occurs before `date`. + ```js +test('passes when input is before date', () => { + expect(new Date('01/01/2018')).toBeBefore(new Date('01/01/2019')); + expect('01/01/2019').not.toBeBefore(new Date('01/01/2018')); +}); +``` + ### Function #### .toBeFunction() @@ -817,7 +836,7 @@ test('passes when value includes all substrings', () => { | [
mjmiles](https://github.com/mjmiles)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Documentation") | [
Gary Meehan](https://github.com/garmeeh)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Tests") | [
Fredrik MΓ€kilΓ€](https://github.com/GitHug)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Tests") | [
Daniel Reinoso](http://kloc.io/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Tests") | [
Chris Hut](https://github.com/tophernuts)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Tests") | [
Kelvin Ly](https://github.com/cactorium)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Tests") | [
Francis Ngo](https://github.com/francisngo)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Tests") | | [
Amish Shah](https://hydrabolt.me/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Tests") | [
Dave Cooper](http://davecooper.org)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=grug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=grug "Tests") | [
Swann Polydor](https://github.com/soueuls)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Tests") | [
vikneshwar](https://github.com/vikneshwar)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Tests") | [
Budi Irawan](http://budiirawan.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Tests") | [
Tejas Bubane](http://foss-geek.blogspot.com/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Tests") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Documentation") | [
Subinoy Ghosh](https://github.com/subinoy7)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Tests") | | [
Simen Bekkhus](https://github.com/SimenB)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=SimenB "Documentation") | [
Orta](http://orta.io)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=orta "Documentation") | [
Tom](https://jsdevtom.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Documentation") [πŸ’‘](#example-jsdevtom "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Tests") | [
Lucian Buzzo](https://github.com/LucianBuzzo)
| [
Thiago Delgado Pinto](https://github.com/thiagodp)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Documentation") [πŸ’‘](#example-thiagodp "Examples") [πŸ€”](#ideas-thiagodp "Ideas, Planning, & Feedback") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Tests") | [
Ragnar Laud](https://github.com/xprn)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=xprn "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=xprn "Documentation") | [
Luiz AmΓ©rico](https://github.com/blikblum)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=blikblum "Code") | -| [
Frederick Fogerty](https://github.com/frederickfogerty)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [πŸ€”](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [
Benjamin Kay](https://github.com/benjaminkay93)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | +| [
Frederick Fogerty](https://github.com/frederickfogerty)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [πŸ€”](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [
Benjamin Kay](https://github.com/benjaminkay93)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | [
Gilles De Mey](https://demey.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Tests") | ## LICENSE diff --git a/src/matchers/toBeAfter/__snapshots__/index.test.js.snap b/src/matchers/toBeAfter/__snapshots__/index.test.js.snap new file mode 100644 index 00000000..ee674920 --- /dev/null +++ b/src/matchers/toBeAfter/__snapshots__/index.test.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`.not.toBeAfter fails when given a later date 1`] = ` +"expect(received).not.toBeAfter() + +Expected date to be after 2018-06-01T22:00:00.000Z but received: + 2018-06-02T22:00:00.000Z" +`; + +exports[`.toBeAfter fails when given an earlier date 1`] = ` +"expect(received).toBeAfter() + +Expected date to be after 2018-06-02T22:00:00.000Z but received: + 2018-06-01T22:00:00.000Z" +`; diff --git a/src/matchers/toBeAfter/index.js b/src/matchers/toBeAfter/index.js new file mode 100644 index 00000000..11043147 --- /dev/null +++ b/src/matchers/toBeAfter/index.js @@ -0,0 +1,26 @@ +import { matcherHint, printReceived } from 'jest-matcher-utils'; + +import predicate from './predicate'; + +const passMessage = (received, after) => () => + matcherHint('.not.toBeAfter', 'received', '') + + '\n\n' + + `Expected date to be after ${printReceived(after)} but received:\n` + + ` ${printReceived(received)}`; + +const failMessage = (received, after) => () => + matcherHint('.toBeAfter', 'received', '') + + '\n\n' + + `Expected date to be after ${printReceived(after)} but received:\n` + + ` ${printReceived(received)}`; + +export default { + toBeAfter: (date, after) => { + const pass = predicate(date, after); + if (pass) { + return { pass: true, message: passMessage(date, after) }; + } + + return { pass: false, message: failMessage(date, after) }; + } +}; diff --git a/src/matchers/toBeAfter/index.test.js b/src/matchers/toBeAfter/index.test.js new file mode 100644 index 00000000..f5f0adb1 --- /dev/null +++ b/src/matchers/toBeAfter/index.test.js @@ -0,0 +1,30 @@ +import matcher from './'; + +expect.extend(matcher); + +const EARLIER = new Date('2018-06-01T22:00:00.000Z'); +const LATER = new Date('2018-06-02T22:00:00.000Z'); + +describe('.toBeAfter', () => { + test('passes when given a later date', () => { + expect(LATER).toBeAfter(EARLIER); + }); + + test('fails when given an earlier date', () => { + expect(() => { + expect(EARLIER).toBeAfter(LATER); + }).toThrowErrorMatchingSnapshot(); + }); +}); + +describe('.not.toBeAfter', () => { + test('passes when not given an earlier date', () => { + expect(EARLIER).not.toBeAfter(LATER); + }); + + test('fails when given a later date', () => { + expect(() => { + expect(LATER).not.toBeAfter(EARLIER); + }).toThrowErrorMatchingSnapshot(); + }); +}); diff --git a/src/matchers/toBeAfter/predicate.js b/src/matchers/toBeAfter/predicate.js new file mode 100644 index 00000000..66d536f4 --- /dev/null +++ b/src/matchers/toBeAfter/predicate.js @@ -0,0 +1,5 @@ +function toBeAfter(date, after) { + return date > after; +} + +export default (date, after) => toBeAfter(date, after); diff --git a/src/matchers/toBeAfter/predicate.test.js b/src/matchers/toBeAfter/predicate.test.js new file mode 100644 index 00000000..6b2693f6 --- /dev/null +++ b/src/matchers/toBeAfter/predicate.test.js @@ -0,0 +1,14 @@ +import predicate from './predicate'; + +const EARLIER = new Date('2018-06-01T22:00:00.000Z'); +const LATER = new Date('2018-06-02T22:00:00.000Z'); + +describe('toBeAfter Predicate', () => { + test('returns true when given an earlier date', () => { + expect(predicate(LATER, EARLIER)).toBe(true); + }); + + test('returns false when given a later date', () => { + expect(predicate(EARLIER, LATER)).toBe(false); + }); +}); diff --git a/src/matchers/toBeBefore/__snapshots__/index.test.js.snap b/src/matchers/toBeBefore/__snapshots__/index.test.js.snap new file mode 100644 index 00000000..fd94035a --- /dev/null +++ b/src/matchers/toBeBefore/__snapshots__/index.test.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`.not.toBeBefore fails when given an earlier date 1`] = ` +"expect(received).not.toBeBefore() + +Expected date to be before 2018-06-02T22:00:00.000Z but received: + 2018-06-01T22:00:00.000Z" +`; + +exports[`.toBeBefore fails when given a later date 1`] = ` +"expect(received).toBeBefore() + +Expected date to be before 2018-06-01T22:00:00.000Z but received: + 2018-06-02T22:00:00.000Z" +`; diff --git a/src/matchers/toBeBefore/index.js b/src/matchers/toBeBefore/index.js new file mode 100644 index 00000000..4ecf680e --- /dev/null +++ b/src/matchers/toBeBefore/index.js @@ -0,0 +1,26 @@ +import { matcherHint, printReceived } from 'jest-matcher-utils'; + +import predicate from './predicate'; + +const passMessage = (received, before) => () => + matcherHint('.not.toBeBefore', 'received', '') + + '\n\n' + + `Expected date to be before ${printReceived(before)} but received:\n` + + ` ${printReceived(received)}`; + +const failMessage = (received, before) => () => + matcherHint('.toBeBefore', 'received', '') + + '\n\n' + + `Expected date to be before ${printReceived(before)} but received:\n` + + ` ${printReceived(received)}`; + +export default { + toBeBefore: (date, before) => { + const pass = predicate(date, before); + if (pass) { + return { pass: true, message: passMessage(date, before) }; + } + + return { pass: false, message: failMessage(date, before) }; + } +}; diff --git a/src/matchers/toBeBefore/index.test.js b/src/matchers/toBeBefore/index.test.js new file mode 100644 index 00000000..244880f9 --- /dev/null +++ b/src/matchers/toBeBefore/index.test.js @@ -0,0 +1,30 @@ +import matcher from './'; + +expect.extend(matcher); + +const EARLIER = new Date('2018-06-01T22:00:00.000Z'); +const LATER = new Date('2018-06-02T22:00:00.000Z'); + +describe('.toBeBefore', () => { + test('passes when given an earlier date', () => { + expect(EARLIER).toBeBefore(LATER); + }); + + test('fails when given a later date', () => { + expect(() => { + expect(LATER).toBeBefore(EARLIER); + }).toThrowErrorMatchingSnapshot(); + }); +}); + +describe('.not.toBeBefore', () => { + test('passes when given an earlier date', () => { + expect(LATER).not.toBeBefore(EARLIER); + }); + + test('fails when given an earlier date', () => { + expect(() => { + expect(EARLIER).not.toBeBefore(LATER); + }).toThrowErrorMatchingSnapshot(); + }); +}); diff --git a/src/matchers/toBeBefore/predicate.js b/src/matchers/toBeBefore/predicate.js new file mode 100644 index 00000000..dbe8a86c --- /dev/null +++ b/src/matchers/toBeBefore/predicate.js @@ -0,0 +1,5 @@ +function toBeBefore(date, before) { + return date < before; +} + +export default (date, before) => toBeBefore(date, before); diff --git a/src/matchers/toBeBefore/predicate.test.js b/src/matchers/toBeBefore/predicate.test.js new file mode 100644 index 00000000..6b89bd7a --- /dev/null +++ b/src/matchers/toBeBefore/predicate.test.js @@ -0,0 +1,14 @@ +import predicate from './predicate'; + +const EARLIER = new Date('06/02/2018'); +const LATER = new Date('07/02/2018'); + +describe('toBeAfter Predicate', () => { + test('returns true when given a later date', () => { + expect(predicate(EARLIER, LATER)).toBe(true); + }); + + test('returns false when given an earlier date', () => { + expect(predicate(LATER, EARLIER)).toBe(false); + }); +}); diff --git a/types/index.d.ts b/types/index.d.ts index 0e3323ab..651aa90f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -52,6 +52,18 @@ declare namespace jest { */ toBeArrayOfSize(x: number): R; + /** + * Use `.toBeAfter` when checking if a date occurs after `date`. + * @param {Date} date + */ + toBeAfter(date: Date): R; + + /** + * Use `.toBeBefore` when checking if a date occurs before `date`. + * @param {Date} date + */ + toBeBefore(date: Date): R; + /** * Use `.toIncludeAllMembers` when checking if an `Array` contains all of the same members of a given set. * @param {Array.<*>} members