chai-moment-esm
v1.2.0
Published
Date/time comparisons in Chai with MomentJS
Downloads
1
Readme
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:
expect( moment( '2016-12-31' ).isSame( '2017-01-01' ) ).to.be.true;
// => "expected false to be true"
Do this instead:
expect( moment('2016-12-31') ).is.same.moment( '2017-01-01' );
// => "expected 2016-12-31 00:00:00 to be 2017-01-01 00:00:00"
Usage
Include the plugin as normal:
import { use, expect } from 'chai'
import { chaiMoment } from 'chai-moment-esm'
use(chaiMoment)
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 = moment( 1487070166000 );
let m1 = moment( 1487070166773 );
expect( m1 ).is.same.moment( m0 ); // => false
expect( m1 ).is.same.moment( m0, 'second' ); // => 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 = moment( 1487070166000 );
let m1 = moment( 1487070166500 );
let m2 = moment( 1487070166773 );
expect( m1 ).is.betweenMoments( m0, m2 ); // => true
expect( m1 ).is.betweenMoments( m0, m2, 'second' ); // => false
expect( m1 ).is.betweenMoments( m0, m2, 'second', '[]' ); // => true
expect( m0 ).is.betweenMoments( m0, m2 ); // => false
expect( m0 ).is.betweenMoments( m0, m2, null, '[)' ); // => true
expect( m0 ).is.betweenMoments( m0, m2, null, '(]' ); // => false
expect( m2 ).is.betweenMoments( m0, m2, null, '[)' ); // => false
expect( m2 ).is.betweenMoments( m0, m2, null, '(]' ); // => 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 = moment( 1487070166000 );
let m1 = moment( 1487070166773 );
expect( m0 ).is.before.moment( m1 ); // => true
expect( m0 ).is.before.moment( m1, 'second' ); // => false
after
The after flag tells Chai MomentJS to use MomentJS's isAfter
query function.
let m0 = moment( 1487070166000 );
let m1 = moment( 1487070166773 );
expect( m1 ).is.after.moment( m0 ); // => true
expect( m1 ).is.after.moment( m0, 'second' ); // => false
sameOrBefore
The sameOrBefore flag tells Chai MomentJS to use MomentJS's isSameOrBefore
query function.
let m0 = moment( 1487070166000 );
let m1 = moment( 1487070166773 );
expect( m0 ).is.sameOrBefore.moment( m1 ); // => true
expect( m0 ).is.sameOrBefore.moment( m1, 'second' ); // => true
sameOrAfter
The sameOrAfter flag tells Chai MomentJS to use MomentJS's isSameOrAfter
query function.
let m0 = moment( 1487070166000 );
let m1 = moment( 1487070166773 );
expect( m1 ).is.sameOrAfter.moment( m0 ); // => true
expect( m1 ).is.sameOrAfter.moment( m0, 'second' ); // => 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.