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

v0.1.0

Published

date assertions for chai, powered by moment

Downloads

31,450

Readme

chai-moment Build Status

Matchers for dates and formatted date strings powered by moment.js

Using

Also see the tests and examples.

browser-side

include chai moment after chai and moment:

<script src="moment.js"></script>
<script src="chai.js"></script>
<script src="chai-moment.js"></script>

server-side

have chai use chai-moment:


var chai = require('chai');
chai.use(require('chai-moment'));

Assertions

Compare any moment-parseable date/string/whatever with another, with optional granularity. See Moment.js docs on parsing.

When using granularity, please use one of the following: year, month, week, day, hour, minute, second. When using tdd-style assertions, if you do not use one of the listed granularities, the argument will be interpreted as a custom error message.

sameMoment


var dateString = '2016-04-21',
    date = new Date(2016, 3, 21),
    milliseconds = 1461222000000,  // assumes system has PDT timezone
    obj = { y: 2016, M: 3, d: 21 },
    arr = [2016, 3, 21],
    momentObj = moment('2016-04-21'),
    oneDayLater = '2016-04-22';

// using should-style assertions
dateString.should.be.sameMoment(date);
dateString.should.be.sameMoment(oneDayLater, 'month');

// using expect-style assertions
expect(milliseconds).to.be.sameMoment(obj);
expect(dateString).to.be.sameMoment(oneDayLater, 'month');

// using tdd assertions
assert.sameMoment(arr, momentObj);
assert.sameMoment(arr, oneDayLater, 'month');
assert.sameMoment(arr, oneDayLater, 'month', 'custom error message');
assert.sameMoment(arr, oneDayLater, 'custom error message');  // fails

beforeMoment


var dateString = '2016-04-21',
    oneDayLater = '2016-04-22';

// using should-style assertions
dateString.should.be.beforeMoment(oneDayLater);
dateString.should.be.beforeMoment(oneDayLater, 'month');  // fails

// using expect-style assertions
expect(dateString).to.be.beforeMoment(oneDayLater);
expect(dateString).to.be.beforeMoment(oneDayLater, 'month');  // fails

// using tdd assertions
assert.beforeMoment(dateString, oneDayLater);
assert.beforeMoment(dateString, oneDayLater, 'month');  // fails
assert.beforeMoment(dateString, oneDayLater, 'month', 'custom error message');  // fails
assert.beforeMoment(dateString, oneDayLater, 'custom error message');

afterMoment


var dateString = '2016-04-21',
    oneDayLater = '2016-04-22';

// using should-style assertions
oneDayLater.should.be.afterMoment(dateString);
oneDayLater.should.be.afterMoment(dateString, 'month');  // fails

// using expect-style assertions
expect(oneDayLater).to.be.afterMoment(dateString);
expect(oneDayLater).to.be.afterMoment(dateString, 'month');  // fails

// using tdd assertions
assert.afterMoment(oneDayLater, dateString);
assert.afterMoment(oneDayLater, dateString, 'month');  // fails
assert.afterMoment(oneDayLater, dateString, 'month', 'custom error message');  // fails
assert.afterMoment(oneDayLater, dateString, 'custom error message');

Configuration

setErrorFormat(format)

Sets the format used for reporting moments in failed assertions.


var chaiMoment = require('chai-moment');

chaiMoment.setErrorFormat('L');

expect(moment('2016-04-21')).to.be.beforeMoment(moment('2016-04-22'));
// Error('expected 04/21/2017 to be before 04/22/2016')

Thanks

Thanks to chai-fuzzy for the project structure.