TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

chai-moment-esm - Chai MomentTS

This is copy of 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' ) );
// => "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"


Include the plugin as normal:

import { use, expect } from 'chai'
import { chaiMoment } from 'chai-moment-esm'

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


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.


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


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


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


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 to:


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




Package Sidebar


npm i chai-moment-esm

Weekly Downloads






Unpacked Size

25.9 kB

Total Files


Last publish


  • tkhduracell