npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chai-moment-js

v1.0.0

Published

Date/time comparisons in Chai with MomentJS

Downloads

202

Readme

Chai MomentJS

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

  • @mguterl for chai-datetime, which inspired this plugin.
  • @fastfrwrd for chai-moment, 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 license. Please see the enclosed license file for specific terms.