diff --git a/index.js b/index.js index 032ba74..601459a 100644 --- a/index.js +++ b/index.js @@ -68,17 +68,26 @@ function _chaiMoment(chai, utils){ }); assert.sameMoment = function (val, exp, granularity, msg) { - if(!allowedGranularitiesLookup[granularity]) msg = granularity; + if(!allowedGranularitiesLookup[granularity]) { + msg = granularity; + granularity = undefined; + } new chai.Assertion(val, msg).to.be.sameMoment(exp, granularity); }; assert.beforeMoment = function (val, exp, granularity, msg) { - if(!allowedGranularitiesLookup[granularity]) msg = granularity; + if(!allowedGranularitiesLookup[granularity]) { + msg = granularity; + granularity = undefined; + } new chai.Assertion(val, msg).to.be.beforeMoment(exp, granularity); }; assert.afterMoment = function (val, exp, granularity, msg) { - if(!allowedGranularitiesLookup[granularity]) msg = granularity; + if(!allowedGranularitiesLookup[granularity]) { + msg = granularity; + granularity = undefined; + } new chai.Assertion(val, msg).to.be.afterMoment(exp, granularity); }; } diff --git a/test/index.test.js b/test/index.test.js index 88c4c96..9a29daf 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -3,7 +3,7 @@ var chaiMoment = require('../index.js'); chai.use(chaiMoment); -chaiMoment.setErrorFormat('LLLL'); +chaiMoment.setErrorFormat('YYYYMMDDHHmmss'); var moment = require('moment'); @@ -78,10 +78,16 @@ describe('sameMoment', function() { assert.sameMoment(date, oneDayLater, 'month'); }); + it('tdd-style with error message', function() { + assert.throws(function() { + assert.sameMoment(date, oneDayLater, 'day', 'not the same day'); + }, /^not the same day: expected \d{14} to be the same as \d{14} \(granularity: day\)$/); + }); + it('tdd-style with error message in place of granularity should raise error', function() { assert.throws(function() { assert.sameMoment(date, oneDayLater, 'moments are not the same'); - }, 'moments are not the same'); + }, /^moments are not the same: expected \d{14} to be the same as \d{14}$/); }); }) @@ -122,10 +128,16 @@ describe('afterMoment', function() { assert.afterMoment(oneYearLater, date, 'month'); }); + it('tdd-style with error message', function() { + assert.throws(function() { + assert.afterMoment(date, obj, 'second', 'not after second granularity'); + }, /^not after second granularity: expected \d{14} to be after \d{14} \(granularity: second\)$/); + }); + it('tdd-style with error message in place of granularity should raise error', function() { assert.throws(function() { assert.afterMoment(date, obj, 'moment is not after expected'); - }, 'moment is not after expected'); + }, /^moment is not after expected: expected \d{14} to be after \d{14}$/); }); }); @@ -166,10 +178,16 @@ describe('beforeMoment', function() { assert.beforeMoment(date, oneYearLater, 'month'); }); + it('tdd-style with error message in place of granularity should raise error', function() { + assert.throws(function() { + assert.beforeMoment(date, obj, 'second', 'not before second granularity'); + }, /^not before second granularity: expected \d{14} to be before \d{14} \(granularity: second\)$/); + }); + it('tdd-style with error message in place of granularity should raise error', function() { assert.throws(function() { assert.beforeMoment(date, obj, 'moment is not before expected'); - }, 'moment is not before expected'); + }, /^moment is not before expected: expected \d{14} to be before \d{14}$/); }); }); @@ -178,7 +196,7 @@ describe('beforeMoment', function() { describe('chaiMoment.setErrorFormat', function() { it('is a function', function() { - expect(chaiMoment.setErrorFormat).to.be.a.function; + expect(chaiMoment.setErrorFormat).to.be.a('function'); }); it('sets the moment.format() call for an error', function() {