Code Monkey home page Code Monkey logo

chai-moment's Introduction

chai-moment

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');

Thanks

Thanks to chai-fuzzy for the project structure.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.