Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chai-datetime

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chai-datetime - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

74

chai-datetime.js

@@ -41,5 +41,45 @@ (function(plugin){

'expected ' + actualDate + ' to not equal ' + expectedDate
)
);
});
chai.Assertion.addChainableMethod('beforeDate', function(date) {
var actual = this._obj;
this.assert(
actual.getUTCFullYear <= date.getUTCFullYear && actual.getUTCMonth() <= date.getUTCMonth() && actual.getUTCDate() < date.getUTCDate(),
'expected ' + actual.toDateString() + ' to be before ' + date.toDateString(),
'expected ' + actual.toDateString() + ' not to be before ' + date.toDateString()
);
});
chai.Assertion.addChainableMethod('afterDate', function(date) {
var actual = this._obj;
this.assert(
actual.getUTCFullYear >= date.getUTCFullYear && actual.getUTCMonth() >= date.getUTCMonth() && actual.getUTCDate() > date.getUTCDate(),
'expected ' + actual.toDateString() + ' to be after ' + date.toDateString(),
'expected ' + actual.toDateString() + ' not to be after ' + date.toDateString()
);
});
chai.Assertion.addChainableMethod('beforeTime', function(time) {
var actual = this._obj;
this.assert(
actual.getTime() < time.getTime(),
'expected ' + actual + ' to be before ' + time,
'expected ' + actual + ' not to be before ' + time
);
});
chai.Assertion.addChainableMethod('afterTime', function(time) {
var actual = this._obj;
this.assert(
actual.getTime() > time.getTime(),
'expected ' + actual + ' to be after ' + time,
'expected ' + actual + ' not to be after ' + time
);
});
// Asserts

@@ -56,2 +96,18 @@ var assert = chai.assert;

assert.beforeDate = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.beforeDate(exp);
}
assert.notBeforeDate = function(val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.beforeDate(exp);
}
assert.afterDate = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.afterDate(exp);
}
assert.notAfterDate = function(val, exp, msg) {
new chai.Assertion(val, msg).not.to.be.afterDate(exp);
}
assert.equalTime = function(val, exp, msg) {

@@ -65,2 +121,18 @@ new chai.Assertion(val, msg).to.be.equalTime(exp);

assert.beforeTime = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.beforeTime(exp);
}
assert.notBeforeTime = function(val, exp, msg) {
new chai.Assertion(val, msg).not.to.be.beforeTime(exp);
}
assert.afterTime = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.afterTime(exp);
}
assert.notAfterTime = function(val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.afterTime(exp);
}
}));

4

