chai-moment-js

Date/time comparisons in Chai with MomentJS

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
chai-moment-js
431.0.06 years ago6 years agoMinified + gzip package size for chai-moment-js in KB

Readme

Chai MomentJS
Chai MomentJS is a plugin for the Chai1 assertion library that provides date/time comparisons. It is a wrapper for some of the query functions in the MomentJS2 date library. Use Chai MomentJS 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:
let chai = require( 'chai' );
let chaiMoment = require( 'chai-moment-js' );

chai.use( chaiMoment );
let expect = chai.expect;

Test Methods

Chai MomentJS 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 isSame3 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 startOf4 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 isBetween5 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
isBefore6 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 isAfter7 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 isSameOrBefore8 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 isSameOrAfter9 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:
  • @mguterl for chai-datetime3, which inspired this plugin.
  • @fastfrwrd for chai-moment10, which I didn't know about until I got a name collision upon running npm publish!

License

The content of this repository is licensed under the 3-Clause BSD license4. Please see the enclosed license file5 for specific terms.