chai-moment-esm - Chai MomentTS
This is copy of https://github.com/alexgs/chai-moment-js but updated for ESM.
Chai MomentJS is a plugin for the Chai assertion library that provides date/time comparisons. It is a wrapper for some of the query functions in the MomentJS date library. Use Chai MomentTS to write fluent BDD/TDD tests and get useful error messages.
In other words, don't do this:
tobetrue;// => "expected false to be true"
Do this instead:
issame;// => "expected 2016-12-31 00:00:00 to be 2017-01-01 00:00:00"
Usage
Include the plugin as normal:
Test Methods
Chai MomentTS provides two methods that can used to compare dates: moment
and betweenMoments
.
moment( date, [accuracy] )
In the default usage, moment
is a wrapper for the MomentJS's isSame
query function. (See "Flags" below for how to change behavior from the default.) It has one required argument, date
, which can be either a a native JavaScript Date object or a Moment object from MomentJS. The optional argument, accuracy
, specifies the accuracy of the comparison. You can use any of the vales recognized by MomentJS's startOf
function, but the most common ones are:
- second
- minute
- hour
- day
- month
- year
You can use them like this:
let m0 = ;let m1 = ;issame; // => falseissame; // => true
betweenMoments( start, end, [accuracy], [inclusivity] )
This is a wrapper for MomentJS's isBetween
query function. It requires start
and end
arguments, which may be either a Date or a Moment. The accuracy
parameters functions as in the moment
function; it will accept null
for millisecond accuracy.
Finally, the inclusivity
parameter determines whether to return true or false if the object-under-test matches the start
or end
argument. Basically, a parenthesis excludes an exact match (returns false) while a square bracket includes an exact match (returns true). The default is to exclude on exact matches.
The following table explains inclusivity in more concrete terms:
argument | result of exact match on start |
result of exact match on end |
---|---|---|
'()' | false |
false |
'[]' | true |
true |
'(]' | false |
true |
'[)' | true |
false |
The meaning of "exact match" is determined by the accuracy
parameter.
Some examples:
let m0 = ;let m1 = ;let m2 = ;is; // => trueis; // => falseis; // => trueis; // => falseis; // => trueis; // => falseis; // => falseis; // => true
Flags
These flags change the behavior of the moment
comparison function. This allows you to write fluent TDD/BDD statements like expect( fileDate ).is.before.moment( myDate )
.
Don't combine flags. That's bad, like crossing-the-streams bad.
before
The before flag tells Chai MomentJS to use MomentJS's isBefore
query function.
let m0 = ;let m1 = ;isbefore; // => trueisbefore; // => false
after
The after flag tells Chai MomentJS to use MomentJS's isAfter
query function.
let m0 = ;let m1 = ;isafter; // => trueisafter; // => false
sameOrBefore
The sameOrBefore flag tells Chai MomentJS to use MomentJS's isSameOrBefore
query function.
let m0 = ;let m1 = ;issameOrBefore; // => trueissameOrBefore; // => true
sameOrAfter
The sameOrAfter flag tells Chai MomentJS to use MomentJS's isSameOrAfter
query function.
let m0 = ;let m1 = ;issameOrAfter; // => trueissameOrAfter; // => true
Thanks
Thanks to:
- @alexgs for chai-moment-js, which this is copy of.
License
The content of this repository is licensed under the 3-Clause BSD license. Please see the enclosed license file for specific terms.