From f2681ef052d04ecaae2a4bac18ec8aeccd50ad4e Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 09:13:15 +0200 Subject: [PATCH 01/35] Refactor tests [ci skip] --- test/unexpected-moment.spec.js | 917 +++++++++++++++++++++------------ 1 file changed, 592 insertions(+), 325 deletions(-) diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 4f2297d..c6ece0c 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -8,397 +8,664 @@ describe('unexpected-moment', function () { .use(unexpectedMoment); var clock; - var oneSecond; - var oneMinute; - var oneHour; - var oneDay; - var oneWeek; - var oneMonth; - var oneYear; before(function () { clock = sinon.useFakeTimers(); moment.tz.add('Europe/Copenhagen|CET CEST|-10 -20|0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-2azC0 Tz0 VuO0 60q0 WM0 1fA0 1cM0 1cM0 1cM0 S00 1HA0 Nc0 1C00 Dc0 1Nc0 Ao0 1h5A0 1a00 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00'); moment.tz.setDefault('Europe/Copenhagen'); - - oneSecond = moment.duration(1, 'second').valueOf(); - oneMinute = moment.duration(1, 'minute').valueOf(); - oneHour = moment.duration(1, 'hour').valueOf(); - oneDay = moment.duration(1, 'day').valueOf(); - oneWeek = moment.duration(1, 'week').valueOf(); - oneMonth = moment.duration(1, 'month').valueOf(); - oneYear = moment.duration(1, 'year').valueOf(); }); after(function () { clock.restore(); }); - it('identifies a moment.js instance', function () { - expect(moment(), 'to be a moment'); - }); + describe('to be a moment', function () { + it('passes if the subject is a moment instance', function () { + expect(moment(), 'to be a moment'); + }); - it('does not falsely identify a date instance as a moment.js instance', function () { - expect(new Date(), 'not to be a moment'); + it('throws the correct error if the subject is not a moment instance', function () { + expect(function () { + expect('not moment', 'to be a moment'); + }, 'to error with', 'expected \'not moment\' to be a moment'); + }); }); - it('identifies a moment as equal to it\'s clone', function () { - var m = moment(); - expect(m, 'to equal', m.clone()); - }); + describe('not to be a moment', function () { + it('does not falsely identify a date instance as a moment instance', function () { + expect(new Date(), 'not to be a moment'); + }); - it('identifies two unequal moments', function () { - expect(moment(0), 'not to equal', moment(1)); + it('throws the correct error if the subject is a moment instance', function () { + expect(function () { + expect(moment('2016-01-01'), 'not to be a moment'); + }, 'to error with', 'expected moment(2016-01-01T00:00:00.000+01:00) not to be a moment'); + }); }); - it('identifies strict equality in moments', function () { - expect(moment(0), 'to equal', moment(0)); - expect(moment(0), 'not to equal', new Date(0)); - }); + describe('to equal', function () { + it('passes if the moments are equal', function () { + expect(moment(1), 'to equal', moment(1)); + }); + + it('passes if the value is a clone of the subject', function () { + var m = moment(); + expect(m, 'to equal', m.clone()); + }); + + it('allows specifying granularity as the fourth parameter', function () { + expect(moment(1), 'to equal', moment(0), 'second'); + }); + + it('allows specifying granularity as a readable label', function () { + expect(moment(1), 'to equal', moment(0), 'in years'); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to equal', moment('2016-01-02')); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'to equal moment(2016-01-02T00:00:00.000+01:00)\n' + + '\n' + + '-moment(2016-01-01T00:00:00.000+01:00)\n' + + '+moment(2016-01-02T00:00:00.000+01:00)' + ); + }); + + it('throws the correct error if the value is not a moment object', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to equal', '2016-01-01'); + }, + 'to error with', + 'Unknown assertion to equal \n' + + '\n' + + 'Did you mean to satisfy ?' + ); + }); - it('identifies a moment that occurs before another chronologically', function () { - expect(moment(0), 'to be before', moment(1)); - expect(moment(0), 'to be before', moment(1).toISOString()); - expect(moment(0), 'to be before', moment(1).toArray()); - expect(moment(0), 'to be before', moment(1).toDate()); - expect(moment(0), 'to be before', moment(1).valueOf()); - expect(moment(0), 'to be before', moment(1).toObject()); - - - expect(moment(1), 'not to be before', moment(0)); - expect(moment(1), 'not to be before', moment(0).toISOString()); - expect(moment(1), 'not to be before', moment(0).toArray()); - expect(moment(1), 'not to be before', moment(0).toDate()); - expect(moment(1), 'not to be before', moment(0).valueOf()); - expect(moment(1), 'not to be before', moment(0).toObject()); + it('throws the correct error if the assertion fails and granularity is provided', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to equal', moment('2016-01-02'), 'in milliseconds'); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'to equal moment(2016-01-02T00:00:00.000+01:00) in milliseconds\n' + + '\n' + + '-moment(2016-01-01T00:00:00.000+01:00)\n' + + '+moment(2016-01-02T00:00:00.000+01:00)' + ); + }); }); - it('identifies a moment that occurs after another chronologically', function () { - expect(moment(1), 'to be after', moment(0)); - expect(moment(1), 'to be after', moment(0).toISOString()); - expect(moment(1), 'to be after', moment(0).toArray()); - expect(moment(1), 'to be after', moment(0).toDate()); - expect(moment(1), 'to be after', moment(0).valueOf()); - expect(moment(1), 'to be after', moment(0).toObject()); - - expect(moment(0), 'not to be after', moment(1)); - expect(moment(0), 'not to be after', moment(1).toISOString()); - expect(moment(0), 'not to be after', moment(1).toArray()); - expect(moment(0), 'not to be after', moment(1).toDate()); - expect(moment(0), 'not to be after', moment(1).valueOf()); - expect(moment(0), 'not to be after', moment(1).toObject()); + describe('not to equal', function () { + it('passes if the moments are not equal', function () { + expect(moment(0), 'not to equal', moment(1)); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-01'), 'not to equal', moment('2016-01-01')); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'not to equal moment(2016-01-01T00:00:00.000+01:00)' + ); + }); }); - it('identifies a moment that occurs at same time or before another chronologically', function () { - expect(moment(0), 'to be same or before', moment(1)); - expect(moment(0), 'to be same or before', moment(1).toISOString()); - expect(moment(0), 'to be same or before', moment(1).toArray()); - expect(moment(0), 'to be same or before', moment(1).toDate()); - expect(moment(0), 'to be same or before', moment(1).valueOf()); - expect(moment(0), 'to be same or before', moment(1).toObject()); - - expect(moment(1), 'not to be same or before', moment(0)); - expect(moment(1), 'not to be same or before', moment(0).toISOString()); - expect(moment(1), 'not to be same or before', moment(0).toArray()); - expect(moment(1), 'not to be same or before', moment(0).toDate()); - expect(moment(1), 'not to be same or before', moment(0).valueOf()); - expect(moment(1), 'not to be same or before', moment(0).toObject()); - - expect(moment(0), 'to be same or before', moment(0)); - expect(moment(0), 'to be same or before', moment(0).toISOString()); - expect(moment(0), 'to be same or before', moment(0).toArray()); - expect(moment(0), 'to be same or before', moment(0).toDate()); - expect(moment(0), 'to be same or before', moment(0).valueOf()); - expect(moment(0), 'to be same or before', moment(0).toObject()); - - expect(moment(1), 'not to be same or before', moment(0)); - expect(moment(1), 'not to be same or before', moment(0).toISOString()); - expect(moment(1), 'not to be same or before', moment(0).toArray()); - expect(moment(1), 'not to be same or before', moment(0).toDate()); - expect(moment(1), 'not to be same or before', moment(0).valueOf()); - expect(moment(1), 'not to be same or before', moment(0).toObject()); + describe('to satisfy', function () { + it('passes if the two moments are equal', function () { + expect(moment('2016-01-01'), 'to satisfy', moment('2016-01-01')); + }); + + it('passes if the value is a clone of the subject', function () { + var aMoment = moment('2016-01-01'); + expect(aMoment, 'to satisfy', aMoment.clone()); + }); + + it('passes if passed an ISO-formatted string as the expected value', function () { + expect(moment('2016-01-01'), 'to satisfy', '2016-01-01T00:00:00+01:00'); + }); + + it('passes if passed an array as the expected value', function () { + expect(moment('2016-01-01'), 'to satisfy', [2016, 0, 1, 0, 0, 0, 0]); + }); + + it('passes if passed an object as the expected value', function () { + expect(moment('2016-01-01'), 'to satisfy', { year: 2016, month: 0, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }); + }); + + it('passes with a partial object', function () { + expect(moment('2016-01-01'), 'to satisfy', { year: 2016, date: 1, seconds: 0 }); + }); + + it('throws if passed an empty object as the value', function () { + expect(function () { + expect(moment('2016-01-01'), 'to satisfy', {}); + }, 'to error'); + }); + + it('throws if passed an objet with unknown keys', function () { + expect(function () { + expect(moment('2016-01-01'), 'not to satisfy', { + years: 2016, + mints: 0 + }); + }, 'to error'); + }); + + it('passes if passed a number as the expected value', function () { + expect(moment(123456), 'to satisfy', 123456); + }); + + + it('passes if passed a Date as the expected value', function () { + expect(moment('2016-01-01 00:00:01'), 'to satisfy', new Date('2016-01-01 00:00:01')); + }); + + // see: https://github.com/moment/moment/issues/2633 + it('throws for utc moments if the expected value is not a moment instance even though it represents the same moment', function () { + expect(function () { + expect(moment.utc('2015-01-01T00:00:00+00:00'), 'not to satisfy', [2015, 0, 1, 0, 0, 0, 0]); + }, 'to error'); + }); + + it('throws the correct error if the assertion fails for a string value', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', '2016-01-02'); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'to satisfy \'2016-01-02\'\n' + + '\n' + + '-2016-01-01\n' + + '+2016-01-02' + ); + }); + + it('throws the correct error if the assertion fails for an array value', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', [2016, 0, 2]); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'to satisfy \'2016-01-02\'\n' + + '\n' + + '-[ 2016, 0, 1 ]\n' + + '+[ 2016, 0, 2 ]' + ); + }); + + it('throws the correct error if the assertion fails for an object value', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', { year: 2016, month: 0, date: 2, minute: 10, millisecond: 3 }); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'to satisfy \'2016-01-02\'\n' + + '\n' + + '-{ year: 2016, month: 0, day: 1, minute: 0, second: 0, millisecond: 0 }\n' + + '+{ year: 2016, month: 0, date: 2, minute: 10, millisecond: 3 }' + ); + }); + + it('throws the correct error if the assertion fails for a unix timestamp (seconds)', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', 1451689200); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'to satisfy \'2016-01-02\'\n' + + '\n' + + '-1451602800\n' + + '+1451689200' + ); + }); + + it('throws the correct error if the assertion fails for a unix timestamp (milliseconds)', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', 1451689200000); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'to satisfy \'2016-01-02\'\n' + + '\n' + + '-1451602800000\n' + + '+1451689200000' + ); + }); + + it('throws the correct error if the assertion fails for a Date value', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', new Date('2016-01-02')); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'to satisfy \'2016-01-02\'\n' + + '\n' + + '-new Date(\'2016-01-01\')\n' + + '+new Date(\'2016-01-02\')' + ); + }); }); - it('identifies a moment that occurs at same time or after chronologically', function () { - expect(moment(1), 'to be same or after', moment(0)); - expect(moment(1), 'to be same or after', moment(0).toISOString()); - expect(moment(1), 'to be same or after', moment(0).toArray()); - expect(moment(1), 'to be same or after', moment(0).toDate()); - expect(moment(1), 'to be same or after', moment(0).valueOf()); - expect(moment(1), 'to be same or after', moment(0).toObject()); - - expect(moment(0), 'not to be same or after', moment(1)); - expect(moment(0), 'not to be same or after', moment(1).toISOString()); - expect(moment(0), 'not to be same or after', moment(1).toArray()); - expect(moment(0), 'not to be same or after', moment(1).toDate()); - expect(moment(0), 'not to be same or after', moment(1).valueOf()); - expect(moment(0), 'not to be same or after', moment(1).toObject()); - - expect(moment(0), 'to be same or after', moment(0)); - expect(moment(0), 'to be same or after', moment(0).toISOString()); - expect(moment(0), 'to be same or after', moment(0).toArray()); - expect(moment(0), 'to be same or after', moment(0).toDate()); - expect(moment(0), 'to be same or after', moment(0).valueOf()); - expect(moment(0), 'to be same or after', moment(0).toObject()); + describe('not to satisfy', function () { + it('passes if the moments are not equal', function () { + expect(moment(0), 'not to satisfy', moment(1)); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-01'), 'not to satisfy', moment('2016-01-01')); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'not to satisfy moment(2016-01-01T00:00:00.000+01:00)' + ); + }); }); - it('identifies moments between other moments', function () { - expect(moment(0), 'not to be between', moment(0), moment(2)); - expect(moment(1), 'to be between', moment(0), moment(2)); - expect(moment(2), 'not to be between', moment(0), moment(2)); + describe('to be before', function () { + it('passes if the value occurs before the subject', function () { + expect(moment(0), 'to be before', moment(1)); + }); - // Inclusivity - expect(moment(0), 'not to be inclusively between', moment(1), moment(2)); - expect(moment(1), 'to be inclusively between', moment(1), moment(2)); - expect(moment(2), 'to be inclusively between', moment(1), moment(2)); - expect(moment(3), 'not to be inclusively between', moment(1), moment(2)); + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-03'), 'to be before', new Date('2016-01-02 00:00:00')); + }, + 'to error with', + 'expected moment(2016-01-03T00:00:00.000+01:00)\n' + + 'to be before new Date(\'Fri, 01 Jan 2016 23:00:00 GMT\')' + ); + }); }); - it('allows formatting moments asserting against expected string outputs', function () { - expect(moment(1), 'when formatted with', 'YYYY', 'to be', '1970'); - expect(moment(1), 'when formatted with', 'YYYY', 'to equal', '1970'); - expect(moment(1), 'when formatted with', 'YYYYMM', 'not to be', '1970'); - expect(moment(1), 'when formatted with', 'YYYYMM', 'not to equal', '1970'); + describe('not to be before', function () { + it('passes if the value does not occur before the subject', function () { + expect(moment(1), 'not to be before', moment(0)); + }); - expect(moment.utc(), 'when formatted with', '', 'to be', '1970-01-01T00:00:00Z'); - expect(moment.utc(), 'when formatted with', '', 'to equal', '1970-01-01T00:00:00Z'); - expect(moment.utc(1000), 'when formatted with', '', 'not to be', '1970-01-01T00:00:00Z'); - expect(moment.utc(1000), 'when formatted with', '', 'not to equal', '1970-01-01T00:00:00Z'); + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-01'), 'not to be before', '2016-01-02 00:00:00'); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'not to be before \'2016-01-02 00:00:00\'' + ); + }); }); - it('identifies (un)equality in various granularity levels', function () { - expect(moment(1), 'to equal', moment(1), 'in milliseconds'); - expect(moment(1), 'to equal', moment(1), 'in seconds'); - expect(moment(1), 'to equal', moment(1), 'in minutes'); - expect(moment(1), 'to equal', moment(1), 'in hours'); - expect(moment(1), 'to equal', moment(1), 'in days'); - expect(moment(1), 'to equal', moment(1), 'in weeks'); - expect(moment(1), 'to equal', moment(1), 'in months'); - expect(moment(1), 'to equal', moment(1), 'in years'); - - expect(moment(1), 'not to equal', moment(2), 'in milliseconds'); - expect(moment(1), 'to equal', moment(2), 'in seconds'); - expect(moment(1), 'to equal', moment(2), 'in minutes'); - expect(moment(1), 'to equal', moment(2), 'in hours'); - expect(moment(1), 'to equal', moment(2), 'in days'); - expect(moment(1), 'to equal', moment(2), 'in weeks'); - expect(moment(1), 'to equal', moment(2), 'in months'); - expect(moment(1), 'to equal', moment(2), 'in years'); - - expect(moment(oneSecond), 'not to equal', moment(oneSecond + oneSecond), 'in milliseconds'); - expect(moment(oneSecond), 'not to equal', moment(oneSecond + oneSecond), 'in seconds'); - expect(moment(oneSecond), 'to equal', moment(oneSecond + oneSecond), 'in minutes'); - expect(moment(oneSecond), 'to equal', moment(oneSecond + oneSecond), 'in hours'); - expect(moment(oneSecond), 'to equal', moment(oneSecond + oneSecond), 'in days'); - expect(moment(oneSecond), 'to equal', moment(oneSecond + oneSecond), 'in weeks'); - expect(moment(oneSecond), 'to equal', moment(oneSecond + oneSecond), 'in months'); - expect(moment(oneSecond), 'to equal', moment(oneSecond + oneSecond), 'in years'); - - expect(moment(oneMinute), 'not to equal', moment(oneMinute + oneMinute), 'in milliseconds'); - expect(moment(oneMinute), 'not to equal', moment(oneMinute + oneMinute), 'in seconds'); - expect(moment(oneMinute), 'not to equal', moment(oneMinute + oneMinute), 'in minutes'); - expect(moment(oneMinute), 'to equal', moment(oneMinute + oneMinute), 'in hours'); - expect(moment(oneMinute), 'to equal', moment(oneMinute + oneMinute), 'in days'); - expect(moment(oneMinute), 'to equal', moment(oneMinute + oneMinute), 'in weeks'); - expect(moment(oneMinute), 'to equal', moment(oneMinute + oneMinute), 'in months'); - expect(moment(oneMinute), 'to equal', moment(oneMinute + oneMinute), 'in years'); - - expect(moment(oneHour), 'not to equal', moment(oneHour + oneHour), 'in milliseconds'); - expect(moment(oneHour), 'not to equal', moment(oneHour + oneHour), 'in seconds'); - expect(moment(oneHour), 'not to equal', moment(oneHour + oneHour), 'in minutes'); - expect(moment(oneHour), 'not to equal', moment(oneHour + oneHour), 'in hours'); - expect(moment(oneHour), 'to equal', moment(oneHour + oneHour), 'in days'); - expect(moment(oneHour), 'to equal', moment(oneHour + oneHour), 'in weeks'); - expect(moment(oneHour), 'to equal', moment(oneHour + oneHour), 'in months'); - expect(moment(oneHour), 'to equal', moment(oneHour + oneHour), 'in years'); - - expect(moment(oneDay), 'not to equal', moment(oneDay + oneDay), 'in milliseconds'); - expect(moment(oneDay), 'not to equal', moment(oneDay + oneDay), 'in seconds'); - expect(moment(oneDay), 'not to equal', moment(oneDay + oneDay), 'in minutes'); - expect(moment(oneDay), 'not to equal', moment(oneDay + oneDay), 'in hours'); - expect(moment(oneDay), 'not to equal', moment(oneDay + oneDay), 'in days'); - expect(moment(oneDay), 'to equal', moment(oneDay + oneDay), 'in weeks'); - expect(moment(oneDay), 'to equal', moment(oneDay + oneDay), 'in months'); - expect(moment(oneDay), 'to equal', moment(oneDay + oneDay), 'in years'); - - expect(moment(oneWeek), 'not to equal', moment(oneWeek + oneWeek), 'in milliseconds'); - expect(moment(oneWeek), 'not to equal', moment(oneWeek + oneWeek), 'in seconds'); - expect(moment(oneWeek), 'not to equal', moment(oneWeek + oneWeek), 'in minutes'); - expect(moment(oneWeek), 'not to equal', moment(oneWeek + oneWeek), 'in hours'); - expect(moment(oneWeek), 'not to equal', moment(oneWeek + oneWeek), 'in days'); - expect(moment(oneWeek), 'not to equal', moment(oneWeek + oneWeek), 'in weeks'); - expect(moment(oneWeek), 'to equal', moment(oneWeek + oneWeek), 'in months'); - expect(moment(oneWeek), 'to equal', moment(oneWeek + oneWeek), 'in years'); - - expect(moment(oneMonth), 'not to equal', moment(oneMonth + oneMonth), 'in milliseconds'); - expect(moment(oneMonth), 'not to equal', moment(oneMonth + oneMonth), 'in seconds'); - expect(moment(oneMonth), 'not to equal', moment(oneMonth + oneMonth), 'in minutes'); - expect(moment(oneMonth), 'not to equal', moment(oneMonth + oneMonth), 'in hours'); - expect(moment(oneMonth), 'not to equal', moment(oneMonth + oneMonth), 'in days'); - expect(moment(oneMonth), 'not to equal', moment(oneMonth + oneMonth), 'in weeks'); - expect(moment(oneMonth), 'not to equal', moment(oneMonth + oneMonth), 'in months'); - expect(moment(oneMonth), 'to equal', moment(oneMonth + oneMonth), 'in years'); - - expect(moment(oneYear), 'not to equal', moment(oneYear + oneYear), 'in milliseconds'); - expect(moment(oneYear), 'not to equal', moment(oneYear + oneYear), 'in seconds'); - expect(moment(oneYear), 'not to equal', moment(oneYear + oneYear), 'in minutes'); - expect(moment(oneYear), 'not to equal', moment(oneYear + oneYear), 'in hours'); - expect(moment(oneYear), 'not to equal', moment(oneYear + oneYear), 'in days'); - expect(moment(oneYear), 'not to equal', moment(oneYear + oneYear), 'in weeks'); - expect(moment(oneYear), 'not to equal', moment(oneYear + oneYear), 'in months'); - expect(moment(oneYear), 'not to equal', moment(oneYear + oneYear), 'in years'); + describe('to be after', function () { + it('passes if the value occurs after the subject', function () { + expect(moment(1), 'to be after', moment(0)); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to be after', [2016, 0, 2, 0, 0, 0]); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00) ' + + 'to be after [ 2016, 0, 2, 0, 0, 0 ]' + ); + }); }); - it('identifies if a moment is the start of a unit of time', function () { - expect(moment(123456).startOf('second'), 'to be the start of second'); - expect(moment(123456).startOf('minute'), 'to be the start of minute'); - expect(moment(123456).startOf('hour'), 'to be the start of hour'); - expect(moment(123456).startOf('day'), 'to be the start of day'); - expect(moment(123456).startOf('week'), 'to be the start of week'); - expect(moment(123456).startOf('isoWeek'), 'to be the start of isoWeek'); - expect(moment(123456).startOf('month'), 'to be the start of month'); - expect(moment(123456).startOf('quarter'), 'to be the start of quarter'); - expect(moment(123456).startOf('year'), 'to be the start of year'); - - expect(moment(123456).startOf('second').add(1), 'not to be the start of second'); - expect(moment(123456).startOf('minute').add(1, 'second'), 'not to be the start of minute'); - expect(moment(123456).startOf('hour').add(1, 'second'), 'not to be the start of hour'); - expect(moment(123456).startOf('day').add(1, 'second'), 'not to be the start of day'); - expect(moment(123456).startOf('week').add(1, 'second'), 'not to be the start of week'); - expect(moment(123456).startOf('isoWeek').add(1, 'second'), 'not to be the start of isoWeek'); - expect(moment(123456).startOf('month').add(1, 'second'), 'not to be the start of month'); - expect(moment(123456).startOf('quarter').add(1, 'second'), 'not to be the start of quarter'); - expect(moment(123456).startOf('year').add(1, 'second'), 'not to be the start of year'); + describe('not to be after', function () { + it('passes if the value does not occur after the subject', function () { + expect(moment(0), 'not to be after', moment(1)); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-02'), 'not to be after', { year: 2016, month: 0, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }); + }, + 'to error with', + 'expected moment(2016-01-02T00:00:00.000+01:00) ' + + 'not to be after\n' + + '{\n' + + ' year: 2016, month: 0, day: 1, hour: 0, minute: 0, second: 0,\n' + + ' millisecond: 0\n' + + '}' + ); + }); }); - it('identifies if a moment is the end of a unit of time', function () { - expect(moment(123456).endOf('second'), 'to be the end of second'); - expect(moment(123456).endOf('minute'), 'to be the end of minute'); - expect(moment(123456).endOf('hour'), 'to be the end of hour'); - expect(moment(123456).endOf('day'), 'to be the end of day'); - expect(moment(123456).endOf('week'), 'to be the end of week'); - expect(moment(123456).endOf('isoWeek'), 'to be the end of isoWeek'); - expect(moment(123456).endOf('month'), 'to be the end of month'); - expect(moment(123456).endOf('quarter'), 'to be the end of quarter'); - expect(moment(123456).endOf('year'), 'to be the end of year'); - - expect(moment(123456).endOf('second').subtract(1), 'not to be the end of second'); - expect(moment(123456).endOf('minute').subtract(1, 'second'), 'not to be the end of minute'); - expect(moment(123456).endOf('hour').subtract(1, 'second'), 'not to be the end of hour'); - expect(moment(123456).endOf('day').subtract(1, 'second'), 'not to be the end of day'); - expect(moment(123456).endOf('week').subtract(1, 'second'), 'not to be the end of week'); - expect(moment(123456).endOf('isoWeek').subtract(1, 'second'), 'not to be the end of isoWeek'); - expect(moment(123456).endOf('month').subtract(1, 'second'), 'not to be the end of month'); - expect(moment(123456).endOf('quarter').subtract(1, 'second'), 'not to be the end of quarter'); - expect(moment(123456).endOf('year').subtract(1, 'second'), 'not to be the end of year'); + describe('to be same or before', function () { + it('passes if the value occurs before the subject', function () { + expect(moment(0), 'to be same or before', moment(1)); + }); + + it('passes if the value is the same as the subject', function () { + expect(moment(0), 'to be same or before', moment(0)); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to be same or before', 1451602800); // 1451602800 -> new Date('2016-01-01 00:00:00') + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00) ' + + 'to be same or before 1451602800' + ); + }); }); - it('identifies satisfaction and dissatisfaction in moments', function () { - // it('identifies satisfying and disatisfying moments/dates', function () { - var aMoment = moment('2015-01-01T00:00:00+01:00'); - var aUtcMoment = moment.utc('2015-01-01T00:00:00+00:00'); + describe('not to be same or before', function () { + it('passes if the value does not occur before the subject', function () { + expect(moment(1), 'not to be same or before', moment(0)); + }); - expect(aMoment, 'to satisfy', aMoment.clone()); - expect(aMoment, 'not to satisfy', aMoment.clone().add(1)); + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-01'), 'not to be same or before', 1451689200000); // 1451689200000 -> new Date('2016-01-02 00:00:00') + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'not to be same or before 1451689200000' + ); + }); + }); - expect(moment(1000), 'to satisfy', moment(1000)); - expect(moment(1000), 'not to satisfy', moment(2000)); + describe('to be same or after', function () { + it('passes if the value occurs after the subject', function () { + expect(moment(1), 'to be same or after', moment(0)); + }); - expect(aMoment, 'to satisfy', aMoment.toDate()); - expect(aMoment, 'not to equal', aMoment.toDate()); + it('passes if the value is the same as the subject', function () { + expect(moment(0), 'to be same or after', moment(0)); + }); - expect(aUtcMoment, 'to satisfy', new Date('Thu Jan 01 2015 01:00:00 GMT+0100 (CET)')); - expect(aUtcMoment, 'not to equal', new Date('Thu Jan 01 2015 01:00:00 GMT+0100 (CET)')); + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to be same or after', new Date('2016-01-02 00:00:00')); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'to be same or after new Date(\'Fri, 01 Jan 2016 23:00:00 GMT\')' + ); + }); + }); - expect(aMoment, 'to satisfy', aMoment.toArray()); - expect(aMoment, 'not to equal', aMoment.toArray()); - expect(aMoment, 'not to satisfy', [1, 2, 3, 4, 5, 6, 7, 8]); + describe('not to be same or after', function () { + it('passes if the value does not occur after the subject', function () { + expect(moment(0), 'not to be same or after', moment(1)); + }); - expect(aUtcMoment, 'not to satisfy', [2015, 0, 1, 0, 0, 0, 0]); // see: https://github.com/moment/moment/issues/2633 - expect(aUtcMoment, 'not to equal', [2015, 0, 1, 0, 0, 0, 0]); + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-02'), 'not to be same or after', moment('2016-01-01 00:00:00')); + }, + 'to error with', + 'expected moment(2016-01-02T00:00:00.000+01:00)\n' + + 'not to be same or after moment(2016-01-01T00:00:00.000+01:00)' + ); + }); + }); + describe('to be between', function () { + it('passes if the subject occurs between the two values', function () { + expect(moment(1), 'to be between', moment(0), moment(2)); + }); - expect(aMoment, 'to satisfy', aMoment.toISOString()); - expect(aMoment, 'to satisfy', aMoment.format()); + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to be between', moment('2016-01-02'), moment('2016-01-03')); + }, + 'to error with', + 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'to be between moment(2016-01-02T00:00:00.000+01:00) and moment(2016-01-03T00:00:00.000+01:00)' + ); + }); + }); - expect(aMoment, 'to satisfy', aMoment.toJSON()); - expect(aMoment, 'not to equal', aMoment.toJSON()); + describe('not to be between', function () { + it('passes if the subject does not occur between the two values', function () { + expect(moment(0), 'not to be between', moment(1), new Date(2)); + }); - expect(aUtcMoment, 'to satisfy', '2015-01-01T00:00:00.000Z'); - expect(aUtcMoment, 'to satisfy', '2015-01-01T00:00:00+00:00'); - expect(aUtcMoment, 'not to equal', '2015-01-01T00:00:00.000Z'); + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-02'), 'not to be between', moment('2016-01-01'), moment('2016-01-03')); + }, + 'to error with', + 'expected moment(2016-01-02T00:00:00.000+01:00)\n' + + 'not to be between moment(2016-01-01T00:00:00.000+01:00) and moment(2016-01-03T00:00:00.000+01:00)' + ); + }); + }); - expect(aMoment, 'to satisfy', aMoment.toObject()); - expect(aMoment, 'not to equal', aMoment.toObject()); + describe('to be inclusively between', function () { + it('passes if the subject is the same as the first value', function () { + expect(moment(0), 'to be inclusively between', moment(0), moment(1)); + }); - expect(aUtcMoment, 'not to satisfy', aUtcMoment.toObject()); // see: https://github.com/moment/moment/issues/2633 + it('passes if the subject is the same as the second value', function () { + expect(moment(1), 'to be inclusively between', moment(0), moment(1)); + }); - // partial object passes - expect(aMoment, 'to satisfy', { - years: 2015, - date: 1, - seconds: 0 + it('passes if the subject occurs betwen the two values', function () { + expect(moment(1), 'to be inclusively between', moment(0), moment(2)); }); - // but not an empty one - expect(aMoment, 'not to satisfy', {}); + it('passes if the two values are the same', function () { + expect(moment(0), 'to be inclusively between', moment(0), moment(0)); + }); - // TODO: nor one with unkown keys - // expect(aUtcMoment, 'not to satisfy', { - // years: 2015, - // mints: 0 - // }); + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-02'), 'to be inclusively between', new Date('2016-01-01 00:00:00'), new Date('2016-01-01 12:00:00')); + }, + 'to error with', + 'expected moment(2016-01-02T00:00:00.000+01:00)\n' + + 'to be inclusively between new Date(\'Thu, 31 Dec 2015 23:00:00 GMT\') ' + + 'and new Date(\'Fri, 01 Jan 2016 11:00:00 GMT\')' + ); + }); }); - it('does not mutate the moment objects when checking for equality', function () { - var moment1 = moment(1000); - var moment2 = moment(123456); - var moment1Formartted = moment1.format(); - var moment2Formartted = moment2.format(); - expect(moment1, 'not to equal', moment2); - expect(moment1Formartted, 'to equal', moment1.format()); - expect(moment2Formartted, 'to equal', moment2.format()); + describe('not to be inclusively between', function () { + it('passes if the subject is neither the same as the first value or the second, nor does it occur between them', function () { + expect(moment(0), 'not to be inclusively between', moment(1), moment(1)); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-02'), 'not to be inclusively between', moment('2016-01-02'), moment('2016-01-02')); + }, + 'to error with', + 'expected moment(2016-01-02T00:00:00.000+01:00)\n' + + 'not to be inclusively between moment(2016-01-02T00:00:00.000+01:00) ' + + 'and moment(2016-01-02T00:00:00.000+01:00)' + ); + }); }); - it('does not mutate the moment objects when checking for satisfaction', function () { - var moment1 = moment(1000); - var moment2 = moment(123456); - var moment1Formartted = moment1.format(); - var moment2Formartted = moment2.format(); - expect(moment1, 'not to satisfy', moment2); - expect(moment1Formartted, 'to equal', moment1.format()); - expect(moment2Formartted, 'to equal', moment2.format()); + describe('when formatted with', function () { + it('formats a moment with the provided format and delegates to the next assertion', function () { + expect(moment(1), 'when formatted with', 'YYYY', 'to be', '1970'); + }); + + it('formats a moment with the default format if called with an empty string format', function () { + expect(moment.utc(0), 'when formatted with', '', 'to equal', '1970-01-01T00:00:00Z'); + }); }); - it('does not mutate the moment objects when checking for equality at various granularity levels', function () { - var moment1 = moment(1000); - var moment2 = moment(1500); - var moment1Formartted = moment1.format(); - var moment2Formartted = moment2.format(); - expect(moment1, 'to equal', moment2, 'in seconds'); - expect(moment1Formartted, 'to equal', moment1.format()); - expect(moment2Formartted, 'to equal', moment2.format()); + describe('to be the start of', function () { + it('start of second', function () { + expect(moment('2016-01-01 00:00:01+01:00'), 'to be the start of second'); + }); + + it('start of minute', function () { + expect(moment('2016-01-01 00:01:00+01:00'), 'to be the start of minute'); + }); + + it('start of hour', function () { + expect(moment('2016-01-01 01:00:00+01:00'), 'to be the start of hour'); + }); + + it('start of day', function () { + expect(moment('2016-01-02 00:00:00+01:00'), 'to be the start of day'); + }); + + it('start of week', function () { + expect(moment('2016-01-03 00:00:00+01:00'), 'to be the start of week'); + }); + + it('start of isoWeek', function () { + expect(moment('2016-01-04 00:00:00+01:00'), 'to be the start of isoWeek'); + }); + + it('start of month', function () { + expect(moment('2016-01-01 00:00:00+01:00'), 'to be the start of month'); + }); + + it('start of quarter', function () { + expect(moment('2016-01-01 00:00:00+01:00'), 'to be the start of quarter'); + }); + + it('start of year', function () { + expect(moment('2016-01-01 00:00:00+01:00'), 'to be the start of year'); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-02'), 'to be the start of year'); + }, + 'to error with', + 'expected moment(2016-01-02T00:00:00.000+01:00) ' + + 'to be the start of year\n' + + '\n' + + '-moment(2016-01-02T00:00:00.000+01:00)\n' + + '+moment(2016-01-01T00:00:00.000+01:00)' + ); + }); + + it('does not mutate the moment objects', function () { + var startOfDay = moment('2016-01-02T00:00:00.000+01:00'); + expect(startOfDay, 'to be the start of day'); + expect(startOfDay.format(), 'to be', '2016-01-02T00:00:00+01:00'); + }); }); - it('does not mutate the moment objects when checking for equality against a certain format', function () { - var moment1 = moment(1000); - var moment1Formartted = moment1.format(); - expect(moment1, 'when formatted with', 'YYYYMMDD', 'to equal', '19700101'); - expect(moment1Formartted, 'to equal', moment1.format()); + describe('not to be the start of', function () { + it('passes if the passed moment is not start of the unit of time provided', function () { + expect(moment('2016-01-01 00:00:01.002+01:00'), 'not to be the start of second'); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-02'), 'not to be the start of day'); + }, + 'to error with', + 'expected moment(2016-01-02T00:00:00.000+01:00) ' + + 'not to be the start of day' + ); + }); }); - it('does not mutate the moment objects when checking for equality against the start of a unit of time', function () { - var moment1 = moment(1000).startOf('isoWeek'); - var moment1Formartted = moment1.format(); - expect(moment1, 'to be the start of isoWeek'); - expect(moment1Formartted, 'to equal', moment1.format()); + describe('to be the end of', function () { + it('end of second', function () { + expect(moment('2016-01-01 00:00:01.999+01:00'), 'to be the end of second'); + }); + + it('end of minute', function () { + expect(moment('2016-01-01 00:01:59.999+01:00'), 'to be the end of minute'); + }); + + it('end of hour', function () { + expect(moment('2016-01-01 01:59:59.999+01:00'), 'to be the end of hour'); + }); + + it('end of day', function () { + expect(moment('2016-01-02 23:59:59.999+01:00'), 'to be the end of day'); + }); + + it('end of week', function () { + expect(moment('2016-01-09 23:59:59.999+01:00'), 'to be the end of week'); + }); + + it('end of isoWeek', function () { + expect(moment('2016-01-10 23:59:59.999+01:00'), 'to be the end of isoWeek'); + }); + + it('end of month', function () { + expect(moment('2016-01-31 23:59:59.999+01:00'), 'to be the end of month'); + }); + + it('end of quarter', function () { + expect(moment('2016-03-31 23:59:59.999+02:00'), 'to be the end of quarter'); + }); + + it('end of year', function () { + expect(moment('2016-12-31 23:59:59.999+01:00'), 'to be the end of year'); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-02'), 'to be the end of year'); + }, + 'to error with', + 'expected moment(2016-01-02T00:00:00.000+01:00) ' + + 'to be the end of year\n' + + '\n' + + '-moment(2016-01-02T00:00:00.000+01:00)\n' + + '+moment(2016-12-31T23:59:59.999+01:00)' + ); + }); + + it('does not mutate the moment objects', function () { + var endOfYear = moment('2016-12-31T23:59:59.999+01:00'); + expect(endOfYear, 'to be the end of year'); + expect(endOfYear.format(), 'to be', '2016-12-31T23:59:59+01:00'); + }); }); - it('does not mutate the moment objects when checking for equality against the end of a unit of time', function () { - var moment1 = moment(1000); - var moment1Formartted = moment1.format(); - expect(moment1, 'not to be the end of day'); - expect(moment1Formartted, 'to equal', moment1.format()); + describe('not to be the end of', function () { + it('passes if the passed moment is not end of the unit of time provided', function () { + expect(moment('2016-01-01 00:00:01.002+01:00'), 'not to be the end of second'); + }); + + it('throws the correct error if the assertion fails', function () { + expect( + function () { + expect(moment('2016-01-02T23:59:59.999+01:00'), 'not to be the end of day'); + }, + 'to error with', + 'expected moment(2016-01-02T23:59:59.999+01:00) ' + + 'not to be the end of day' + ); + }); }); }); From b4fa55aad276845ffe7d1eb2422a9dfd67e7937e Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 09:14:29 +0200 Subject: [PATCH 02/35] Remove unneeded sinon devDependency [ci skip] --- package.json | 1 - test/unexpected-moment.spec.js | 11 +---------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/package.json b/package.json index c3eb918..39f9883 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,6 @@ "istanbul": "^0.4.0", "mocha": "^2.3.3", "moment-timezone": "^0.4.1", - "sinon": "^1.17.0", "unexpected": "^10.14.2", "unexpected-documentation-site-generator": "^4.0.0", "unexpected-markdown": "^1.4.0" diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index c6ece0c..2cd506a 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -1,5 +1,4 @@ -var sinon = require('sinon'), - moment = require('moment-timezone'), +var moment = require('moment-timezone'), unexpected = require('unexpected'), unexpectedMoment = require('../lib/unexpected-moment'); @@ -7,19 +6,11 @@ describe('unexpected-moment', function () { var expect = unexpected.clone() .use(unexpectedMoment); - var clock; - before(function () { - clock = sinon.useFakeTimers(); - moment.tz.add('Europe/Copenhagen|CET CEST|-10 -20|0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-2azC0 Tz0 VuO0 60q0 WM0 1fA0 1cM0 1cM0 1cM0 S00 1HA0 Nc0 1C00 Dc0 1Nc0 Ao0 1h5A0 1a00 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00'); moment.tz.setDefault('Europe/Copenhagen'); }); - after(function () { - clock.restore(); - }); - describe('to be a moment', function () { it('passes if the subject is a moment instance', function () { expect(moment(), 'to be a moment'); From 0ebd156481e3fa28d80e51d3c9f6daaf636b172c Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 09:16:51 +0200 Subject: [PATCH 03/35] Fix: do not show a diff for 'not to equal' --- lib/unexpected-moment.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index c8c4835..70c0d34 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -86,7 +86,9 @@ }; expect.fail({ diff: function (output, diff, inspect, equal) { - return diff(subject, value); + if (!expect.flags.not) { + return diff(subject, value); + } } }); } From dcca04ef21efeb8ac8abea828b499430b638e285 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 09:30:48 +0200 Subject: [PATCH 04/35] Expect moment strings to be quoted in error and diff outputs Presents a breaking change for users asserting against the error outputs. --- test/unexpected-moment.spec.js | 102 +++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 44 deletions(-) diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 2cd506a..034d7ad 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -31,7 +31,7 @@ describe('unexpected-moment', function () { it('throws the correct error if the subject is a moment instance', function () { expect(function () { expect(moment('2016-01-01'), 'not to be a moment'); - }, 'to error with', 'expected moment(2016-01-01T00:00:00.000+01:00) not to be a moment'); + }, 'to error with', 'expected moment(\'2016-01-01T00:00:00.000+01:00\') not to be a moment'); }); }); @@ -59,11 +59,11 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to equal', moment('2016-01-02')); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + - 'to equal moment(2016-01-02T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + 'to equal moment(\'2016-01-02T00:00:00.000+01:00\')\n' + '\n' + - '-moment(2016-01-01T00:00:00.000+01:00)\n' + - '+moment(2016-01-02T00:00:00.000+01:00)' + '-moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + '+moment(\'2016-01-02T00:00:00.000+01:00\')' ); }); @@ -85,11 +85,11 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to equal', moment('2016-01-02'), 'in milliseconds'); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + - 'to equal moment(2016-01-02T00:00:00.000+01:00) in milliseconds\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + 'to equal moment(\'2016-01-02T00:00:00.000+01:00\') in milliseconds\n' + '\n' + - '-moment(2016-01-01T00:00:00.000+01:00)\n' + - '+moment(2016-01-02T00:00:00.000+01:00)' + '-moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + '+moment(\'2016-01-02T00:00:00.000+01:00\')' ); }); }); @@ -105,8 +105,8 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'not to equal', moment('2016-01-01')); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + - 'not to equal moment(2016-01-01T00:00:00.000+01:00)' + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + 'not to equal moment(\'2016-01-01T00:00:00.000+01:00\')' ); }); }); @@ -168,13 +168,27 @@ describe('unexpected-moment', function () { }, 'to error'); }); + it('throws the correct error if the assertion fails for a moment value', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', moment('2016-01-02')); + }, + 'to error with', + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + 'to satisfy moment(\'2016-01-02T00:00:00.000+01:00\')\n' + + '\n' + + '-moment(\'2016-01-01T00:00:00.000+01.00\')\n' + + '+moment(\'2016-01-02T00:00:00.000+01.00\')' + ); + }); + it('throws the correct error if the assertion fails for a string value', function () { expect( function () { expect(moment('2016-01-01'), 'to satisfy', '2016-01-02'); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy \'2016-01-02\'\n' + '\n' + '-2016-01-01\n' + @@ -188,7 +202,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to satisfy', [2016, 0, 2]); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy \'2016-01-02\'\n' + '\n' + '-[ 2016, 0, 1 ]\n' + @@ -202,7 +216,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to satisfy', { year: 2016, month: 0, date: 2, minute: 10, millisecond: 3 }); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy \'2016-01-02\'\n' + '\n' + '-{ year: 2016, month: 0, day: 1, minute: 0, second: 0, millisecond: 0 }\n' + @@ -216,7 +230,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to satisfy', 1451689200); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy \'2016-01-02\'\n' + '\n' + '-1451602800\n' + @@ -230,7 +244,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to satisfy', 1451689200000); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy \'2016-01-02\'\n' + '\n' + '-1451602800000\n' + @@ -244,7 +258,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to satisfy', new Date('2016-01-02')); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy \'2016-01-02\'\n' + '\n' + '-new Date(\'2016-01-01\')\n' + @@ -264,8 +278,8 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'not to satisfy', moment('2016-01-01')); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + - 'not to satisfy moment(2016-01-01T00:00:00.000+01:00)' + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + 'not to satisfy moment(\'2016-01-01T00:00:00.000+01:00\')' ); }); }); @@ -281,7 +295,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-03'), 'to be before', new Date('2016-01-02 00:00:00')); }, 'to error with', - 'expected moment(2016-01-03T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-03T00:00:00.000+01:00\')\n' + 'to be before new Date(\'Fri, 01 Jan 2016 23:00:00 GMT\')' ); }); @@ -298,7 +312,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'not to be before', '2016-01-02 00:00:00'); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'not to be before \'2016-01-02 00:00:00\'' ); }); @@ -315,7 +329,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to be after', [2016, 0, 2, 0, 0, 0]); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00) ' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + 'to be after [ 2016, 0, 2, 0, 0, 0 ]' ); }); @@ -332,7 +346,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-02'), 'not to be after', { year: 2016, month: 0, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }); }, 'to error with', - 'expected moment(2016-01-02T00:00:00.000+01:00) ' + + 'expected moment(\'2016-01-02T00:00:00.000+01:00\') ' + 'not to be after\n' + '{\n' + ' year: 2016, month: 0, day: 1, hour: 0, minute: 0, second: 0,\n' + @@ -357,7 +371,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to be same or before', 1451602800); // 1451602800 -> new Date('2016-01-01 00:00:00') }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00) ' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + 'to be same or before 1451602800' ); }); @@ -374,7 +388,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'not to be same or before', 1451689200000); // 1451689200000 -> new Date('2016-01-02 00:00:00') }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'not to be same or before 1451689200000' ); }); @@ -395,7 +409,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to be same or after', new Date('2016-01-02 00:00:00')); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to be same or after new Date(\'Fri, 01 Jan 2016 23:00:00 GMT\')' ); }); @@ -412,8 +426,8 @@ describe('unexpected-moment', function () { expect(moment('2016-01-02'), 'not to be same or after', moment('2016-01-01 00:00:00')); }, 'to error with', - 'expected moment(2016-01-02T00:00:00.000+01:00)\n' + - 'not to be same or after moment(2016-01-01T00:00:00.000+01:00)' + 'expected moment(\'2016-01-02T00:00:00.000+01:00\')\n' + + 'not to be same or after moment(\'2016-01-01T00:00:00.000+01:00\')' ); }); }); @@ -429,8 +443,8 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to be between', moment('2016-01-02'), moment('2016-01-03')); }, 'to error with', - 'expected moment(2016-01-01T00:00:00.000+01:00)\n' + - 'to be between moment(2016-01-02T00:00:00.000+01:00) and moment(2016-01-03T00:00:00.000+01:00)' + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + 'to be between moment(\'2016-01-02T00:00:00.000+01:00\') and moment(\'2016-01-03T00:00:00.000+01:00\')' ); }); }); @@ -446,8 +460,8 @@ describe('unexpected-moment', function () { expect(moment('2016-01-02'), 'not to be between', moment('2016-01-01'), moment('2016-01-03')); }, 'to error with', - 'expected moment(2016-01-02T00:00:00.000+01:00)\n' + - 'not to be between moment(2016-01-01T00:00:00.000+01:00) and moment(2016-01-03T00:00:00.000+01:00)' + 'expected moment(\'2016-01-02T00:00:00.000+01:00\')\n' + + 'not to be between moment(\'2016-01-01T00:00:00.000+01:00\') and moment(\'2016-01-03T00:00:00.000+01:00\')' ); }); }); @@ -475,7 +489,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-02'), 'to be inclusively between', new Date('2016-01-01 00:00:00'), new Date('2016-01-01 12:00:00')); }, 'to error with', - 'expected moment(2016-01-02T00:00:00.000+01:00)\n' + + 'expected moment(\'2016-01-02T00:00:00.000+01:00\')\n' + 'to be inclusively between new Date(\'Thu, 31 Dec 2015 23:00:00 GMT\') ' + 'and new Date(\'Fri, 01 Jan 2016 11:00:00 GMT\')' ); @@ -493,9 +507,9 @@ describe('unexpected-moment', function () { expect(moment('2016-01-02'), 'not to be inclusively between', moment('2016-01-02'), moment('2016-01-02')); }, 'to error with', - 'expected moment(2016-01-02T00:00:00.000+01:00)\n' + - 'not to be inclusively between moment(2016-01-02T00:00:00.000+01:00) ' + - 'and moment(2016-01-02T00:00:00.000+01:00)' + 'expected moment(\'2016-01-02T00:00:00.000+01:00\')\n' + + 'not to be inclusively between moment(\'2016-01-02T00:00:00.000+01:00\') ' + + 'and moment(\'2016-01-02T00:00:00.000+01:00\')' ); }); }); @@ -553,11 +567,11 @@ describe('unexpected-moment', function () { expect(moment('2016-01-02'), 'to be the start of year'); }, 'to error with', - 'expected moment(2016-01-02T00:00:00.000+01:00) ' + + 'expected moment(\'2016-01-02T00:00:00.000+01:00\') ' + 'to be the start of year\n' + '\n' + - '-moment(2016-01-02T00:00:00.000+01:00)\n' + - '+moment(2016-01-01T00:00:00.000+01:00)' + '-moment(\'2016-01-02T00:00:00.000+01:00\')\n' + + '+moment(\'2016-01-01T00:00:00.000+01:00\')' ); }); @@ -579,7 +593,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-02'), 'not to be the start of day'); }, 'to error with', - 'expected moment(2016-01-02T00:00:00.000+01:00) ' + + 'expected moment(\'2016-01-02T00:00:00.000+01:00\') ' + 'not to be the start of day' ); }); @@ -628,11 +642,11 @@ describe('unexpected-moment', function () { expect(moment('2016-01-02'), 'to be the end of year'); }, 'to error with', - 'expected moment(2016-01-02T00:00:00.000+01:00) ' + + 'expected moment(\'2016-01-02T00:00:00.000+01:00\') ' + 'to be the end of year\n' + '\n' + - '-moment(2016-01-02T00:00:00.000+01:00)\n' + - '+moment(2016-12-31T23:59:59.999+01:00)' + '-moment(\'2016-01-02T00:00:00.000+01:00\')\n' + + '+moment(\'2016-12-31T23:59:59.999+01:00\')' ); }); @@ -654,7 +668,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-02T23:59:59.999+01:00'), 'not to be the end of day'); }, 'to error with', - 'expected moment(2016-01-02T23:59:59.999+01:00) ' + + 'expected moment(\'2016-01-02T23:59:59.999+01:00\') ' + 'not to be the end of day' ); }); From e12cc6c8b3b2f52fd323b4058ec33d6edb437a98 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 13:15:02 +0200 Subject: [PATCH 05/35] Quote moment strings in error outputs and diffs Makes it easier to copy moments from the error outputs into code. --- lib/unexpected-moment.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index 70c0d34..eee4c85 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -24,11 +24,11 @@ equal: function (a, b) { return a.isSame(b); }, - inspect: function (value, depth, output) { + inspect: function (value, depth, output, inspect) { var name = value.isUtc() ? 'moment.utc' : 'moment'; output.jsFunctionName(name) .text('(') - .jsPrimitive(value.format(outputFormat)) + .jsPrimitive(inspect(value.format(outputFormat))) .text(')'); }, diff: function (actual, expected, output, diff) { From a9f0fd86a94bd6992e36b3d9bc60a47c3fb005e4 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 13:19:39 +0200 Subject: [PATCH 06/35] Use instanceof to identify moment objects Ability to use instanceof was added to moment in version 2.10.7 --- lib/unexpected-moment.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index eee4c85..faaabb0 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -19,7 +19,7 @@ name: 'moment', base: 'object', identify: function (value) { - return value && moment.isMoment(value); + return value && value instanceof moment; }, equal: function (a, b) { return a.isSame(b); From 13dda22a94b67f489e8c1400fe93e32eb59e4c81 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 14:37:40 +0200 Subject: [PATCH 07/35] Make assertion optional for 'when formatted with' --- lib/unexpected-moment.js | 2 +- test/unexpected-moment.spec.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index faaabb0..f656ec3 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -69,7 +69,7 @@ return expect(subject.isBetween(from, to, null, inclusivity), '[not] to be true'); }); - expect.addAssertion(' when formatted with ', function (expect, subject, format) { + expect.addAssertion(' when formatted with ', function (expect, subject, format) { return expect.shift(subject.format(format)); }); diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 034d7ad..e2c765b 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -522,6 +522,13 @@ describe('unexpected-moment', function () { it('formats a moment with the default format if called with an empty string format', function () { expect(moment.utc(0), 'when formatted with', '', 'to equal', '1970-01-01T00:00:00Z'); }); + + it('returns the formatted moment as the fulfilment value if an assertion is not provide', function () { + expect(moment(0), 'when formatted with', '') + .then(function (formatted) { + expect(formatted, 'to equal', '1970-01-01T00:00:00+01:00'); + }); + }); }); describe('to be the start of', function () { From e252108bffb9580b596914a158c8bbb4713d7183 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 15:16:51 +0200 Subject: [PATCH 08/35] when formatted with: make 'when' optional --- lib/unexpected-moment.js | 2 +- test/unexpected-moment.spec.js | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index f656ec3..9d9bef7 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -69,7 +69,7 @@ return expect(subject.isBetween(from, to, null, inclusivity), '[not] to be true'); }); - expect.addAssertion(' when formatted with ', function (expect, subject, format) { + expect.addAssertion(' [when] formatted with ', function (expect, subject, format) { return expect.shift(subject.format(format)); }); diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index e2c765b..062a333 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -523,12 +523,16 @@ describe('unexpected-moment', function () { expect(moment.utc(0), 'when formatted with', '', 'to equal', '1970-01-01T00:00:00Z'); }); - it('returns the formatted moment as the fulfilment value if an assertion is not provide', function () { - expect(moment(0), 'when formatted with', '') + it('returns the formatted moment as the fulfilment value if an assertion is not provided', function () { + expect(moment(0), 'when formatted with', 'YYYYMMDD') .then(function (formatted) { - expect(formatted, 'to equal', '1970-01-01T00:00:00+01:00'); + expect(formatted, 'to equal', '19700101'); }); }); + + it('also works without the \'when\'', function () { + expect(moment(0), 'formatted with', 'YYYYMMDD', 'to equal', '19700101'); + }); }); describe('to be the start of', function () { From 6839f89a5f1772760ba1172028673035d6148f39 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 15:37:36 +0200 Subject: [PATCH 09/35] Add 'when formatted' assertion --- lib/unexpected-moment.js | 8 +++++++- test/unexpected-moment.spec.js | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index 9d9bef7..27dfb1d 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -69,7 +69,13 @@ return expect(subject.isBetween(from, to, null, inclusivity), '[not] to be true'); }); - expect.addAssertion(' [when] formatted with ', function (expect, subject, format) { + expect.addAssertion([ + ' [when] formatted ', + ' [when] formatted with ' + ], function (expect, subject, format) { + if (expect.testDescription.indexOf('with') < 0) { + format = undefined; + } return expect.shift(subject.format(format)); }); diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 062a333..648fa71 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -514,6 +514,23 @@ describe('unexpected-moment', function () { }); }); + describe('when formatted', function () { + it('formats a moment with the default format and delegates to the next assertion', function () { + expect(moment(1000), 'when formatted to equal', '1970-01-01T01:00:01+01:00'); + }); + + it('returns the formatted moment as the fulfilment value if an assertion is not provided', function () { + expect(moment(0), 'when formatted') + .then(function (formatted) { + expect(formatted, 'to equal', '1970-01-01T00:00:00+01:00'); + }); + }); + + it('also works without the \'when\'', function () { + expect(moment(0), 'formatted', 'to equal', '1970-01-01T01:00:00+01:00'); + }); + }); + describe('when formatted with', function () { it('formats a moment with the provided format and delegates to the next assertion', function () { expect(moment(1), 'when formatted with', 'YYYY', 'to be', '1970'); From 4dd5f418b533119429832f3cba698f83da127afb Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 15:42:14 +0200 Subject: [PATCH 10/35] Add documentation for 'when formatted' --- .../assertions/moment/when-formatted.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 documentation/assertions/moment/when-formatted.md diff --git a/documentation/assertions/moment/when-formatted.md b/documentation/assertions/moment/when-formatted.md new file mode 100644 index 0000000..a2912f1 --- /dev/null +++ b/documentation/assertions/moment/when-formatted.md @@ -0,0 +1,19 @@ +Format a moment instance using [the default ISO8601 format](http://momentjs.com/docs/#/displaying/format/), then delegate the formatted value to another assertion. + +```js +expect(moment('2015-11-02'), 'when formatted to be', '2015-11-02T00:00:00+01:00'); +``` + +When the assertion fails you'll get this output: + +```js +expect(moment('2015-11-02'), 'when formatted to be', '2015'); +``` + +```output +expected moment(2015-11-02T00:00:00.000+01:00) +when formatted with 'YYYYMMDD' to be '2015' + +-2015-11-02T00:00:00+01:00 ++2015 +``` From 5cd1a9871e16a0da18e58a97675f2e36040c00a6 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 16:22:52 +0200 Subject: [PATCH 11/35] Improve diff output for 'to equal' --- lib/unexpected-moment.js | 12 +++++------- test/unexpected-moment.spec.js | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index 27dfb1d..10a6e19 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -28,11 +28,11 @@ var name = value.isUtc() ? 'moment.utc' : 'moment'; output.jsFunctionName(name) .text('(') - .jsPrimitive(inspect(value.format(outputFormat))) + .jsString(inspect(value.format(outputFormat))) .text(')'); }, - diff: function (actual, expected, output, diff) { - return diff(actual.format(outputFormat), expected.format(outputFormat)); + diff: function (actual, expected, output, diff, inspect) { + return diff(inspect(actual).toString(), inspect(expected).toString()); } }); @@ -100,16 +100,14 @@ } }); - // TODO: error diff for this expect.addAssertion(' [not] to be the start of (second|minute|hour|day|week|isoWeek|month|quarter|year)', function (expect, subject) { var unitOfTime = expect.alternations[0]; - return expect(subject.isSame(subject.clone().startOf(unitOfTime)), '[not] to be true'); + return expect(subject, '[not] to equal', subject.clone().startOf(unitOfTime)); }); - // TODO: error diff for this expect.addAssertion(' [not] to be the end of (second|minute|hour|day|week|isoWeek|month|quarter|year)', function (expect, subject) { var unitOfTime = expect.alternations[0]; - return expect(subject.isSame(subject.clone().endOf(unitOfTime)), '[not] to be true'); + return expect(subject, '[not] to equal', subject.clone().endOf(unitOfTime)); }); // TODO: error diff for this diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 648fa71..a4f99d2 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -67,6 +67,20 @@ describe('unexpected-moment', function () { ); }); + it('throws the correct error if the assertion fails due to the value being in UTC', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to equal', moment.utc('2016-01-02')); + }, + 'to error with', + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + 'to equal moment.utc(\'2016-01-02T00:00:00.000+00:00\')\n' + + '\n' + + '-moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + '+moment.utc(\'2016-01-02T00:00:00.000+00:00\')' + ); + }); + it('throws the correct error if the value is not a moment object', function () { expect( function () { @@ -177,8 +191,8 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy moment(\'2016-01-02T00:00:00.000+01:00\')\n' + '\n' + - '-moment(\'2016-01-01T00:00:00.000+01.00\')\n' + - '+moment(\'2016-01-02T00:00:00.000+01.00\')' + '-moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + '+moment(\'2016-01-02T00:00:00.000+01:00\')' ); }); From 106acb5c7a075c10dcd03fbd79aaa6e6aa0dd2cb Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 19:03:55 +0200 Subject: [PATCH 12/35] Add diffs for 'to satisfy' --- lib/unexpected-moment.js | 137 ++++++++++++++++++++++++++------- test/unexpected-moment.spec.js | 130 +++++++++++++++++++++++-------- 2 files changed, 210 insertions(+), 57 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index 10a6e19..4aef837 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -36,6 +36,117 @@ } }); + expect.addAssertion(' [not] to equal ', function (expect, subject, value, label) { + // TODO: validate granularity? + var granularity = (label || '').replace(/^in /, '').replace(/s$/, ''); + + if (subject.isSame(value, granularity) === expect.flags.not) { + expect.argsOutput = function (output) { + output.appendInspected(value); + if (granularity) { + output.sp().text(label); + } + }; + return expect.fail({ + diff: function (output, diff, inspect, equal) { + if (!expect.flags.not) { + return diff(subject, value); + } + } + }); + } + }); + + expect.addAssertion(' to satisfy ', function (expect, subject, value) { + if (value instanceof moment) { + return expect(subject, 'to equal', value); + } + // TODO: use nested errorMode? + + // TODO: perhaps add a moment-like type that filters out invalid values in the first place + var originalSuppressDeprecationWarningsSetting = moment.suppressDeprecationWarnings; + moment.suppressDeprecationWarnings = true; + var valueAsMomentInstance = moment(value); + moment.suppressDeprecationWarnings = originalSuppressDeprecationWarningsSetting; + + if (!valueAsMomentInstance.isValid()) { + return expect.fail(); + } + + if (value instanceof Date) { + var subjectAsDate = subject.toDate(); + if (subjectAsDate.getTime() !== value.getTime()) { + return expect.fail({ + diff: function (output, diff, inspect, equal) { + return diff(subjectAsDate.toString(), value.toString()); + } + }); + } + return; + } + + if (Array.isArray(value)) { + if (!value.length) { + return expect.fail(); + } + return expect( + subject.toArray().slice(0, value.length), + 'to satisfy', + value + ); + } + + var valueType = typeof value; + if (valueType === 'object') { + var normalizedValue = {}; + var keys = Object.keys(value); + if (!keys.length) { + return expect.fail(); + } + for (var i = 0; i < keys.length; i++) { + var unit = keys[i]; + var normalizedUnit = moment.normalizeUnits(unit); + if (!normalizedUnit) { + expect.errorMode = 'nested'; + return expect.fail({ + message: function (output) { + output.nl() + .text('Unit \'') + .jsString(unit) + .text('\'') + .sp() + .text('is not a valid moment unit'); + } + }); + } + if (normalizedUnit === 'day') { + normalizedUnit = 'date'; // Urgh + } + if (normalizedUnit !== 'date') { + normalizedUnit += 's'; // Urgh + } + normalizedValue[normalizedUnit] = value[unit]; + } + return expect(subject.toObject(), 'to satisfy', normalizedValue); + } + + if (valueType === 'string') { + var desiredFormat = valueAsMomentInstance._f || outputFormat; + return expect(subject.format(desiredFormat), 'to satisfy', value); + } + + if (valueType === 'number') { + var subjectAsNumber = subject.valueOf(); + if (subjectAsNumber !== value) { + return expect.fail({ + diff: function (output, diff, inspect, equal) { + return diff(String(subjectAsNumber), String(value)); + } + }); + } + } + }); + expect.addAssertion(' [not] to be a moment', function (expect, subject) { return expect(moment.isMoment(subject), '[not] to be true'); }); @@ -79,27 +190,6 @@ return expect.shift(subject.format(format)); }); - expect.addAssertion(' [not] to equal ', function (expect, subject, value, label) { - // TODO: validate granularity? - var granularity = (label || '').replace(/^in /, '').replace(/s$/, ''); - - if (subject.isSame(value, granularity) === expect.flags.not) { - expect.argsOutput = function (output) { - output.appendInspected(value); - if (granularity) { - output.sp().text(label); - } - }; - expect.fail({ - diff: function (output, diff, inspect, equal) { - if (!expect.flags.not) { - return diff(subject, value); - } - } - }); - } - }); - expect.addAssertion(' [not] to be the start of (second|minute|hour|day|week|isoWeek|month|quarter|year)', function (expect, subject) { var unitOfTime = expect.alternations[0]; return expect(subject, '[not] to equal', subject.clone().startOf(unitOfTime)); @@ -109,11 +199,6 @@ var unitOfTime = expect.alternations[0]; return expect(subject, '[not] to equal', subject.clone().endOf(unitOfTime)); }); - - // TODO: error diff for this - expect.addAssertion(' to satisfy ', function (expect, subject, value) { - return expect(subject.isSame(value), 'to be true'); - }); } }; })); diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index a4f99d2..48bab9d 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -139,14 +139,26 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to satisfy', '2016-01-01T00:00:00+01:00'); }); + it('passes if passed a number as the expected value', function () { + expect(moment(123456), 'to satisfy', 123456); + }); + + it('passes if passed a Date as the expected value', function () { + expect(moment('2016-01-01 00:00:01'), 'to satisfy', new Date('2016-01-01 00:00:01')); + }); + it('passes if passed an array as the expected value', function () { expect(moment('2016-01-01'), 'to satisfy', [2016, 0, 1, 0, 0, 0, 0]); }); - it('passes if passed an object as the expected value', function () { + it('passes if passed an object with a \'day\' unit as the expected value', function () { expect(moment('2016-01-01'), 'to satisfy', { year: 2016, month: 0, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }); }); + it('passes if passed an object with a \'date\' unit as the expected value', function () { + expect(moment('2016-01-01'), 'to satisfy', { year: 2016, month: 0, date: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }); + }); + it('passes with a partial object', function () { expect(moment('2016-01-01'), 'to satisfy', { year: 2016, date: 1, seconds: 0 }); }); @@ -157,28 +169,31 @@ describe('unexpected-moment', function () { }, 'to error'); }); + it('throws if passed an empty array as the value', function () { + expect(function () { + expect(moment('2016-01-01'), 'to satisfy', []); + }, 'to error'); + }); + it('throws if passed an objet with unknown keys', function () { expect(function () { - expect(moment('2016-01-01'), 'not to satisfy', { + expect(moment('2016-01-01'), 'to satisfy', { years: 2016, mints: 0 }); }, 'to error'); }); - it('passes if passed a number as the expected value', function () { - expect(moment(123456), 'to satisfy', 123456); - }); - - - it('passes if passed a Date as the expected value', function () { - expect(moment('2016-01-01 00:00:01'), 'to satisfy', new Date('2016-01-01 00:00:01')); + it('throws if passed an empty string as the value', function () { + expect(function () { + expect(moment('2016-01-01'), 'to satisfy', ''); + }, 'to error'); }); // see: https://github.com/moment/moment/issues/2633 it('throws for utc moments if the expected value is not a moment instance even though it represents the same moment', function () { expect(function () { - expect(moment.utc('2015-01-01T00:00:00+00:00'), 'not to satisfy', [2015, 0, 1, 0, 0, 0, 0]); + expect(moment.utc('2015-01-01T00:00:00+00:00'), 'to satisfy', [2015, 0, 1, 0, 0, 0, 0]); }, 'to error'); }); @@ -202,7 +217,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to satisfy', '2016-01-02'); }, 'to error with', - 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + 'to satisfy \'2016-01-02\'\n' + '\n' + '-2016-01-01\n' + @@ -216,11 +231,58 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to satisfy', [2016, 0, 2]); }, 'to error with', - 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - 'to satisfy \'2016-01-02\'\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + + 'to satisfy [ 2016, 0, 2 ]\n' + '\n' + - '-[ 2016, 0, 1 ]\n' + - '+[ 2016, 0, 2 ]' + '[\n' + + ' 2016,\n' + + ' 0,\n' + + ' 1 // should equal 2\n' + + ']' + ); + }); + + it('throws the correct error if the assertion fails for an empty array value', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', []); + }, + 'to error with', + 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + + 'to satisfy []' + ); + }); + + it('throws the correct error if the assertion fails for an empty object value', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', {}); + }, + 'to error with', + 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + + 'to satisfy {}' + ); + }); + + it('throws the correct error if the assertion fails for an empty string value', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', ''); + }, + 'to error with', + 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + + 'to satisfy \'\'' + ); + }); + + it('throws the correct error if the assertion fails for due to a bad string value', function () { + expect( + function () { + expect(moment('2016-01-01'), 'to satisfy', 'not a date'); + }, + 'to error with', + 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + + 'to satisfy \'not a date\'' ); }); @@ -231,35 +293,41 @@ describe('unexpected-moment', function () { }, 'to error with', 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - 'to satisfy \'2016-01-02\'\n' + + 'to satisfy { year: 2016, month: 0, date: 2, minute: 10, millisecond: 3 }\n' + '\n' + - '-{ year: 2016, month: 0, day: 1, minute: 0, second: 0, millisecond: 0 }\n' + - '+{ year: 2016, month: 0, date: 2, minute: 10, millisecond: 3 }' + '{\n' + + ' years: 2016,\n' + + ' months: 0,\n' + + ' date: 1, // should equal 2\n' + + ' hours: 0,\n' + + ' minutes: 0, // should equal 10\n' + + ' seconds: 0,\n' + + ' milliseconds: 0 // should equal 3\n' + + '}' ); }); - it('throws the correct error if the assertion fails for a unix timestamp (seconds)', function () { + it('throws the correct error if the assertion fails due to an object having a bad unit', function () { expect( function () { - expect(moment('2016-01-01'), 'to satisfy', 1451689200); + expect(moment('2016-01-01'), 'to satisfy', { year: 2016, month: 0, date: 1, minutez: 0, millisecond: 0 }); }, 'to error with', 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - 'to satisfy \'2016-01-02\'\n' + - '\n' + - '-1451602800\n' + - '+1451689200' + 'to satisfy { year: 2016, month: 0, date: 1, minutez: 0, millisecond: 0 }\n' + + ' \n' + + ' Unit \'minutez\' is not a valid moment unit' ); }); - it('throws the correct error if the assertion fails for a unix timestamp (milliseconds)', function () { + it('throws the correct error if the assertion fails for a number (milliseconds)', function () { expect( function () { expect(moment('2016-01-01'), 'to satisfy', 1451689200000); }, 'to error with', - 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - 'to satisfy \'2016-01-02\'\n' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + + 'to satisfy 1451689200000\n' + '\n' + '-1451602800000\n' + '+1451689200000' @@ -273,10 +341,10 @@ describe('unexpected-moment', function () { }, 'to error with', 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - 'to satisfy \'2016-01-02\'\n' + + 'to satisfy new Date(\'Sat, 02 Jan 2016 00:00:00 GMT\')\n' + '\n' + - '-new Date(\'2016-01-01\')\n' + - '+new Date(\'2016-01-02\')' + '-new Date(\'Fri, 01 Jan 2016 00:00:00 GMT\')\n' + + '+new Date(\'Sat, 02 Jan 2016 00:00:00 GMT\')' ); }); }); @@ -343,7 +411,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to be after', [2016, 0, 2, 0, 0, 0]); }, 'to error with', - 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + + 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to be after [ 2016, 0, 2, 0, 0, 0 ]' ); }); From 57ea28a1388b132abdbc3cd7e05ea02bfd8bee4c Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Mon, 27 Jun 2016 23:48:59 +0200 Subject: [PATCH 13/35] Add a new moment-date type Inherits from Unexpected's 'date' type but inspects/diffs dates in local time. --- lib/unexpected-moment.js | 27 ++++++++++++++++++--------- test/unexpected-moment.spec.js | 14 +++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index 4aef837..a6aabf5 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -36,6 +36,23 @@ } }); + expect.addType({ + name: 'moment-date', + base: 'date', + identify: function (value) { + return value && value instanceof Date; + }, + inspect: function (value, depth, output, inspect) { + output.jsFunctionName('new Date') + .text('(') + .jsString(inspect(value.toString())) + .text(')'); + }, + diff: function (actual, expected, output, diff, inspect) { + return diff(inspect(actual).toString(), inspect(expected).toString()); + } + }); + expect.addAssertion(' [not] to equal ', function (expect, subject, value, label) { // TODO: validate granularity? var granularity = (label || '').replace(/^in /, '').replace(/s$/, ''); @@ -74,15 +91,7 @@ } if (value instanceof Date) { - var subjectAsDate = subject.toDate(); - if (subjectAsDate.getTime() !== value.getTime()) { - return expect.fail({ - diff: function (output, diff, inspect, equal) { - return diff(subjectAsDate.toString(), value.toString()); - } - }); - } - return; + return expect(subject.toDate(), 'to equal', value); } if (Array.isArray(value)) { diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 48bab9d..03fbb64 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -341,10 +341,10 @@ describe('unexpected-moment', function () { }, 'to error with', 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - 'to satisfy new Date(\'Sat, 02 Jan 2016 00:00:00 GMT\')\n' + + 'to satisfy new Date(\'Sat Jan 02 2016 01:00:00 GMT+0100 (CET)\')\n' + '\n' + - '-new Date(\'Fri, 01 Jan 2016 00:00:00 GMT\')\n' + - '+new Date(\'Sat, 02 Jan 2016 00:00:00 GMT\')' + '-new Date(\'Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\')\n' + + '+new Date(\'Sat Jan 02 2016 01:00:00 GMT+0100 (CET)\')' ); }); }); @@ -378,7 +378,7 @@ describe('unexpected-moment', function () { }, 'to error with', 'expected moment(\'2016-01-03T00:00:00.000+01:00\')\n' + - 'to be before new Date(\'Fri, 01 Jan 2016 23:00:00 GMT\')' + 'to be before new Date(\'Sat Jan 02 2016 00:00:00 GMT+0100 (CET)\')' ); }); }); @@ -492,7 +492,7 @@ describe('unexpected-moment', function () { }, 'to error with', 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - 'to be same or after new Date(\'Fri, 01 Jan 2016 23:00:00 GMT\')' + 'to be same or after new Date(\'Sat Jan 02 2016 00:00:00 GMT+0100 (CET)\')' ); }); }); @@ -572,8 +572,8 @@ describe('unexpected-moment', function () { }, 'to error with', 'expected moment(\'2016-01-02T00:00:00.000+01:00\')\n' + - 'to be inclusively between new Date(\'Thu, 31 Dec 2015 23:00:00 GMT\') ' + - 'and new Date(\'Fri, 01 Jan 2016 11:00:00 GMT\')' + 'to be inclusively between new Date(\'Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\') ' + + 'and new Date(\'Fri Jan 01 2016 12:00:00 GMT+0100 (CET)\')' ); }); }); From e4698d43429affd81e48b59eff7705dd3988db27 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Tue, 28 Jun 2016 00:04:22 +0200 Subject: [PATCH 14/35] Update test to reflect fixed bug Ref: https://github.com/moment/moment/issues/2633 --- test/unexpected-moment.spec.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 03fbb64..cb88d32 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -191,10 +191,25 @@ describe('unexpected-moment', function () { }); // see: https://github.com/moment/moment/issues/2633 - it('throws for utc moments if the expected value is not a moment instance even though it represents the same moment', function () { + it('passes for utc moments if passed a value that is not a moment instance or has no timezone information', function () { + // The argument here is that as a moment object, the timezone is relevant. + // But as an array|object|number there is no timezone information; + // therefore, the two represent a similar moment in time. + expect(function () { + expect(moment.utc('2015-01-01T00:00:00+00:00'), 'to satisfy', moment([2015, 0, 1, 0, 0, 0, 0])); + }, 'to error'); + // but... expect(function () { expect(moment.utc('2015-01-01T00:00:00+00:00'), 'to satisfy', [2015, 0, 1, 0, 0, 0, 0]); + }, 'not to error'); + // but for a string with with timezone information... + expect(function () { + expect(moment.utc('2015-01-01T00:00:00+00:00'), 'to satisfy', '2015-01-01T00:00:00+01:00'); }, 'to error'); + // and a string with no timezone information... + expect(function () { + expect(moment.utc('2015-01-01T00:00:00+00:00'), 'to satisfy', '2015-01-01T00:00:00'); + }, 'not to error'); }); it('throws the correct error if the assertion fails for a moment value', function () { From 980367f3e2f7dee0107ddc79f7e638b7fcc308b0 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Tue, 28 Jun 2016 00:19:45 +0200 Subject: [PATCH 15/35] to satisfy: add test for partial array --- test/unexpected-moment.spec.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index cb88d32..3546841 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -159,10 +159,14 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to satisfy', { year: 2016, month: 0, date: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }); }); - it('passes with a partial object', function () { + it('passes with a partial object as the value', function () { expect(moment('2016-01-01'), 'to satisfy', { year: 2016, date: 1, seconds: 0 }); }); + it('passes with a partial array as the value', function () { + expect(moment('2016-01-01'), 'to satisfy', [ 2016 ]); + }); + it('throws if passed an empty object as the value', function () { expect(function () { expect(moment('2016-01-01'), 'to satisfy', {}); From e420cc3a3dc1654a597ac0275b151c966f2ec8da Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Tue, 28 Jun 2016 00:23:47 +0200 Subject: [PATCH 16/35] Set timezone for travis builds --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index c76b348..63d4be6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,5 +5,7 @@ node_js: - "4" - "5" - "6" +env: + - TZ="Europe/Copenhagen" before_install: "npm install -g npm && npm cache clean" script: "npm run-script travis" From 026f0b5d3bafbed5834f6f8b2f6882ca39750428 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Tue, 28 Jun 2016 00:26:07 +0200 Subject: [PATCH 17/35] Update examples in documentation --- documentation/assertions/any/to-be-a-moment.md | 2 +- documentation/assertions/moment/to-be-after.md | 4 ++-- documentation/assertions/moment/to-be-before.md | 4 ++-- documentation/assertions/moment/to-be-between.md | 4 ++-- .../assertions/moment/to-be-same-or-after.md | 4 ++-- .../assertions/moment/to-be-same-or-before.md | 4 ++-- documentation/assertions/moment/to-be-the-end-of.md | 5 ++++- .../assertions/moment/to-be-the-start-of.md | 5 ++++- documentation/assertions/moment/to-equal.md | 8 ++++---- documentation/assertions/moment/to-satisfy.md | 12 +++++++++++- .../assertions/moment/when-formatted-with.md | 2 +- documentation/assertions/moment/when-formatted.md | 3 +-- documentation/index.md | 8 ++++---- 13 files changed, 40 insertions(+), 25 deletions(-) diff --git a/documentation/assertions/any/to-be-a-moment.md b/documentation/assertions/any/to-be-a-moment.md index cf3b373..60312a8 100644 --- a/documentation/assertions/any/to-be-a-moment.md +++ b/documentation/assertions/any/to-be-a-moment.md @@ -12,5 +12,5 @@ expect(new Date('2015-01-01'), 'to be a moment'); ``` ```output -expected new Date('Thu, 01 Jan 2015 00:00:00 GMT') to be a moment +expected new Date('Thu Jan 01 2015 01:00:00 GMT+0100 (CET)') to be a moment ``` diff --git a/documentation/assertions/moment/to-be-after.md b/documentation/assertions/moment/to-be-after.md index 45e539f..1d12130 100644 --- a/documentation/assertions/moment/to-be-after.md +++ b/documentation/assertions/moment/to-be-after.md @@ -13,6 +13,6 @@ expect(moment('2015-02-03'), 'to be after', moment('2015-04-03')); ``` ```output -expected moment(2015-02-03T00:00:00.000+01:00) -to be after moment(2015-04-03T00:00:00.000+02:00) +expected moment('2015-02-03T00:00:00.000+01:00') +to be after moment('2015-04-03T00:00:00.000+02:00') ``` diff --git a/documentation/assertions/moment/to-be-before.md b/documentation/assertions/moment/to-be-before.md index d35325a..3b17c91 100644 --- a/documentation/assertions/moment/to-be-before.md +++ b/documentation/assertions/moment/to-be-before.md @@ -13,6 +13,6 @@ expect(moment('2015-04-03'), 'to be before', moment('2015-02-03')); ``` ```output -expected moment(2015-04-03T00:00:00.000+02:00) -to be before moment(2015-02-03T00:00:00.000+01:00) +expected moment('2015-04-03T00:00:00.000+02:00') +to be before moment('2015-02-03T00:00:00.000+01:00') ``` diff --git a/documentation/assertions/moment/to-be-between.md b/documentation/assertions/moment/to-be-between.md index 7e292f8..82a74b6 100644 --- a/documentation/assertions/moment/to-be-between.md +++ b/documentation/assertions/moment/to-be-between.md @@ -17,6 +17,6 @@ expect(moment('2015-01-01'), 'to be between', moment('2015-02-01'), moment('2015 ``` ```output -expected moment(2015-01-01T00:00:00.000+01:00) -to be between moment(2015-02-01T00:00:00.000+01:00) and moment(2015-03-01T00:00:00.000+01:00) +expected moment('2015-01-01T00:00:00.000+01:00') +to be between moment('2015-02-01T00:00:00.000+01:00') and moment('2015-03-01T00:00:00.000+01:00') ``` diff --git a/documentation/assertions/moment/to-be-same-or-after.md b/documentation/assertions/moment/to-be-same-or-after.md index f2a9942..fe74f3f 100644 --- a/documentation/assertions/moment/to-be-same-or-after.md +++ b/documentation/assertions/moment/to-be-same-or-after.md @@ -13,6 +13,6 @@ expect(moment('2015-02-03'), 'to be same or after', moment('2015-04-03')); ``` ```output -expected moment(2015-02-03T00:00:00.000+01:00) -to be same or after moment(2015-04-03T00:00:00.000+02:00) +expected moment('2015-02-03T00:00:00.000+01:00') +to be same or after moment('2015-04-03T00:00:00.000+02:00') ``` diff --git a/documentation/assertions/moment/to-be-same-or-before.md b/documentation/assertions/moment/to-be-same-or-before.md index 9e9a7fd..5fdc51f 100644 --- a/documentation/assertions/moment/to-be-same-or-before.md +++ b/documentation/assertions/moment/to-be-same-or-before.md @@ -13,6 +13,6 @@ expect(moment('2015-04-01'), 'to be same or before', moment('2015-01-01')); ``` ```output -expected moment(2015-04-01T00:00:00.000+02:00) -to be same or before moment(2015-01-01T00:00:00.000+01:00) +expected moment('2015-04-01T00:00:00.000+02:00') +to be same or before moment('2015-01-01T00:00:00.000+01:00') ``` diff --git a/documentation/assertions/moment/to-be-the-end-of.md b/documentation/assertions/moment/to-be-the-end-of.md index f435da3..06eaa6c 100644 --- a/documentation/assertions/moment/to-be-the-end-of.md +++ b/documentation/assertions/moment/to-be-the-end-of.md @@ -13,5 +13,8 @@ expect(moment('2015-01-01 00:00:00'), 'to be the end of day'); ``` ```output -expected moment(2015-01-01T00:00:00.000+01:00) to be the end of day +expected moment('2015-01-01T00:00:00.000+01:00') to be the end of day + +-moment('2015-01-01T00:00:00.000+01:00') ++moment('2015-01-01T23:59:59.999+01:00') ``` diff --git a/documentation/assertions/moment/to-be-the-start-of.md b/documentation/assertions/moment/to-be-the-start-of.md index c5d2f6b..4a16da8 100644 --- a/documentation/assertions/moment/to-be-the-start-of.md +++ b/documentation/assertions/moment/to-be-the-start-of.md @@ -13,5 +13,8 @@ expect(moment('2015-01-01 01'), 'to be the start of year'); ``` ```output -expected moment(2015-01-01T01:00:00.000+01:00) to be the start of year +expected moment('2015-01-01T01:00:00.000+01:00') to be the start of year + +-moment('2015-01-01T01:00:00.000+01:00') ++moment('2015-01-01T00:00:00.000+01:00') ``` diff --git a/documentation/assertions/moment/to-equal.md b/documentation/assertions/moment/to-equal.md index 95a5604..d57a1b5 100644 --- a/documentation/assertions/moment/to-equal.md +++ b/documentation/assertions/moment/to-equal.md @@ -27,9 +27,9 @@ expect(moment('2015-11-01'), 'to equal', moment('2015-11-02'), 'in days'); ``` ```output -expected moment(2015-11-01T00:00:00.000+01:00) -to equal moment(2015-11-02T00:00:00.000+01:00) in days +expected moment('2015-11-01T00:00:00.000+01:00') +to equal moment('2015-11-02T00:00:00.000+01:00') in days --2015-11-01T00:00:00.000+01:00 -+2015-11-02T00:00:00.000+01:00 +-moment('2015-11-01T00:00:00.000+01:00') ++moment('2015-11-02T00:00:00.000+01:00') ``` diff --git a/documentation/assertions/moment/to-satisfy.md b/documentation/assertions/moment/to-satisfy.md index b385026..046b41e 100644 --- a/documentation/assertions/moment/to-satisfy.md +++ b/documentation/assertions/moment/to-satisfy.md @@ -18,6 +18,16 @@ expect(moment('2015-11-01'), 'to satisfy', { year: 2015, month: 11, day: 1 }); ``` ```output -expected moment(2015-11-01T00:00:00.000+01:00) +expected moment('2015-11-01T00:00:00.000+01:00') to satisfy { year: 2015, month: 11, day: 1 } + +{ + years: 2015, + months: 10, // should equal 11 + date: 1, + hours: 0, + minutes: 0, + seconds: 0, + milliseconds: 0 +} ``` diff --git a/documentation/assertions/moment/when-formatted-with.md b/documentation/assertions/moment/when-formatted-with.md index 21fd8ff..384c5ed 100644 --- a/documentation/assertions/moment/when-formatted-with.md +++ b/documentation/assertions/moment/when-formatted-with.md @@ -11,7 +11,7 @@ expect(moment('2015-11-02'), 'when formatted with', 'YYYYMMDD', 'to be', '2015') ``` ```output -expected moment(2015-11-02T00:00:00.000+01:00) +expected moment('2015-11-02T00:00:00.000+01:00') when formatted with 'YYYYMMDD' to be '2015' -20151102 diff --git a/documentation/assertions/moment/when-formatted.md b/documentation/assertions/moment/when-formatted.md index a2912f1..e0c5834 100644 --- a/documentation/assertions/moment/when-formatted.md +++ b/documentation/assertions/moment/when-formatted.md @@ -11,8 +11,7 @@ expect(moment('2015-11-02'), 'when formatted to be', '2015'); ``` ```output -expected moment(2015-11-02T00:00:00.000+01:00) -when formatted with 'YYYYMMDD' to be '2015' +expected moment('2015-11-02T00:00:00.000+01:00') when formatted to be '2015' -2015-11-02T00:00:00+01:00 +2015 diff --git a/documentation/index.md b/documentation/index.md index d94e462..3747be9 100644 --- a/documentation/index.md +++ b/documentation/index.md @@ -21,11 +21,11 @@ expect(date, 'to equal', moment('2015-11-03')); ``` ```output -expected moment(2015-11-06T00:00:00.000+01:00) -to equal moment(2015-11-03T00:00:00.000+01:00) +expected moment('2015-11-06T00:00:00.000+01:00') +to equal moment('2015-11-03T00:00:00.000+01:00') --2015-11-06T00:00:00.000+01:00 -+2015-11-03T00:00:00.000+01:00 +-moment('2015-11-06T00:00:00.000+01:00') ++moment('2015-11-03T00:00:00.000+01:00') ``` [![NPM version](https://badge.fury.io/js/unexpected-moment.svg)](http://badge.fury.io/js/unexpected-moment) From 155cb63a853882df69c1ba754ac34f453d8492d3 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Tue, 28 Jun 2016 13:17:09 +0200 Subject: [PATCH 18/35] Fix failing test See discussion in https://github.com/unexpectedjs/unexpected-moment/pull/3 --- test/unexpected-moment.spec.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 3546841..0d86c32 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -87,9 +87,7 @@ describe('unexpected-moment', function () { expect(moment('2016-01-01'), 'to equal', '2016-01-01'); }, 'to error with', - 'Unknown assertion to equal \n' + - '\n' + - 'Did you mean to satisfy ?' + 'expected moment(\'2016-01-01T00:00:00.000+01:00\') to equal \'2016-01-01\'' ); }); From 6ed046e6b0970cfe7f897a372b115d0ae50a7f7f Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Wed, 20 Jul 2016 15:20:34 +0200 Subject: [PATCH 19/35] Constrain to supported moment-like types Breaking change --- lib/unexpected-moment.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index a6aabf5..8888025 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -74,7 +74,7 @@ } }); - expect.addAssertion(' to satisfy ', function (expect, subject, value) { + expect.addAssertion(' to satisfy ', function (expect, subject, value) { if (value instanceof moment) { return expect(subject, 'to equal', value); } @@ -160,23 +160,23 @@ return expect(moment.isMoment(subject), '[not] to be true'); }); - expect.addAssertion(' [not] to be before ', function (expect, subject, value) { + expect.addAssertion(' [not] to be before ', function (expect, subject, value) { return expect(subject.isBefore(value), '[not] to be true'); }); - expect.addAssertion(' [not] to be after ', function (expect, subject, value) { + expect.addAssertion(' [not] to be after ', function (expect, subject, value) { return expect(subject.isAfter(value), '[not] to be true'); }); - expect.addAssertion(' [not] to be same or before ', function (expect, subject, value) { + expect.addAssertion(' [not] to be same or before ', function (expect, subject, value) { return expect(subject.isSameOrBefore(value), '[not] to be true'); }); - expect.addAssertion(' [not] to be same or after ', function (expect, subject, value) { + expect.addAssertion(' [not] to be same or after ', function (expect, subject, value) { return expect(subject.isSameOrAfter(value), '[not] to be true'); }); - expect.addAssertion(' [not] to be [inclusively] between ', function (expect, subject, from, to) { + expect.addAssertion(' [not] to be [inclusively] between ', function (expect, subject, from, to) { expect.argsOutput = function (output) { output.appendInspected(from).text(' and ').appendInspected(to); }; From 488cd9e163b9bacbe1c4a8faed01fa2b9dde0c32 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Wed, 20 Jul 2016 16:07:55 +0200 Subject: [PATCH 20/35] Remove moment-date type Since it overrides Unexpected's date type which was not the intention. --- lib/unexpected-moment.js | 46 +++++++++++++++++++++------------- test/unexpected-moment.spec.js | 15 +++++++++-- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index 8888025..abe18da 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -36,23 +36,6 @@ } }); - expect.addType({ - name: 'moment-date', - base: 'date', - identify: function (value) { - return value && value instanceof Date; - }, - inspect: function (value, depth, output, inspect) { - output.jsFunctionName('new Date') - .text('(') - .jsString(inspect(value.toString())) - .text(')'); - }, - diff: function (actual, expected, output, diff, inspect) { - return diff(inspect(actual).toString(), inspect(expected).toString()); - } - }); - expect.addAssertion(' [not] to equal ', function (expect, subject, value, label) { // TODO: validate granularity? var granularity = (label || '').replace(/^in /, '').replace(/s$/, ''); @@ -91,7 +74,34 @@ } if (value instanceof Date) { - return expect(subject.toDate(), 'to equal', value); + if (!subject.isSame(value)) { + expect.argsOutput = function (output) { + output.jsKeyword('new') + .sp() + .text('Date(') + .appendInspected(value.toString()) + .text(')'); + }; + return expect.fail({ + diff: function (output, diff) { + return output.block(function (output) { + output.jsKeyword('new') + .sp() + .text('Date(\'') + .nl() + .jsKeyword('new') + .sp() + .text('Date(\''); + }).block(diff( + subject.toDate().toString(), + value.toString() + )).block(function (output) { + output.text('\')').nl().text('\')'); + }); + } + }); + } + return; } if (Array.isArray(value)) { diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 0d86c32..342f243 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -360,8 +360,8 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy new Date(\'Sat Jan 02 2016 01:00:00 GMT+0100 (CET)\')\n' + '\n' + - '-new Date(\'Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\')\n' + - '+new Date(\'Sat Jan 02 2016 01:00:00 GMT+0100 (CET)\')' + 'new Date(\'-Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\')\n' + + 'new Date(\'+Sat Jan 02 2016 01:00:00 GMT+0100 (CET)\')' ); }); }); @@ -800,4 +800,15 @@ describe('unexpected-moment', function () { ); }); }); + + it('does not interfere with unexpected\'s date type', function () { + expect( + function () { + expect(new Date('2016-01-01'), 'to equal', new Date('2016-01-02')); + }, + 'to error with', + 'expected new Date(\'Fri, 01 Jan 2016 00:00:00 GMT\')\n' + + 'to equal new Date(\'Sat, 02 Jan 2016 00:00:00 GMT\')' + ); + }); }); From 62d44cf2e2db5a33b419b4e4750370f658cdd4ec Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Wed, 20 Jul 2016 16:09:24 +0200 Subject: [PATCH 21/35] Move those lines around --- lib/unexpected-moment.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index abe18da..31bcef7 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -9,12 +9,12 @@ root.moment.unexpectedMoment = factory(root.moment); } }(this, function (moment) { - var outputFormat = 'YYYY-MM-DDTHH:mm:ss.SSSZ'; - return { name: 'unexpected-moment', installInto: function (expect) { + var outputFormat = 'YYYY-MM-DDTHH:mm:ss.SSSZ'; + expect.addType({ name: 'moment', base: 'object', @@ -36,6 +36,10 @@ } }); + expect.addAssertion(' [not] to be a moment', function (expect, subject) { + return expect(moment.isMoment(subject), '[not] to be true'); + }); + expect.addAssertion(' [not] to equal ', function (expect, subject, value, label) { // TODO: validate granularity? var granularity = (label || '').replace(/^in /, '').replace(/s$/, ''); @@ -166,10 +170,6 @@ } }); - expect.addAssertion(' [not] to be a moment', function (expect, subject) { - return expect(moment.isMoment(subject), '[not] to be true'); - }); - expect.addAssertion(' [not] to be before ', function (expect, subject, value) { return expect(subject.isBefore(value), '[not] to be true'); }); From 1c666992edd18f47be4e235396c0c935df5110a4 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Wed, 20 Jul 2016 16:55:25 +0200 Subject: [PATCH 22/35] Use moment.isSame instead of modifying it's behaviour Move code used for diffing where it's supposed to be. --- lib/unexpected-moment.js | 119 ++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 57 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index 31bcef7..e2d27de 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -77,51 +77,17 @@ return expect.fail(); } - if (value instanceof Date) { - if (!subject.isSame(value)) { - expect.argsOutput = function (output) { - output.jsKeyword('new') - .sp() - .text('Date(') - .appendInspected(value.toString()) - .text(')'); - }; - return expect.fail({ - diff: function (output, diff) { - return output.block(function (output) { - output.jsKeyword('new') - .sp() - .text('Date(\'') - .nl() - .jsKeyword('new') - .sp() - .text('Date(\''); - }).block(diff( - subject.toDate().toString(), - value.toString() - )).block(function (output) { - output.text('\')').nl().text('\')'); - }); - } - }); - } - return; - } - - if (Array.isArray(value)) { - if (!value.length) { - return expect.fail(); - } - return expect( - subject.toArray().slice(0, value.length), - 'to satisfy', - value - ); + var valueIsArray = Array.isArray(value); + if (valueIsArray && !value.length) { + return expect.fail(); } var valueType = typeof value; - if (valueType === 'object') { - var normalizedValue = {}; + var valueIsDate = value instanceof Date; + var valueIsObject = !valueIsDate && !valueIsArray && + valueType === 'object'; + var normalizedObjectValue = {}; + if (valueIsObject) { var keys = Object.keys(value); if (!keys.length) { return expect.fail(); @@ -148,25 +114,64 @@ if (normalizedUnit !== 'date') { normalizedUnit += 's'; // Urgh } - normalizedValue[normalizedUnit] = value[unit]; + normalizedObjectValue[normalizedUnit] = value[unit]; } - return expect(subject.toObject(), 'to satisfy', normalizedValue); } - if (valueType === 'string') { - var desiredFormat = valueAsMomentInstance._f || outputFormat; - return expect(subject.format(desiredFormat), 'to satisfy', value); - } - - if (valueType === 'number') { - var subjectAsNumber = subject.valueOf(); - if (subjectAsNumber !== value) { - return expect.fail({ - diff: function (output, diff, inspect, equal) { - return diff(String(subjectAsNumber), String(value)); + if (!subject.isSame(value)) { + return expect.fail({ + diff: function (output, diff) { + if (valueIsDate) { + expect.argsOutput = function (output) { + output.jsKeyword('new') + .sp() + .text('Date(') + .appendInspected(value.toString()) + .text(')'); + }; + return output.block(function (output) { + output.jsKeyword('new') + .sp() + .text('Date(\'') + .nl() + .jsKeyword('new') + .sp() + .text('Date(\''); + }).block(diff( + subject.toDate().toString(), + value.toString() + )).block(function (output) { + output.text('\')').nl().text('\')'); + }); } - }); - } + if (valueIsArray) { + return diff( + subject.toArray().slice(0, value.length), + value + ); + } + if (valueIsObject) { + return diff( + subject.toObject(), + normalizedObjectValue + ); + } + if (valueType === 'string') { + var desiredFormat = valueAsMomentInstance._f || outputFormat; + return diff( + subject.format(desiredFormat), + value + ); + } + if (valueType === 'number') { + var subjectAsNumber = subject.valueOf(); + return diff( + String(subjectAsNumber), + String(value) + ); + } + } + }); } }); From 866b6cb0cecd160f4083f1b49e8f487044453c74 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Wed, 20 Jul 2016 16:59:15 +0200 Subject: [PATCH 23/35] Remove keys not provided in the value from the diff --- lib/unexpected-moment.js | 8 +++++++- test/unexpected-moment.spec.js | 2 -- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index e2d27de..fac90e0 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -151,8 +151,14 @@ ); } if (valueIsObject) { + var subjectAsObject = subject.toObject(); + Object.keys(subjectAsObject).forEach(function (key) { + if (!(key in normalizedObjectValue)) { + delete subjectAsObject[key]; + } + }); return diff( - subject.toObject(), + subjectAsObject, normalizedObjectValue ); } diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 342f243..0adc8b4 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -316,9 +316,7 @@ describe('unexpected-moment', function () { ' years: 2016,\n' + ' months: 0,\n' + ' date: 1, // should equal 2\n' + - ' hours: 0,\n' + ' minutes: 0, // should equal 10\n' + - ' seconds: 0,\n' + ' milliseconds: 0 // should equal 3\n' + '}' ); From 9485a5affd1aa604ca98f1fc486b8f5e9f396a71 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Wed, 20 Jul 2016 21:40:31 +0200 Subject: [PATCH 24/35] Add value validation and output dates in local time --- lib/unexpected-moment.js | 187 +++++++++++++++++++++++++-------------- 1 file changed, 119 insertions(+), 68 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index fac90e0..1085a35 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -61,111 +61,136 @@ } }); - expect.addAssertion(' to satisfy ', function (expect, subject, value) { - if (value instanceof moment) { - return expect(subject, 'to equal', value); + function ensureValid(value, expect) { + if (value instanceof moment || value instanceof Date) { + return; } - // TODO: use nested errorMode? - // TODO: perhaps add a moment-like type that filters out invalid values in the first place - var originalSuppressDeprecationWarningsSetting = moment.suppressDeprecationWarnings; + var originalSetting = moment.suppressDeprecationWarnings; moment.suppressDeprecationWarnings = true; - var valueAsMomentInstance = moment(value); - moment.suppressDeprecationWarnings = originalSuppressDeprecationWarningsSetting; + var valueAsMoment = moment(value); + moment.suppressDeprecationWarnings = originalSetting; - if (!valueAsMomentInstance.isValid()) { + if (!valueAsMoment.isValid()) { return expect.fail(); } - var valueIsArray = Array.isArray(value); - if (valueIsArray && !value.length) { - return expect.fail(); + if (Array.isArray(value)) { + if (!value.length) { + return expect.fail(); + } + return; } - var valueType = typeof value; - var valueIsDate = value instanceof Date; - var valueIsObject = !valueIsDate && !valueIsArray && - valueType === 'object'; - var normalizedObjectValue = {}; - if (valueIsObject) { + if (typeof value === 'object') { var keys = Object.keys(value); if (!keys.length) { return expect.fail(); } for (var i = 0; i < keys.length; i++) { var unit = keys[i]; - var normalizedUnit = moment.normalizeUnits(unit); - if (!normalizedUnit) { + if (!moment.normalizeUnits(unit)) { expect.errorMode = 'nested'; return expect.fail({ message: function (output) { output.nl() - .text('Unit \'') - .jsString(unit) - .text('\'') - .sp() - .text('is not a valid moment unit'); + .text('Unit \'') + .jsString(unit) + .text('\'') + .sp() + .text('is not a valid moment unit'); } }); } - if (normalizedUnit === 'day') { - normalizedUnit = 'date'; // Urgh - } - if (normalizedUnit !== 'date') { - normalizedUnit += 's'; // Urgh - } - normalizedObjectValue[normalizedUnit] = value[unit]; } } + } + + function inspectDate(date, output) { + output.jsKeyword('new') + .sp() + .text('Date(') + .appendInspected(date.toString()) + .text(')'); + } + function diffDates(date1, date2, diff, output) { + return output.block(function (output) { + output.jsKeyword('new') + .sp() + .text('Date(\'') + .nl() + .jsKeyword('new') + .sp() + .text('Date(\''); + }).block(diff( + date1.toString(), + date2.toString() + )).block(function (output) { + output.text('\')') + .nl() + .text('\')'); + }); + } + + function normalizeObject(object) { + var normalizedObject = {}; + Object.keys(object).forEach(function (unit) { + var normalizedUnit = moment.normalizeUnits(unit); + if (normalizedUnit === 'day') { + normalizedUnit = 'date'; // Urgh + } + if (normalizedUnit !== 'date') { + normalizedUnit += 's'; // Urgh + } + normalizedObject[normalizedUnit] = object[unit]; + }, {}); + return normalizedObject; + } + + expect.addAssertion(' to satisfy ', function (expect, subject, value) { + if (value instanceof moment) { + return expect(subject, 'to equal', value); + } + ensureValid(value, expect); if (!subject.isSame(value)) { return expect.fail({ diff: function (output, diff) { - if (valueIsDate) { + if (value instanceof Date) { expect.argsOutput = function (output) { - output.jsKeyword('new') - .sp() - .text('Date(') - .appendInspected(value.toString()) - .text(')'); + inspectDate(value, output); }; - return output.block(function (output) { - output.jsKeyword('new') - .sp() - .text('Date(\'') - .nl() - .jsKeyword('new') - .sp() - .text('Date(\''); - }).block(diff( - subject.toDate().toString(), - value.toString() - )).block(function (output) { - output.text('\')').nl().text('\')'); - }); + return diffDates( + subject.toDate(), + value, + diff, + output + ); } - if (valueIsArray) { + if (Array.isArray(value)) { return diff( subject.toArray().slice(0, value.length), value ); } - if (valueIsObject) { + var valueType = typeof value; + if (valueType === 'object') { + var normalizedValue = normalizeObject(value); var subjectAsObject = subject.toObject(); Object.keys(subjectAsObject).forEach(function (key) { - if (!(key in normalizedObjectValue)) { + if (!normalizedValue.hasOwnProperty(key)) { delete subjectAsObject[key]; } }); return diff( subjectAsObject, - normalizedObjectValue + normalizedValue ); } if (valueType === 'string') { - var desiredFormat = valueAsMomentInstance._f || outputFormat; + var matchingFormat = moment(value)._f; return diff( - subject.format(desiredFormat), + subject.format(matchingFormat || outputFormat), value ); } @@ -181,33 +206,59 @@ } }); + function assert(expect, subject, value, method) { + ensureValid(value, expect); + if (subject[method](value) === expect.flags.not) { + if (value instanceof Date) { + expect.argsOutput = function (output) { + inspectDate(value, output); + }; + } + return expect.fail(); + } + } + expect.addAssertion(' [not] to be before ', function (expect, subject, value) { - return expect(subject.isBefore(value), '[not] to be true'); + return assert(expect, subject, value, 'isBefore'); }); expect.addAssertion(' [not] to be after ', function (expect, subject, value) { - return expect(subject.isAfter(value), '[not] to be true'); + return assert(expect, subject, value, 'isAfter'); }); expect.addAssertion(' [not] to be same or before ', function (expect, subject, value) { - return expect(subject.isSameOrBefore(value), '[not] to be true'); + return assert(expect, subject, value, 'isSameOrBefore'); }); expect.addAssertion(' [not] to be same or after ', function (expect, subject, value) { - return expect(subject.isSameOrAfter(value), '[not] to be true'); + return assert(expect, subject, value, 'isSameOrAfter'); }); expect.addAssertion(' [not] to be [inclusively] between ', function (expect, subject, from, to) { - expect.argsOutput = function (output) { - output.appendInspected(from).text(' and ').appendInspected(to); - }; - var inclusivity; if (expect.flags.inclusively) { inclusivity = '[]'; } - - return expect(subject.isBetween(from, to, null, inclusivity), '[not] to be true'); + if (subject.isBetween(from, to, null, inclusivity) === expect.flags.not) { + expect.argsOutput = function (output) { + if (from instanceof Date) { + output.append(function (output) { + inspectDate(from, output); + }); + } else { + output.appendInspected(from); + } + output.text(' and '); + if (to instanceof Date) { + output.append(function (output) { + inspectDate(to, output); + }); + } else { + output.appendInspected(to); + } + }; + return expect.fail(); + } }); expect.addAssertion([ From 4d60b54a50b63ab8dc7a3fdb99640062e75afba0 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Wed, 20 Jul 2016 21:43:10 +0200 Subject: [PATCH 25/35] Fix erroneous test --- test/unexpected-moment.spec.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 0adc8b4..5d64ead 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -193,25 +193,13 @@ describe('unexpected-moment', function () { }); // see: https://github.com/moment/moment/issues/2633 - it('passes for utc moments if passed a value that is not a moment instance or has no timezone information', function () { - // The argument here is that as a moment object, the timezone is relevant. - // But as an array|object|number there is no timezone information; - // therefore, the two represent a similar moment in time. - expect(function () { - expect(moment.utc('2015-01-01T00:00:00+00:00'), 'to satisfy', moment([2015, 0, 1, 0, 0, 0, 0])); - }, 'to error'); - // but... + it('throws for utc moments if passed a value that has no timezone information', function () { expect(function () { expect(moment.utc('2015-01-01T00:00:00+00:00'), 'to satisfy', [2015, 0, 1, 0, 0, 0, 0]); - }, 'not to error'); - // but for a string with with timezone information... - expect(function () { - expect(moment.utc('2015-01-01T00:00:00+00:00'), 'to satisfy', '2015-01-01T00:00:00+01:00'); }, 'to error'); - // and a string with no timezone information... expect(function () { expect(moment.utc('2015-01-01T00:00:00+00:00'), 'to satisfy', '2015-01-01T00:00:00'); - }, 'not to error'); + }, 'to error'); }); it('throws the correct error if the assertion fails for a moment value', function () { From 59a3706fef1caf9fc7585a1dff972e6bed62b2ef Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Wed, 20 Jul 2016 21:53:45 +0200 Subject: [PATCH 26/35] Fix error message --- lib/unexpected-moment.js | 4 ++-- test/unexpected-moment.spec.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index 1085a35..75767ee 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -98,7 +98,7 @@ .jsString(unit) .text('\'') .sp() - .text('is not a valid moment unit'); + .text('is not a valid unit'); } }); } @@ -149,10 +149,10 @@ } expect.addAssertion(' to satisfy ', function (expect, subject, value) { + ensureValid(value, expect); if (value instanceof moment) { return expect(subject, 'to equal', value); } - ensureValid(value, expect); if (!subject.isSame(value)) { return expect.fail({ diff: function (output, diff) { diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 5d64ead..64d2989 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -319,7 +319,7 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy { year: 2016, month: 0, date: 1, minutez: 0, millisecond: 0 }\n' + ' \n' + - ' Unit \'minutez\' is not a valid moment unit' + ' Unit \'minutez\' is not a valid unit' ); }); From e2369b09200b6ec401416c238086fd94604b678d Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Wed, 20 Jul 2016 23:31:58 +0200 Subject: [PATCH 27/35] Update examples in documentation --- documentation/assertions/any/to-be-a-moment.md | 2 +- documentation/assertions/moment/to-satisfy.md | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/documentation/assertions/any/to-be-a-moment.md b/documentation/assertions/any/to-be-a-moment.md index 60312a8..cf3b373 100644 --- a/documentation/assertions/any/to-be-a-moment.md +++ b/documentation/assertions/any/to-be-a-moment.md @@ -12,5 +12,5 @@ expect(new Date('2015-01-01'), 'to be a moment'); ``` ```output -expected new Date('Thu Jan 01 2015 01:00:00 GMT+0100 (CET)') to be a moment +expected new Date('Thu, 01 Jan 2015 00:00:00 GMT') to be a moment ``` diff --git a/documentation/assertions/moment/to-satisfy.md b/documentation/assertions/moment/to-satisfy.md index 046b41e..0529bdb 100644 --- a/documentation/assertions/moment/to-satisfy.md +++ b/documentation/assertions/moment/to-satisfy.md @@ -24,10 +24,6 @@ to satisfy { year: 2015, month: 11, day: 1 } { years: 2015, months: 10, // should equal 11 - date: 1, - hours: 0, - minutes: 0, - seconds: 0, - milliseconds: 0 + date: 1 } ``` From 6fccefc89094401b79d1d6912480fb6facb8c3c0 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Thu, 21 Jul 2016 15:29:11 +0200 Subject: [PATCH 28/35] Fix typo --- test/unexpected-moment.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index 64d2989..c7f1a95 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -177,7 +177,7 @@ describe('unexpected-moment', function () { }, 'to error'); }); - it('throws if passed an objet with unknown keys', function () { + it('throws if passed an object with unknown keys', function () { expect(function () { expect(moment('2016-01-01'), 'to satisfy', { years: 2016, From fb2889c2f244fcd11132f21a23412ac830541c7a Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Sun, 24 Jul 2016 18:00:49 +0200 Subject: [PATCH 29/35] Minor refactor --- lib/unexpected-moment.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index 75767ee..310ea4d 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -134,8 +134,7 @@ } function normalizeObject(object) { - var normalizedObject = {}; - Object.keys(object).forEach(function (unit) { + return Object.keys(object).reduce(function (normalizedObject, unit) { var normalizedUnit = moment.normalizeUnits(unit); if (normalizedUnit === 'day') { normalizedUnit = 'date'; // Urgh @@ -144,8 +143,8 @@ normalizedUnit += 's'; // Urgh } normalizedObject[normalizedUnit] = object[unit]; + return normalizedObject; }, {}); - return normalizedObject; } expect.addAssertion(' to satisfy ', function (expect, subject, value) { From 34a947ed4e94af334a1624c1f68d67b1ac05b2c2 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Sun, 24 Jul 2016 18:01:33 +0200 Subject: [PATCH 30/35] Move timezone bootstrapping into npm test script So that running the tests using "nmp test" doesn't fail when in another timezone. --- .travis.yml | 2 -- package.json | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 63d4be6..c76b348 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,5 @@ node_js: - "4" - "5" - "6" -env: - - TZ="Europe/Copenhagen" before_install: "npm install -g npm && npm cache clean" script: "npm run-script travis" diff --git a/package.json b/package.json index 39f9883..7d72d28 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "scripts": { "lint": "eslint .", - "test": "mocha test/**/*.js documentation/**/*.md && npm run lint", + "test": "TZ='Europe/Copenhagen' mocha test/**/*.js documentation/**/*.md && npm run lint", "travis": "npm test && npm run coverage && ( Date: Sun, 24 Jul 2016 18:04:17 +0200 Subject: [PATCH 31/35] Add tests for diffing moment.utc against moment objects --- test/unexpected-moment.spec.js | 88 ++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index c7f1a95..d088f95 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -216,6 +216,20 @@ describe('unexpected-moment', function () { ); }); + it('throws the correct error if the assertion fails for a utc moment value', function () { + expect( + function () { + expect(moment('2016-01-02'), 'to satisfy', moment.utc('2016-01-02')); + }, + 'to error with', + 'expected moment(\'2016-01-02T00:00:00.000+01:00\')\n' + + 'to satisfy moment.utc(\'2016-01-02T00:00:00.000+00:00\')\n' + + '\n' + + '-moment(\'2016-01-02T00:00:00.000+01:00\')\n' + + '+moment.utc(\'2016-01-02T00:00:00.000+00:00\')' + ); + }); + it('throws the correct error if the assertion fails for a string value', function () { expect( function () { @@ -350,6 +364,80 @@ describe('unexpected-moment', function () { 'new Date(\'+Sat Jan 02 2016 01:00:00 GMT+0100 (CET)\')' ); }); + + it('does not include the diff if the outputs are equal for an array value', function () { + expect( + function () { + expect(moment.utc('2016-01-01T00:00:00+00:00'), 'to satisfy', [2016, 0, 1, 0, 0, 0, 0]); + }, + 'to error with', + 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + + 'to satisfy [ 2016, 0, 1, 0, 0, 0, 0 ]' + ); + }); + + it('does not include the diff if the outputs are equal for an object value', function () { + expect( + function () { + expect(moment.utc('2016-01-01T00:00:00+00:00'), 'to satisfy', { year: 2016, month: 0, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 }); + }, + 'to error with', + 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\') to satisfy\n' + + '{\n' + + ' year: 2016, month: 0, day: 1, hour: 0, minute: 0, second: 0,\n' + + ' millisecond: 0\n' + + '}' + ); + }); + + it('does not include the diff if the outputs are equal for a Date value', function () { + expect( + function () { + expect(moment.utc('2016-01-01T00:00:00+00:00'), 'to satisfy', new Date('Fri Jan 01 2016 00:00:00 GMT+0100 (CET)')); + }, + 'to error with', + 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + + 'to satisfy new Date(\'Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\')' + ); + }); + + it('does not include the diff if the outputs are equal for a string value', function () { + expect( + function () { + expect(moment.utc('2016-01-01T00:00:00+00:00'), 'to satisfy', '2016-01-01T00:00:00+01:00'); + }, + 'to error with', + 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + + 'to satisfy \'2016-01-01T00:00:00+01:00\'' + ); + }); + + it('does not include the diff if the outputs are equal for a number value', function () { + expect( + function () { + expect(moment.utc('2016-01-01T00:00:00+00:00'), 'to satisfy', 1451602800000); + }, + 'to error with', + 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + + 'to satisfy 1451602800000' + ); + }); + + // TODO + it.skip('throws the correct error for utc moments when passed a value that has no timezone information', function () { + expect( + function () { + expect(moment.utc('2016-01-01T00:00:00+00:00'), 'to satisfy', [2016, 0, 1, 0, 0, 0, 0]); + }, + 'to error with', + 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + + 'to satisfy [ 2016, 0, 1, 0, 0, 0, 0 ]\n' + + '\n' + + 'moment.utc( // should be moment(\n' + + ' [ 2016, 0, 1, 0, 0, 0, 0 ]\n' + + ')' + ); + }); }); describe('not to satisfy', function () { From 2de764721c8e694112c3df25a46c7b11a17c7d14 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Fri, 29 Jul 2016 00:40:08 +0200 Subject: [PATCH 32/35] Add proper diffs --- lib/unexpected-moment.js | 214 +++++++++++++++++++++------------ test/unexpected-moment.spec.js | 178 ++++++++++++++++++--------- 2 files changed, 257 insertions(+), 135 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index 310ea4d..d612ddf 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -15,6 +15,136 @@ installInto: function (expect) { var outputFormat = 'YYYY-MM-DDTHH:mm:ss.SSSZ'; + function normalizeObject(object) { + return Object.keys(object).reduce(function (normalizedObject, unit) { + var normalizedUnit = moment.normalizeUnits(unit); + if (normalizedUnit === 'day') { + normalizedUnit = 'date'; // Urgh + } + if (normalizedUnit !== 'date') { + normalizedUnit += 's'; // Urgh + } + normalizedObject[normalizedUnit] = object[unit]; + return normalizedObject; + }, {}); + } + + function diffMoments(actual, expected, output, diff, inspect, equal) { + var actualIsUtc = actual.isUtc(); + var functionName = actualIsUtc ? 'moment.utc' : 'moment'; + var expectedIsMoment = expected instanceof moment; + var expectedIsDate = expected instanceof Date; + var expectedIsArray = Array.isArray(expected); + var typeOfExpected = typeof expected; + var expectedIsObject = typeOfExpected === 'object' && + !(expectedIsArray || expectedIsMoment || expectedIsDate); + var expectedIsString = typeOfExpected === 'string'; + var expectedIsNumber = typeOfExpected === 'number'; + + var momentShouldBe; + var functionNameShouldBe; + if (expectedIsMoment) { + if (actualIsUtc) { + if (!expected.isUtc()) { + functionNameShouldBe = 'moment'; + } + } else { + if (expected.isUtc()) { + functionNameShouldBe = 'moment.utc'; + } + } + if (functionNameShouldBe) { + momentShouldBe = 'should be '; + } + } else { + if (actualIsUtc) { + momentShouldBe = 'should be in local time'; + } + } + + output.jsFunctionName(functionName).text('('); + if (momentShouldBe) { + output.sp().annotationBlock(function () { + this.error(momentShouldBe); + if (functionNameShouldBe) { + this.jsFunctionName(functionNameShouldBe).text('('); + } + }); + } + + var actualForDiff; + var expectedForDiff; + var formattedDiff; + if (expectedIsMoment) { + actualForDiff = actual.format(outputFormat); + expectedForDiff = expected.format(outputFormat); + } + if (expectedIsDate) { + actualForDiff = actual.toDate().toString(); + expectedForDiff = expected.toString(); + } + if (expectedIsArray) { + actualForDiff = actual.toArray().slice(0, expected.length); + expectedForDiff = expected; + } + if (expectedIsObject) { + expectedForDiff = normalizeObject(expected); + actualForDiff = actual.toObject(); + Object.keys(actualForDiff).forEach(function (key) { + if (!expectedForDiff.hasOwnProperty(key)) { + delete actualForDiff[key]; + } + }); + } + if (expectedIsString) { + var expectedFormat = moment(expected)._f; + actualForDiff = actual.format(expectedFormat || outputFormat); + expectedForDiff = expected; + } + if (expectedIsNumber) { + actualForDiff = actual.valueOf(); + expectedForDiff = expected; + formattedDiff = diff(String(actualForDiff), String(expected)); + } + + output.nl().indentLines().i(); + if (expectedIsDate) { + output.jsKeyword('new').sp().text('Date(').nl().i().i(); + } + + var isEqual; + if (!(expectedIsMoment || expectedIsDate)) { + if (equal(actualForDiff, expectedForDiff)) { + isEqual = true; + output.block(inspect(actualForDiff)); + } + } + if (!formattedDiff) { + formattedDiff = diff(actualForDiff, expectedForDiff); + } + if (expectedIsArray || expectedIsObject) { + if (!isEqual && formattedDiff) { + output.block(formattedDiff); + } + } else { + if (!isEqual) { + output.append(inspect(actualForDiff)).sp() + .annotationBlock(function () { + this.error('should be ') + .append(inspect(expectedForDiff)); + if (formattedDiff) { + this.nl().append(formattedDiff); + } + }); + } + } + if (expectedIsDate) { + output.nl().i().text(')'); + } + + return output.nl().text(')'); + } + expect.addType({ name: 'moment', base: 'object', @@ -25,15 +155,13 @@ return a.isSame(b); }, inspect: function (value, depth, output, inspect) { - var name = value.isUtc() ? 'moment.utc' : 'moment'; - output.jsFunctionName(name) + var functionName = value.isUtc() ? 'moment.utc' : 'moment'; + output.jsFunctionName(functionName) .text('(') .jsString(inspect(value.format(outputFormat))) .text(')'); }, - diff: function (actual, expected, output, diff, inspect) { - return diff(inspect(actual).toString(), inspect(expected).toString()); - } + diff: diffMoments }); expect.addAssertion(' [not] to be a moment', function (expect, subject) { @@ -114,39 +242,6 @@ .text(')'); } - function diffDates(date1, date2, diff, output) { - return output.block(function (output) { - output.jsKeyword('new') - .sp() - .text('Date(\'') - .nl() - .jsKeyword('new') - .sp() - .text('Date(\''); - }).block(diff( - date1.toString(), - date2.toString() - )).block(function (output) { - output.text('\')') - .nl() - .text('\')'); - }); - } - - function normalizeObject(object) { - return Object.keys(object).reduce(function (normalizedObject, unit) { - var normalizedUnit = moment.normalizeUnits(unit); - if (normalizedUnit === 'day') { - normalizedUnit = 'date'; // Urgh - } - if (normalizedUnit !== 'date') { - normalizedUnit += 's'; // Urgh - } - normalizedObject[normalizedUnit] = object[unit]; - return normalizedObject; - }, {}); - } - expect.addAssertion(' to satisfy ', function (expect, subject, value) { ensureValid(value, expect); if (value instanceof moment) { @@ -154,52 +249,13 @@ } if (!subject.isSame(value)) { return expect.fail({ - diff: function (output, diff) { + diff: function (output, diff, inspect, equal) { if (value instanceof Date) { expect.argsOutput = function (output) { inspectDate(value, output); }; - return diffDates( - subject.toDate(), - value, - diff, - output - ); - } - if (Array.isArray(value)) { - return diff( - subject.toArray().slice(0, value.length), - value - ); - } - var valueType = typeof value; - if (valueType === 'object') { - var normalizedValue = normalizeObject(value); - var subjectAsObject = subject.toObject(); - Object.keys(subjectAsObject).forEach(function (key) { - if (!normalizedValue.hasOwnProperty(key)) { - delete subjectAsObject[key]; - } - }); - return diff( - subjectAsObject, - normalizedValue - ); - } - if (valueType === 'string') { - var matchingFormat = moment(value)._f; - return diff( - subject.format(matchingFormat || outputFormat), - value - ); - } - if (valueType === 'number') { - var subjectAsNumber = subject.valueOf(); - return diff( - String(subjectAsNumber), - String(value) - ); } + return diffMoments(subject, value, output, diff, inspect, equal); } }); } diff --git a/test/unexpected-moment.spec.js b/test/unexpected-moment.spec.js index d088f95..6b225a2 100644 --- a/test/unexpected-moment.spec.js +++ b/test/unexpected-moment.spec.js @@ -62,22 +62,45 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to equal moment(\'2016-01-02T00:00:00.000+01:00\')\n' + '\n' + - '-moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - '+moment(\'2016-01-02T00:00:00.000+01:00\')' + 'moment(\n' + + ' \'2016-01-01T00:00:00.000+01:00\' // should be \'2016-01-02T00:00:00.000+01:00\'\n' + + ' // -2016-01-01T00:00:00.000+01:00\n' + + ' // +2016-01-02T00:00:00.000+01:00\n' + + ')' ); }); - it('throws the correct error if the assertion fails due to the value being in UTC', function () { + it('throws the correct error if the assertion fails due to the value being in UTC while the subject is in local time', function () { expect( function () { - expect(moment('2016-01-01'), 'to equal', moment.utc('2016-01-02')); + expect(moment('2016-01-01'), 'to equal', moment.utc('2016-01-01')); }, 'to error with', 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - 'to equal moment.utc(\'2016-01-02T00:00:00.000+00:00\')\n' + + 'to equal moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + '\n' + - '-moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - '+moment.utc(\'2016-01-02T00:00:00.000+00:00\')' + 'moment( // should be moment.utc(\n' + + ' \'2016-01-01T00:00:00.000+01:00\' // should be \'2016-01-01T00:00:00.000+00:00\'\n' + + ' // -2016-01-01T00:00:00.000+01:00\n' + + ' // +2016-01-01T00:00:00.000+00:00\n' + + ')' + ); + }); + + it('throws the correct error if the assertion fails due to the value being in local time while the subject is in utc', function () { + expect( + function () { + expect(moment.utc('2016-01-01'), 'to equal', moment('2016-01-01')); + }, + 'to error with', + 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + + 'to equal moment(\'2016-01-01T00:00:00.000+01:00\')\n' + + '\n' + + 'moment.utc( // should be moment(\n' + + ' \'2016-01-01T00:00:00.000+00:00\' // should be \'2016-01-01T00:00:00.000+01:00\'\n' + + ' // -2016-01-01T00:00:00.000+00:00\n' + + ' // +2016-01-01T00:00:00.000+01:00\n' + + ')' ); }); @@ -100,8 +123,11 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to equal moment(\'2016-01-02T00:00:00.000+01:00\') in milliseconds\n' + '\n' + - '-moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - '+moment(\'2016-01-02T00:00:00.000+01:00\')' + 'moment(\n' + + ' \'2016-01-01T00:00:00.000+01:00\' // should be \'2016-01-02T00:00:00.000+01:00\'\n' + + ' // -2016-01-01T00:00:00.000+01:00\n' + + ' // +2016-01-02T00:00:00.000+01:00\n' + + ')' ); }); }); @@ -211,8 +237,11 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy moment(\'2016-01-02T00:00:00.000+01:00\')\n' + '\n' + - '-moment(\'2016-01-01T00:00:00.000+01:00\')\n' + - '+moment(\'2016-01-02T00:00:00.000+01:00\')' + 'moment(\n' + + ' \'2016-01-01T00:00:00.000+01:00\' // should be \'2016-01-02T00:00:00.000+01:00\'\n' + + ' // -2016-01-01T00:00:00.000+01:00\n' + + ' // +2016-01-02T00:00:00.000+01:00\n' + + ')' ); }); @@ -225,8 +254,11 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-02T00:00:00.000+01:00\')\n' + 'to satisfy moment.utc(\'2016-01-02T00:00:00.000+00:00\')\n' + '\n' + - '-moment(\'2016-01-02T00:00:00.000+01:00\')\n' + - '+moment.utc(\'2016-01-02T00:00:00.000+00:00\')' + 'moment( // should be moment.utc(\n' + + ' \'2016-01-02T00:00:00.000+01:00\' // should be \'2016-01-02T00:00:00.000+00:00\'\n' + + ' // -2016-01-02T00:00:00.000+01:00\n' + + ' // +2016-01-02T00:00:00.000+00:00\n' + + ')' ); }); @@ -239,8 +271,11 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + 'to satisfy \'2016-01-02\'\n' + '\n' + - '-2016-01-01\n' + - '+2016-01-02' + 'moment(\n' + + ' \'2016-01-01\' // should be \'2016-01-02\'\n' + + ' // -2016-01-01\n' + + ' // +2016-01-02\n' + + ')' ); }); @@ -253,11 +288,13 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + 'to satisfy [ 2016, 0, 2 ]\n' + '\n' + - '[\n' + - ' 2016,\n' + - ' 0,\n' + - ' 1 // should equal 2\n' + - ']' + 'moment(\n' + + ' [\n' + + ' 2016,\n' + + ' 0,\n' + + ' 1 // should equal 2\n' + + ' ]\n' + + ')' ); }); @@ -314,13 +351,15 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy { year: 2016, month: 0, date: 2, minute: 10, millisecond: 3 }\n' + '\n' + - '{\n' + - ' years: 2016,\n' + - ' months: 0,\n' + - ' date: 1, // should equal 2\n' + - ' minutes: 0, // should equal 10\n' + - ' milliseconds: 0 // should equal 3\n' + - '}' + 'moment(\n' + + ' {\n' + + ' years: 2016,\n' + + ' months: 0,\n' + + ' date: 1, // should equal 2\n' + + ' minutes: 0, // should equal 10\n' + + ' milliseconds: 0 // should equal 3\n' + + ' }\n' + + ')' ); }); @@ -346,8 +385,11 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\') ' + 'to satisfy 1451689200000\n' + '\n' + - '-1451602800000\n' + - '+1451689200000' + 'moment(\n' + + ' 1451602800000 // should be 1451689200000\n' + + ' // -1451602800000\n' + + ' // +1451689200000\n' + + ')' ); }); @@ -360,8 +402,13 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-01T00:00:00.000+01:00\')\n' + 'to satisfy new Date(\'Sat Jan 02 2016 01:00:00 GMT+0100 (CET)\')\n' + '\n' + - 'new Date(\'-Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\')\n' + - 'new Date(\'+Sat Jan 02 2016 01:00:00 GMT+0100 (CET)\')' + 'moment(\n' + + ' new Date(\n' + + ' \'Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\' // should be \'Sat Jan 02 2016 01:00:00 GMT+0100 (CET)\'\n' + + ' // -Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\n' + + ' // +Sat Jan 02 2016 01:00:00 GMT+0100 (CET)\n' + + ' )\n' + + ')' ); }); @@ -372,7 +419,11 @@ describe('unexpected-moment', function () { }, 'to error with', 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + - 'to satisfy [ 2016, 0, 1, 0, 0, 0, 0 ]' + 'to satisfy [ 2016, 0, 1, 0, 0, 0, 0 ]\n' + + '\n' + + 'moment.utc( // should be in local time\n' + + ' [ 2016, 0, 1, 0, 0, 0, 0 ]\n' + + ')' ); }); @@ -386,7 +437,14 @@ describe('unexpected-moment', function () { '{\n' + ' year: 2016, month: 0, day: 1, hour: 0, minute: 0, second: 0,\n' + ' millisecond: 0\n' + - '}' + '}\n' + + '\n' + + 'moment.utc( // should be in local time\n' + + ' {\n' + + ' years: 2016, months: 0, date: 1, hours: 0, minutes: 0, seconds: 0,\n' + + ' milliseconds: 0\n' + + ' }\n' + + ')' ); }); @@ -397,18 +455,30 @@ describe('unexpected-moment', function () { }, 'to error with', 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + - 'to satisfy new Date(\'Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\')' + 'to satisfy new Date(\'Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\')\n' + + '\n' + + 'moment.utc( // should be in local time\n' + + ' new Date(\n' + + ' \'Fri Jan 01 2016 01:00:00 GMT+0100 (CET)\' // should be \'Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\'\n' + + ' // -Fri Jan 01 2016 01:00:00 GMT+0100 (CET)\n' + + ' // +Fri Jan 01 2016 00:00:00 GMT+0100 (CET)\n' + + ' )\n' + + ')' ); }); it('does not include the diff if the outputs are equal for a string value', function () { expect( function () { - expect(moment.utc('2016-01-01T00:00:00+00:00'), 'to satisfy', '2016-01-01T00:00:00+01:00'); + expect(moment.utc('2016-01-01T00:00:00+00:00'), 'to satisfy', '2016-01-01T00:00:00'); }, 'to error with', 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + - 'to satisfy \'2016-01-01T00:00:00+01:00\'' + 'to satisfy \'2016-01-01T00:00:00\'\n' + + '\n' + + 'moment.utc( // should be in local time\n' + + ' \'2016-01-01T00:00:00\'\n' + + ')' ); }); @@ -418,23 +488,13 @@ describe('unexpected-moment', function () { expect(moment.utc('2016-01-01T00:00:00+00:00'), 'to satisfy', 1451602800000); }, 'to error with', - 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + - 'to satisfy 1451602800000' - ); - }); - - // TODO - it.skip('throws the correct error for utc moments when passed a value that has no timezone information', function () { - expect( - function () { - expect(moment.utc('2016-01-01T00:00:00+00:00'), 'to satisfy', [2016, 0, 1, 0, 0, 0, 0]); - }, - 'to error with', - 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\')\n' + - 'to satisfy [ 2016, 0, 1, 0, 0, 0, 0 ]\n' + + 'expected moment.utc(\'2016-01-01T00:00:00.000+00:00\') ' + + 'to satisfy 1451602800000\n' + '\n' + - 'moment.utc( // should be moment(\n' + - ' [ 2016, 0, 1, 0, 0, 0, 0 ]\n' + + 'moment.utc( // should be in local time\n' + + ' 1451606400000 // should be 1451602800000\n' + + ' // -1451606400000\n' + + ' // +1451602800000\n' + ')' ); }); @@ -771,8 +831,11 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-02T00:00:00.000+01:00\') ' + 'to be the start of year\n' + '\n' + - '-moment(\'2016-01-02T00:00:00.000+01:00\')\n' + - '+moment(\'2016-01-01T00:00:00.000+01:00\')' + 'moment(\n' + + ' \'2016-01-02T00:00:00.000+01:00\' // should be \'2016-01-01T00:00:00.000+01:00\'\n' + + ' // -2016-01-02T00:00:00.000+01:00\n' + + ' // +2016-01-01T00:00:00.000+01:00\n' + + ')' ); }); @@ -846,8 +909,11 @@ describe('unexpected-moment', function () { 'expected moment(\'2016-01-02T00:00:00.000+01:00\') ' + 'to be the end of year\n' + '\n' + - '-moment(\'2016-01-02T00:00:00.000+01:00\')\n' + - '+moment(\'2016-12-31T23:59:59.999+01:00\')' + 'moment(\n' + + ' \'2016-01-02T00:00:00.000+01:00\' // should be \'2016-12-31T23:59:59.999+01:00\'\n' + + ' // -2016-01-02T00:00:00.000+01:00\n' + + ' // +2016-12-31T23:59:59.999+01:00\n' + + ')' ); }); From fc5ceeb23e845d4234469a800bfce17649ad3783 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Fri, 29 Jul 2016 00:49:59 +0200 Subject: [PATCH 33/35] Fix inspected moment output --- lib/unexpected-moment.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/unexpected-moment.js b/lib/unexpected-moment.js index d612ddf..4e1a314 100644 --- a/lib/unexpected-moment.js +++ b/lib/unexpected-moment.js @@ -29,7 +29,7 @@ }, {}); } - function diffMoments(actual, expected, output, diff, inspect, equal) { + function formatDiff(actual, expected, output, diff, inspect, equal) { var actualIsUtc = actual.isUtc(); var functionName = actualIsUtc ? 'moment.utc' : 'moment'; var expectedIsMoment = expected instanceof moment; @@ -158,10 +158,10 @@ var functionName = value.isUtc() ? 'moment.utc' : 'moment'; output.jsFunctionName(functionName) .text('(') - .jsString(inspect(value.format(outputFormat))) + .append(inspect(value.format(outputFormat))) .text(')'); }, - diff: diffMoments + diff: formatDiff }); expect.addAssertion(' [not] to be a moment', function (expect, subject) { @@ -255,7 +255,7 @@ inspectDate(value, output); }; } - return diffMoments(subject, value, output, diff, inspect, equal); + return formatDiff(subject, value, output, diff, inspect, equal); } }); } From b30ca026d7bc16e3b8d7ccf9d98f5d4eea8f5b9d Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Fri, 29 Jul 2016 00:50:26 +0200 Subject: [PATCH 34/35] Update documentation examples --- documentation/assertions/moment/to-be-the-end-of.md | 7 +++++-- .../assertions/moment/to-be-the-start-of.md | 7 +++++-- documentation/assertions/moment/to-equal.md | 7 +++++-- documentation/assertions/moment/to-satisfy.md | 12 +++++++----- documentation/index.md | 7 +++++-- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/documentation/assertions/moment/to-be-the-end-of.md b/documentation/assertions/moment/to-be-the-end-of.md index 06eaa6c..fec16dd 100644 --- a/documentation/assertions/moment/to-be-the-end-of.md +++ b/documentation/assertions/moment/to-be-the-end-of.md @@ -15,6 +15,9 @@ expect(moment('2015-01-01 00:00:00'), 'to be the end of day'); ```output expected moment('2015-01-01T00:00:00.000+01:00') to be the end of day --moment('2015-01-01T00:00:00.000+01:00') -+moment('2015-01-01T23:59:59.999+01:00') +moment( + '2015-01-01T00:00:00.000+01:00' // should be '2015-01-01T23:59:59.999+01:00' + // -2015-01-01T00:00:00.000+01:00 + // +2015-01-01T23:59:59.999+01:00 +) ``` diff --git a/documentation/assertions/moment/to-be-the-start-of.md b/documentation/assertions/moment/to-be-the-start-of.md index 4a16da8..2763b4f 100644 --- a/documentation/assertions/moment/to-be-the-start-of.md +++ b/documentation/assertions/moment/to-be-the-start-of.md @@ -15,6 +15,9 @@ expect(moment('2015-01-01 01'), 'to be the start of year'); ```output expected moment('2015-01-01T01:00:00.000+01:00') to be the start of year --moment('2015-01-01T01:00:00.000+01:00') -+moment('2015-01-01T00:00:00.000+01:00') +moment( + '2015-01-01T01:00:00.000+01:00' // should be '2015-01-01T00:00:00.000+01:00' + // -2015-01-01T01:00:00.000+01:00 + // +2015-01-01T00:00:00.000+01:00 +) ``` diff --git a/documentation/assertions/moment/to-equal.md b/documentation/assertions/moment/to-equal.md index d57a1b5..b79fb69 100644 --- a/documentation/assertions/moment/to-equal.md +++ b/documentation/assertions/moment/to-equal.md @@ -30,6 +30,9 @@ expect(moment('2015-11-01'), 'to equal', moment('2015-11-02'), 'in days'); expected moment('2015-11-01T00:00:00.000+01:00') to equal moment('2015-11-02T00:00:00.000+01:00') in days --moment('2015-11-01T00:00:00.000+01:00') -+moment('2015-11-02T00:00:00.000+01:00') +moment( + '2015-11-01T00:00:00.000+01:00' // should be '2015-11-02T00:00:00.000+01:00' + // -2015-11-01T00:00:00.000+01:00 + // +2015-11-02T00:00:00.000+01:00 +) ``` diff --git a/documentation/assertions/moment/to-satisfy.md b/documentation/assertions/moment/to-satisfy.md index 0529bdb..4389bf5 100644 --- a/documentation/assertions/moment/to-satisfy.md +++ b/documentation/assertions/moment/to-satisfy.md @@ -21,9 +21,11 @@ expect(moment('2015-11-01'), 'to satisfy', { year: 2015, month: 11, day: 1 }); expected moment('2015-11-01T00:00:00.000+01:00') to satisfy { year: 2015, month: 11, day: 1 } -{ - years: 2015, - months: 10, // should equal 11 - date: 1 -} +moment( + { + years: 2015, + months: 10, // should equal 11 + date: 1 + } +) ``` diff --git a/documentation/index.md b/documentation/index.md index 3747be9..5374327 100644 --- a/documentation/index.md +++ b/documentation/index.md @@ -24,8 +24,11 @@ expect(date, 'to equal', moment('2015-11-03')); expected moment('2015-11-06T00:00:00.000+01:00') to equal moment('2015-11-03T00:00:00.000+01:00') --moment('2015-11-06T00:00:00.000+01:00') -+moment('2015-11-03T00:00:00.000+01:00') +moment( + '2015-11-06T00:00:00.000+01:00' // should be '2015-11-03T00:00:00.000+01:00' + // -2015-11-06T00:00:00.000+01:00 + // +2015-11-03T00:00:00.000+01:00 +) ``` [![NPM version](https://badge.fury.io/js/unexpected-moment.svg)](http://badge.fury.io/js/unexpected-moment) From 3a7d8b7a52853d56a1c360454979371eae880557 Mon Sep 17 00:00:00 2001 From: Joel Mukuthu Date: Fri, 29 Jul 2016 00:56:20 +0200 Subject: [PATCH 35/35] Set TZ for all scripts --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 7d72d28..c01d567 100644 --- a/package.json +++ b/package.json @@ -9,10 +9,10 @@ "scripts": { "lint": "eslint .", "test": "TZ='Europe/Copenhagen' mocha test/**/*.js documentation/**/*.md && npm run lint", - "travis": "npm test && npm run coverage && (