From 281b8afc183c8bf91d4c3c13d1160ed41070b853 Mon Sep 17 00:00:00 2001 From: Tim Li Date: Wed, 20 Jun 2018 10:37:09 +0930 Subject: [PATCH 1/2] avoid using error message as granularity in message details --- index.js | 15 ++++++++++++--- test/index.test.js | 26 ++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) 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..4c9c8f2 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}$/); }); }); From 6c8bed8e1b9ad2b39123ed919f0c9808cdb99e4d Mon Sep 17 00:00:00 2001 From: Tim Li Date: Wed, 20 Jun 2018 10:40:50 +0930 Subject: [PATCH 2/2] fix wrong chai instance chain --- test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.test.js b/test/index.test.js index 4c9c8f2..9a29daf 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -196,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() {