Matchers for dates and formatted date strings powered by moment.js
Using
Also see the tests and examples.
browser-side
include chai moment after chai and moment:
<script src="moment.js"></script>
<script src="chai.js"></script>
<script src="chai-moment.js"></script>
server-side
have chai use chai-moment:
var chai = ;chai;
Assertions
Compare any moment-parseable date/string/whatever with another, with optional granularity. See Moment.js docs on parsing.
When using granularity, please use one of the following: year
, month
, week
, day
, hour
, minute
, second
. When using tdd-style assertions, if you do not use one of the listed granularities, the argument will be interpreted as a custom error message.
sameMoment
var dateString = '2016-04-21' date = 2016 3 21 milliseconds = 1461222000000 // assumes system has PDT timezone obj = y: 2016 M: 3 d: 21 arr = 2016 3 21 momentObj = oneDayLater = '2016-04-22'; // using should-style assertionsdateStringshouldbe;dateStringshouldbe; // using expect-style assertionstobe;tobe; // using tdd assertionsassert;assert;assert;assert; // fails
beforeMoment
var dateString = '2016-04-21' oneDayLater = '2016-04-22'; // using should-style assertionsdateStringshouldbe;dateStringshouldbe; // fails // using expect-style assertionstobe;tobe; // fails // using tdd assertionsassert;assert; // failsassert; // failsassert;
afterMoment
var dateString = '2016-04-21' oneDayLater = '2016-04-22'; // using should-style assertionsoneDayLatershouldbe;oneDayLatershouldbe; // fails // using expect-style assertionstobe;tobe; // fails // using tdd assertionsassert;assert; // failsassert; // failsassert;
Configuration
setErrorFormat(format)
Sets the format used for reporting moments in failed assertions.
var chaiMoment = ; chaiMoment; tobe;// Error('expected 04/21/2017 to be before 04/22/2016')
Thanks
Thanks to chai-fuzzy for the project structure.