Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid using error message as granularity in message details #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
Expand Down
28 changes: 23 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var chaiMoment = require('../index.js');

chai.use(chaiMoment);

chaiMoment.setErrorFormat('LLLL');
chaiMoment.setErrorFormat('YYYYMMDDHHmmss');

var moment = require('moment');

Expand Down Expand Up @@ -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}$/);
});

})
Expand Down Expand Up @@ -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}$/);
});

});
Expand Down Expand Up @@ -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}$/);
});

});
Expand All @@ -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() {
Expand Down