mguterl / chai-datetime Goto Github PK
View Code? Open in Web Editor NEWMatchers for chai to help with common date equality assertions against JavaScript Date objects.
License: MIT License
Matchers for chai to help with common date equality assertions against JavaScript Date objects.
License: MIT License
I'm currently working on a PR for this, but I wanted to see if the community/author would be interested.
Sometimes I find that I need to test if a date/time is before or equal to another date/time. Moment has isSameOrBefore
/isSameOrAfter
, so I thought something similar would be useful for this library.
I propose adding the following assertions:
beforeOrEqualTime
afterOrEqualTime
beforeOrEqualDate
afterOrEqualDate
Or, if someone knows a way that this can easily be achieved with the existing library, please let me know.
Does this need one?
Example of comparing two dates that differ by less than 1 second: expect(actualUTCDate).to.equalTime yesterdayStartOfDayUTC
Output (indented for clarity):
AssertionError: expected
Mon Mar 23 2015 11:00:00 GMT+1100 (AUS Eastern Daylight Time) to equal
Mon Mar 23 2015 11:00:00 GMT+1100 (AUS Eastern Daylight Time)
It would be nice if the time difference found between two different dates could be given to avoid confusion.
I'm currently testing timestamps that come back from a database where the timestamp is automatically created based on the time of the insertion/update.
It would be neat if chai-datetime could do a "fuzzy" datetime match where you pass an interval and it would return true if the time is within the given time +/- the interval.
For example,
var d1 = new Date(2013, 4, 30, 16, 5),
d2 = new Date(2013, 4, 30, 17);
// Interval provided in seconds, would match if d1 is within 5 seconds of d2
d1.should.fuzzyEqualTime(d2, '5s');
This is sort of a helper method instead of using beforeTime and afterTime so I would understand if you prefer to keep the library slim. It's not too big of a deal to write a small helper that does this already.
Thanks for a great library!
The condition for afterDate
to pass is
actual.getUTCFullYear() > expected.getUTCFullYear()
|| actual.getUTCMonth() > expected.getUTCMonth()
|| actual.getUTCDate() > expected.getUTCDate();
For simplicity, let's assume that all dates are in UTC already.
So, if actual is 2014-01-31 and expected is 2014-12-01, this method would indicate that January 31 is after December 1 of the same year because 31 > 1.
beforeDate
works in a similar way and has the inverse problem (it would compute that 2048-01-01 is before 2014-12-31).
I took a stab at fixing this myself, but realized that converting the dates to UTC before running date calculations has some unintended consequences. For example, two tests from the existing test suite will fail if you run them in the PDT timezone:
describe('when given two identical date objects with different times', function() {
beforeEach(function() {
this.d1 = new Date(2013, 4, 30, 17);
this.d2 = new Date(2013, 4, 30, 16);
});
it('fails', function() {
var test = this;
(function() {
test.d1.should.be.afterDate(test.d2);
}).should.fail(
'expected Thu May 30 2013 to be after Thu May 30 2013'
);
});
describe('when negated', function() {
it('passes', function() {
this.d1.should.not.be.afterDate(this.d2);
});
});
});
Set up:
"devDependencies": {
"chai": "^3.5.0",
"chai-datetime": "^1.4.1",
"chai-spies": "^0.7.1",
"mocha": "^3.3.0",
"nock": "^9.0.13",
"proxyquire": "^1.7.11",
"sinon": "^2.1.0",
"sinon-chai": "^2.9.0",
"sinon-stub-promise": "^4.0.0"
}
chai = require('chai');
var sinonStubPromise = require('sinon-stub-promise');
var sinon = require('sinon');
sinonStubPromise(sinon);
chai.use(require('sinon-chai'));
chai.use(require('chai-datetime'));
global.should = chai.should();
global.sinon = sinon;
global.proxy = require('proxyquire');
global.nock = require('nock');
test:
it('should set normal alert time stamp', ()=> {
normalAlertStatus.setLock();
normalAlertStatus.lockTimeStamp.should.be.afterTime(222);
});
result:
1) foo When setting lock on normal alert should set normal alert time stamp:
TypeError: actual.getTime is not a function
at Object.chai.datetime.afterTime (node_modules/chai-datetime/chai-datetime.js:88:19)
at Assertion.<anonymous> (node_modules/chai-datetime/chai-datetime.js:148:21)
at Assertion.assert (node_modules/chai/lib/chai/utils/addChainableMethod.js:84:49)
at Context.it (test/locks.js:33:49)
When I try to make the assertion below:
var date1 = '2016-01-01T10:00:14.876Z';
var date2 = new Date(date1);
expect(date1).to.equalDate(date2);
I get this exception:
TypeError: date.toDateString is not a function
at Object.chai.datetime.formatDate (node_modules/chai-datetime/chai-datetime.js:51:17)
at Assertion.<anonymous> (node_modules/chai-datetime/chai-datetime.js:105:39)
at Assertion.assert (node_modules/chai/lib/chai/utils/addChainableMethod.js:83:49)
at Context.<anonymous> (test/middlewares/lookup_response_action.spec.js:9:5708)
Any ideas?
Something like
expect("foobar").to.equalDate(new Date());
causes the code to throw
TypeError: date.toDateString is not a function
However, it would be preferable if the lib throws an assertion error, which makes the problem more clear.
AssertionError: expected non-date object (foobar) to equal Thu June 29 2016
This issue is different from #26 where the author apparently wants the lib to accept strings as actual values.
Hey there,
Thanks for this great chai addition. I'm implementing a custom date picker with linked pickers. The second picker initially gets a date 2 weeks after the first pickers date. To test this I wrote the following spec:
it('should initially set the linkedFuturePicker minDate when a date has been selected', function() {
this.subjectA.focus();
$('.ui-datepicker-calendar:first a:nth(8)').click();
this.subjectB.dtpicker('getDate').should.afterDate(this.subjectA.dtpicker('getDate'));
});
Even though the date of the second date picker is after the date of the first picker, my assert fails:
1) DtPicker Linked DatePickers should initially set the linkedFuturePicker minDate when a date has been selected:
AssertionError: expected Thu Jan 02 2014 to be after Thu Dec 26 2013
Hi Guys! This looks like an awesome plugin.
If interested it can be listed on the Chai.js Plugin Directory. Please open a PR for the chai-docs project with info when you are ready for it to be listed.
I'm working with a hotel API where I want to TDD arrival & departure dates. I'd like to simplify:
expect(jsonResponse.arrivalDate).to.equalDate(new Date('2017-01-01'));
To:
expect(jsonResponse.arrivalDate).to.equalDate('2017-01-01');
Essentially if chai-datetime is asked to match a string to a date it would wrap it in new Date(str)
to allow a short-hand.
I'm happy to raise a PR but wanted to check if it's a desirable feature or if I'm the only lazy one out there.
I have followed your read me file to install this fab package and I have also used
npm install @types/chai-datetime
as advised by VSCode, but alas, I am still having problems getting it to be recognised when I try to import the modules. I see this message:
Module "local address of my respository/node_modules/@types/chai-datetime/index" has no exported member 'equaldate'
equalDate being the module I am trying to import using
import { equalDate } from "chai-datetime";
I have even re-installed the package as a global.
Thanks
Currently chai-datetime depends on chai 1.2, and the latest is 1.9. Would you please update it to ^1.9.0?
It kind of seems like it might be dead because the most recent un-resolved issues are all from 2016 or earlier.
If it's not dead a little documentation of the other functions (eg. in what order do I pass arguments to withinTime
?) would be handy.
After reviewing the release notes of Chai, I think that there wouldn't be any harm to allow chai-datetime to use >1.9.0
instead of ^1.9.0
.
I'm using it already manually with chai 2.1.1 but I want to avoid getting warning messages due to npm package version issues.
npm WARN unmet dependency ...\node_modules\chai-datetime requires chai@ but will load
npm WARN unmet dependency ...\node_modules\chai,
npm WARN unmet dependency which is version 2.1.1
[email protected] project
├── [email protected] invalid
├── [email protected]
├── [email protected]
...
npm ERR! invalid: [email protected] c:\itrac\workspace\itrac\itrac-web\node_modules\chai
npm ERR! not ok code 0
@types/[email protected]
is missing types for beforeOrEqualTime
and some other checks. Are these generated from this project or is this a separate (DefinitelyTyped?) project?
Seems to fit with the concept to be able to assert if one Date is before or after the other (with a pretty message).
With the negation you can then also check before-or-equal (same as not.afterDate) and after-or-equal (same as not.beforeDate).
Is there any way to get deep comparisons (in my case, expect(object).to.deep.include(baseObject)
) to use chai-datetime comparisons so that dates in deep locations actually compare properly?
To allow your library to be installed using bower, this repo needs proper tags. Could you tag the latest version and all following releases in git?
Thanks :-)
Forgive my ignorance but is this something that can be used in a JS application to for example determine if a date record is between 2 date ranges or is this more for running test?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.