Skip to content

Commit

Permalink
adds toBeBefore and toBeAfter matchers (#117) (#137)
Browse files Browse the repository at this point in the history
* adds toBeBefore and toBeAfter matchers (#117)

* updates documentation

* adds gillesdemey to contributors

* encode timezone into date constructors
  • Loading branch information
gillesdemey authored and mattphillips committed Jul 30, 2018
1 parent 7a4ead0 commit 3fda960
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
]
}
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -817,7 +836,7 @@ test('passes when value includes all substrings', () => {
| [<img src="https://avatars2.githubusercontent.com/u/33098064?v=4" width="100px;"/><br /><sub>mjmiles</sub>](https://github.com/mjmiles)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/13333582?v=4" width="100px;"/><br /><sub>Gary Meehan</sub>](https://github.com/garmeeh)<br />[💻](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") | [<img src="https://avatars2.githubusercontent.com/u/3191489?v=4" width="100px;"/><br /><sub>Fredrik Mäkilä</sub>](https://github.com/GitHug)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Tests") | [<img src="https://avatars2.githubusercontent.com/u/9046616?v=4" width="100px;"/><br /><sub>Daniel Reinoso</sub>](http://kloc.io/)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Tests") | [<img src="https://avatars1.githubusercontent.com/u/4359781?v=4" width="100px;"/><br /><sub>Chris Hut</sub>](https://github.com/tophernuts)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Tests") | [<img src="https://avatars2.githubusercontent.com/u/1513183?v=4" width="100px;"/><br /><sub>Kelvin Ly</sub>](https://github.com/cactorium)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Tests") | [<img src="https://avatars0.githubusercontent.com/u/11182826?v=4" width="100px;"/><br /><sub>Francis Ngo</sub>](https://github.com/francisngo)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Tests") |
| [<img src="https://avatars1.githubusercontent.com/u/10330923?v=4" width="100px;"/><br /><sub>Amish Shah</sub>](https://hydrabolt.me/)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Tests") | [<img src="https://avatars3.githubusercontent.com/u/2045206?v=4" width="100px;"/><br /><sub>Dave Cooper</sub>](http://davecooper.org)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=grug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=grug "Tests") | [<img src="https://avatars3.githubusercontent.com/u/3630495?v=4" width="100px;"/><br /><sub>Swann Polydor</sub>](https://github.com/soueuls)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2027003?v=4" width="100px;"/><br /><sub>vikneshwar</sub>](https://github.com/vikneshwar)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1243921?v=4" width="100px;"/><br /><sub>Budi Irawan</sub>](http://budiirawan.com)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Tests") | [<img src="https://avatars2.githubusercontent.com/u/980783?v=4" width="100px;"/><br /><sub>Tejas Bubane</sub>](http://foss-geek.blogspot.com/)<br />[💻](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") | [<img src="https://avatars2.githubusercontent.com/u/13134653?v=4" width="100px;"/><br /><sub>Subinoy Ghosh</sub>](https://github.com/subinoy7)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Tests") |
| [<img src="https://avatars1.githubusercontent.com/u/1404810?v=4" width="100px;"/><br /><sub>Simen Bekkhus</sub>](https://github.com/SimenB)<br />[📖](https://github.com/mattphillips/jest-extended/commits?author=SimenB "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/49038?v=4" width="100px;"/><br /><sub>Orta</sub>](http://orta.io)<br />[📖](https://github.com/mattphillips/jest-extended/commits?author=orta "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/17221813?v=4" width="100px;"/><br /><sub>Tom</sub>](https://jsdevtom.com)<br />[💻](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") | [<img src="https://avatars0.githubusercontent.com/u/15064535?v=4" width="100px;"/><br /><sub>Lucian Buzzo</sub>](https://github.com/LucianBuzzo)<br /> | [<img src="https://avatars3.githubusercontent.com/u/2997844?v=4" width="100px;"/><br /><sub>Thiago Delgado Pinto</sub>](https://github.com/thiagodp)<br />[💻](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") | [<img src="https://avatars0.githubusercontent.com/u/3042904?v=4" width="100px;"/><br /><sub>Ragnar Laud</sub>](https://github.com/xprn)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=xprn "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=xprn "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/3047126?v=4" width="100px;"/><br /><sub>Luiz Américo</sub>](https://github.com/blikblum)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=blikblum "Code") |
| [<img src="https://avatars0.githubusercontent.com/u/615334?v=4" width="100px;"/><br /><sub>Frederick Fogerty</sub>](https://github.com/frederickfogerty)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [🤔](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [<img src="https://avatars1.githubusercontent.com/u/10714808?v=4" width="100px;"/><br /><sub>Benjamin Kay</sub>](https://github.com/benjaminkay93)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") |
| [<img src="https://avatars0.githubusercontent.com/u/615334?v=4" width="100px;"/><br /><sub>Frederick Fogerty</sub>](https://github.com/frederickfogerty)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [🤔](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [<img src="https://avatars1.githubusercontent.com/u/10714808?v=4" width="100px;"/><br /><sub>Benjamin Kay</sub>](https://github.com/benjaminkay93)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/868844?v=4" width="100px;"/><br /><sub>Gilles De Mey</sub>](https://demey.io)<br />[💻](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") |
<!-- ALL-CONTRIBUTORS-LIST:END -->
## LICENSE
Expand Down
15 changes: 15 additions & 0 deletions src/matchers/toBeAfter/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toBeAfter fails when given a later date 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeAfter(</><dim>)</>
Expected date to be after <red>2018-06-01T22:00:00.000Z</> but received:
<red>2018-06-02T22:00:00.000Z</>"
`;
exports[`.toBeAfter fails when given an earlier date 1`] = `
"<dim>expect(</><red>received</><dim>).toBeAfter(</><dim>)</>
Expected date to be after <red>2018-06-02T22:00:00.000Z</> but received:
<red>2018-06-01T22:00:00.000Z</>"
`;
26 changes: 26 additions & 0 deletions src/matchers/toBeAfter/index.js
Original file line number Diff line number Diff line change
@@ -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) };
}
};
30 changes: 30 additions & 0 deletions src/matchers/toBeAfter/index.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
5 changes: 5 additions & 0 deletions src/matchers/toBeAfter/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function toBeAfter(date, after) {
return date > after;
}

export default (date, after) => toBeAfter(date, after);
14 changes: 14 additions & 0 deletions src/matchers/toBeAfter/predicate.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
15 changes: 15 additions & 0 deletions src/matchers/toBeBefore/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toBeBefore fails when given an earlier date 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeBefore(</><dim>)</>
Expected date to be before <red>2018-06-02T22:00:00.000Z</> but received:
<red>2018-06-01T22:00:00.000Z</>"
`;
exports[`.toBeBefore fails when given a later date 1`] = `
"<dim>expect(</><red>received</><dim>).toBeBefore(</><dim>)</>
Expected date to be before <red>2018-06-01T22:00:00.000Z</> but received:
<red>2018-06-02T22:00:00.000Z</>"
`;
26 changes: 26 additions & 0 deletions src/matchers/toBeBefore/index.js
Original file line number Diff line number Diff line change
@@ -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) };
}
};
30 changes: 30 additions & 0 deletions src/matchers/toBeBefore/index.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
5 changes: 5 additions & 0 deletions src/matchers/toBeBefore/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function toBeBefore(date, before) {
return date < before;
}

export default (date, before) => toBeBefore(date, before);
14 changes: 14 additions & 0 deletions src/matchers/toBeBefore/predicate.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
12 changes: 12 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3fda960

Please sign in to comment.