timekeeper
Advanced tools
Comparing version
@@ -72,3 +72,3 @@ /** | ||
if (!length && freeze) return freeze; | ||
if (!length && freeze) return new NativeDate(freeze.getTime()); | ||
if (!length && travel) return new NativeDate(time()); | ||
@@ -130,3 +130,3 @@ | ||
/** | ||
* Set current Date Tieme and freeze it. | ||
* Set current Date Time and freeze it. | ||
* | ||
@@ -203,2 +203,2 @@ * @param {Object|String|Number} Date. | ||
return timekeeper; | ||
}); | ||
}); |
{ | ||
"name": "timekeeper", | ||
"description": "Easy testing of time-dependent code.", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"keywords": [ | ||
@@ -12,11 +12,6 @@ "fake date", | ||
"contributors": [ | ||
{ | ||
"name": "Oleg Slobodskoi", | ||
"email": "oleg008@gmail.com" | ||
}, | ||
{ | ||
"name": "Guilherme Tramontina", | ||
"email": "guilherme.tramontina@gmail.com" | ||
} | ||
"Oleg Slobodskoi <oleg008@gmail.com>", | ||
"Guilherme Tramontina <guilherme.tramontina@gmail.com>" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -26,8 +21,7 @@ "chai": "^1.9.2", | ||
"mocha": "^2.0.1", | ||
"mocha-phantomjs": "^3.5.1", | ||
"phantomjs": "^1.9.12" | ||
"mocha-phantomjs": "^4.0.2" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/vesln/timekeeper.git" | ||
"url": "git+ssh://git@github.com/vesln/timekeeper.git" | ||
}, | ||
@@ -38,3 +32,10 @@ "homepage": "http://github.com/vesln/timekeeper", | ||
}, | ||
"main": "./lib/timekeeper.js" | ||
"main": "./lib/timekeeper.js", | ||
"bugs": { | ||
"url": "https://github.com/vesln/timekeeper/issues" | ||
}, | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -1,7 +0,11 @@ | ||
[](http://travis-ci.org/vesln/timekeeper) | ||
# timekeeper | ||
[![travis][travis-image]][travis-url] | ||
[![npm][npm-image]][npm-url] | ||
## Description | ||
[travis-image]: https://travis-ci.org/vesln/timekeeper.svg?branch=master | ||
[travis-url]: https://travis-ci.org/vesln/timekeeper | ||
[npm-image]: https://img.shields.io/npm/v/timekeeper.svg?style=flat | ||
[npm-url]: https://npmjs.org/package/timekeeper | ||
This module mocks `Date` and `Date.now` in order to help you test time-dependent code. | ||
@@ -12,9 +16,9 @@ Provides `travel` and `freeze` functionality for your Node.js tests. | ||
- Please note, that if you are using time freezing, the `setTimeout` and | ||
`setInteval` won't work as exepcted, since they are using the `Date` | ||
class but the time will not change until you call `timekeeper#reset`. | ||
- Please note, that if you are using time freezing, `setTimeout` and | ||
`setInteval` won't work as expected, since they are using the `Date` | ||
class, but the time will not change until you call `timekeeper#reset`. | ||
## Installation | ||
- NPM: `npm install timekeeper --save` | ||
- NPM: `npm install timekeeper --save-dev` | ||
- Bower: `bower install timekeeper` | ||
@@ -72,3 +76,2 @@ | ||
## Requirements | ||
@@ -79,8 +82,2 @@ | ||
## Install | ||
``` | ||
$ npm install timekeeper | ||
``` | ||
## Tests | ||
@@ -96,3 +93,3 @@ | ||
Inspired by the [timecop](https://github.com/jtrupiano/timecop) ruby gem. | ||
Inspired by the [timecop](https://github.com/travisjeffery/timecop) ruby gem. | ||
@@ -99,0 +96,0 @@ ## License |
@@ -8,17 +8,2 @@ /** | ||
/** | ||
* Sleep implementation. | ||
* | ||
* Thanks to Stoyan Stefanov. | ||
* http://www.phpied.com/sleep-in-javascript/ | ||
* | ||
* @param {Number} Milliseconds. | ||
*/ | ||
function sleep(milliseconds) { | ||
var start = new Date().getTime(); | ||
for (var i = 0; i < 1e7; i++) { | ||
if ((new Date().getTime() - start) > milliseconds) break; | ||
} | ||
}; | ||
describe('TimeKeeper', function() { | ||
@@ -32,20 +17,32 @@ describe('freeze', function() { | ||
it('freezes the time create with `new Date` to the supplied one', function() { | ||
sleep(10); | ||
var date = new Date; | ||
date.getTime().should.eql(this.time.getTime()); | ||
afterEach(function() { | ||
tk.reset(); | ||
}); | ||
it('freezes the time create with `Date#now` to the supplied one', function() { | ||
sleep(10); | ||
Date.now().should.eql(this.time.getTime()); | ||
tk.reset(); | ||
it('freezes the time create with `new Date` to the supplied one', function(done) { | ||
setTimeout(function(time) { | ||
var date = new Date(); | ||
date.getTime().should.eql(time.getTime()); | ||
done(); | ||
}, 10, this.time); | ||
}); | ||
it('freezes the time create with `Date#now` to the supplied one', function(done) { | ||
setTimeout(function(time) { | ||
Date.now().should.eql(time.getTime()); | ||
done(); | ||
}, 10, this.time); | ||
}); | ||
it('should not affect other date calls', function() { | ||
tk.freeze(this.time); | ||
(new Date(1330688329320)).getTime().should.eql(1330688329320); | ||
tk.reset(); | ||
}); | ||
it('should be immutable', function() { | ||
var firstDate = new Date(); | ||
firstDate.setFullYear(2001); | ||
var secondDate = new Date(); | ||
secondDate.getFullYear().should.not.equal(2001); | ||
}) | ||
}); | ||
@@ -59,9 +56,14 @@ }); | ||
tk.travel(this.time); | ||
sleep(10); | ||
}); | ||
afterEach(function() { | ||
tk.reset(); | ||
}); | ||
describe('and used with `new Date`', function() { | ||
it('should set the current date time to the supplied one', function() { | ||
(new Date).getTime().should.be.greaterThan(this.time.getTime()); | ||
tk.reset(); | ||
it('should set the current date time to the supplied one', function(done) { | ||
setTimeout(function(time) { | ||
(new Date).getTime().should.be.greaterThan(time.getTime()); | ||
done(); | ||
}, 10, this.time); | ||
}); | ||
@@ -71,6 +73,7 @@ }); | ||
describe('and used with `Date#now`', function() { | ||
it('should set the current date time to the supplied one', function() { | ||
sleep(10); | ||
Date.now().should.be.greaterThan(this.time.getTime()); | ||
tk.reset(); | ||
it('should set the current date time to the supplied one', function(done) { | ||
setTimeout(function(time) { | ||
Date.now().should.be.greaterThan(time.getTime()); | ||
done(); | ||
}, 10, this.time); | ||
}); | ||
@@ -77,0 +80,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
12965
2.95%4
-20%284
0.71%0
-100%115
-2.54%