package.json
{
"name": "chai-datetime",
"version": "1.0.0",
"version": "1.1.0",
"keywords": ["chai", "testing", "jasmine"],
"description": "date / time equality matchers for chai",
"description": "date / time comparison matchers for chai",
"main": "chai-datetime.js",

@@ -7,0 +7,0 @@ "scripts": {

# chai-datetime
Matchers for chai to help with common date equality assertions against
Matchers for chai to help with common date comparison assertions against
JavaScript Date objects.

@@ -16,2 +16,13 @@

### Better Error Messages
Comparing date values for equality with getTime() gives unreadable
error messages:
AssertionError: expected 1369944360000 to equal 1369944300000
Use chai-datetime to get something easier to read:
AssertionError: expected Thu May 30 2013 16:06:00 GMT-0400 (EDT) to equal Thu May 30 2013 16:05:00 GMT-0400 (EDT)
## Usage

@@ -35,35 +46,22 @@

### equalTime
There are a collection of assertions that work on times and dates. Any
assertion that specifies date in the name only compares the date
portion of the Date object.
Value comparison for Date objects, including their time component.
* equalTime
* beforeTime
* afterTime
* equalDate
* beforeDate
* afterDate
```javascript
var subject = new Date(2013, 4, 30, 16, 5);
All assertions are defined for both the BDD and TDD syntaxes.
subject.should.equalTime(new Date(2013, 4, 30, 16, 5));
subject.should.not.equalTime(new Date(2013, 4, 30, 16, 6));
expect(subject).to.equalTime(new Date(2013, 4, 30, 16, 5));
expect(subject).not.to.equalTime(new Date(2013, 4, 30, 16, 6));
assert.equalTime(subject, new Date(2013, 4, 30, 16, 5));
assert.notEqualTime(subject, new Date(2013, 4, 30, 16, 6));
```
### equalDate
Value comparison for Date objects, only comparing the date portion,
ignoring the time.
```javascript
var subject = new Date(2013, 4, 30, 16, 5);
var d1 = new Date(2013, 4, 30, 16, 5),
d2 = new Date(2013, 4, 30, 17);
subject.should.equalDate(new Date(2013, 4, 30, 16, 6));
subject.should.not.equalDate(new Date(2013, 4, 29, 16, 6));
expect(subject).to.equalDate(new Date(2013, 4, 30, 16, 6));
expect(subject).not.to.equalDate(new Date(2013, 4, 29, 16, 6));
assert.equalTime(subject, new Date(2013, 4, 30, 16, 6));
assert.notEqualTime(subject, new Date(2013, 4, 29, 16, 6));
d1.should.equalDate.d2
expect(d1).to.equalDate(d2)
assert.equalDate(d1, d2)
```

@@ -70,0 +68,0 @@

@@ -144,2 +144,243 @@ (function(test){

describe('beforeDate', function() {
describe('when given two different date objects', function() {
beforeEach(function() {
this.d1 = new Date(2013, 4, 30)
this.d2 = new Date(2013, 4, 31)
});
it('passes', function() {
this.d1.should.be.beforeDate(this.d2);
});
describe('when negated', function() {
it('fails', function() {
var test = this;
(function() {
test.d1.should.not.be.beforeDate(test.d2);
}).should.fail(
'expected Thu May 30 2013 not to be before Fri May 31 2013'
);
});
});
});
describe('when given two identical date objects', function() {
beforeEach(function() {
this.d1 = new Date(2013, 4, 30)
this.d2 = new Date(2013, 4, 30)
});
it('fails', function() {
var test = this;
(function() {
test.d1.should.be.beforeDate(test.d2);
}).should.fail(
'expected Thu May 30 2013 to be before Thu May 30 2013'
);
});
describe('when negated', function() {
it('passes', function() {
this.d1.should.not.be.beforeDate(this.d2);
});
});
});
describe('when given two identical dates but different times', function() {
beforeEach(function() {
this.d1 = new Date(2013, 4, 30, 17)
this.d2 = new Date(2013, 4, 30, 18)
});
it('fails', function() {
var test = this;
(function() {
test.d1.should.be.beforeDate(test.d2);
}).should.fail(
'expected Thu May 30 2013 to be before Thu May 30 2013'
);
});
describe('when negated', function() {
it('passes', function() {
this.d1.should.not.be.beforeDate(this.d2);
});
});
});
});
describe('afterDate', function() {
describe('when given two different date objects', function() {
beforeEach(function() {
this.d1 = new Date(2013, 4, 31);
this.d2 = new Date(2013, 4, 30);
});
it('passes', function() {
this.d1.should.be.afterDate(this.d2);
});
describe('when negated', function() {
it('fails', function() {
var test = this;
(function() {
test.d1.should.not.be.afterDate(test.d2);
}).should.fail(
'expected Fri May 31 2013 not to be after Thu May 30 2013'
);
});
});
});
describe('when given two identical date objects', function() {
beforeEach(function() {
this.d1 = new Date(2013, 4, 30);
this.d2 = new Date(2013, 4, 30);
});
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);
});
});
})
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);
});
});
})
});
describe('beforeTime', function() {
describe('when comparing two different times', function() {
beforeEach(function() {
this.d1 = new Date(2013, 4, 30, 16, 5, 0)
this.d2 = new Date(2013, 4, 30, 16, 5, 1)
});
it('passes', function() {
this.d1.should.be.beforeTime(this.d2);
});
describe('when negated', function() {
it('fails', function() {
var test = this;
(function() {
test.d1.should.not.be.beforeTime(test.d2);
}).should.fail(
'expected Thu May 30 2013 16:05:00 GMT-0400 (EDT) not to be before Thu May 30 2013 16:05:01 GMT-0400 (EDT)'
);
});
});
});
describe('when comparing identical times', function() {
beforeEach(function() {
this.d1 = new Date(2013, 4, 30, 16, 5, 0);
this.d2 = new Date(2013, 4, 30, 16, 5, 0);
});
it('fails', function() {
var test = this;
(function() {
test.d1.should.be.beforeTime(test.d2);
}).should.fail(
'expected Thu May 30 2013 16:05:00 GMT-0400 (EDT) to be before Thu May 30 2013 16:05:00 GMT-0400 (EDT)'
);
});
describe('when negated', function() {
it('passes', function() {
this.d1.should.not.be.beforeTime(this.d2);
});
});
});
});
describe('afterTime', function() {
describe('when comparing two different times', function() {
beforeEach(function() {
this.d1 = new Date(2013, 4, 30, 16, 5, 1);
this.d2 = new Date(2013, 4, 30, 16, 5, 0);
});
it('passes', function() {
this.d1.should.be.afterTime(this.d2)
});
describe('when negated', function() {
it('fails', function() {
var test = this;
(function() {
test.d1.should.not.be.afterTime(test.d2)
}).should.fail(
'expected Thu May 30 2013 16:05:01 GMT-0400 (EDT) not to be after Thu May 30 2013 16:05:00 GMT-0400 (EDT)'
);
});
});
});
describe('when comparing two identical times', function() {
beforeEach(function() {
this.d1 = new Date(2013, 4, 30, 16, 5, 0);
this.d2 = new Date(2013, 4, 30, 16, 5, 0);
});
it('fails', function() {
var test = this;
(function() {
test.d1.should.be.afterTime(test.d2)
}).should.fail(
'expected Thu May 30 2013 16:05:00 GMT-0400 (EDT) to be after Thu May 30 2013 16:05:00 GMT-0400 (EDT)'
);
});
describe('when negated', function() {
it('passes', function() {
this.d1.should.not.be.afterTime(this.d2);
});
});
});
});
describe('tdd alias', function() {

@@ -158,2 +399,18 @@ beforeEach(function() {

it('.beforeDate', function() {
assert.beforeDate(this.subject, new Date(2013, 4, 31));
});
it('.notBeforeDate', function() {
assert.notBeforeDate(this.subject, new Date(2013, 4, 30));
});
it('.afterDate', function() {
assert.afterDate(this.subject, new Date(2013, 4, 29));
});
it('.notAfterDate', function() {
assert.notAfterDate(this.subject, new Date(2013, 4, 30));
});
it('.equalTime', function() {

@@ -167,4 +424,20 @@ assert.equalTime(this.subject, new Date(2013, 4, 30, 16, 5));

it('.beforeTime', function() {
assert.beforeTime(this.subject, new Date(2013, 4, 30, 16, 6));
});
it('.notBeforeTime', function() {
assert.notBeforeTime(this.subject, new Date(2013, 4, 30, 16, 5));
});
it('.afterTime', function() {
assert.afterTime(this.subject, new Date(2013, 4, 30, 16, 4));
});
it('.notAfterTime', function() {
assert.notAfterTime(this.subject, new Date(2013, 4, 30, 16, 6));
});
});
});
}));
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc