Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dherault committed Jan 17, 2017
1 parent c13e76a commit 5cacd8b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# ProjectName
# Timeshift

TODO: docs and open-source
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{
"name": "@nelson/timeshift",
"name": "timeshift",
"version": "0.0.0",
"description": "Time shifting in JavaScript",
"author": "Nelson-ai",
"author": "David Hérault <dherault@nelson.ai> (https://github.com/dherault)",
"license": "SEE LICENSE IN LICENSE",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 0"
"test": "./node_modules/.bin/mocha $npm_package_options_mocha",
"coverage": "./node_modules/.bin/istanbul cover _mocha -- $npm_package_options_mocha",
"coverage:serve": "cd coverage/lcov-report && python -m SimpleHTTPServer",
"coverage:all": "npm run coverage && npm run coverage:serve"
},
"options": {
"mocha": "--bail --check-leaks"
},
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function timeshift(...args) {
FakeDate.now = () => OriginalDate.now() - diff;

Date = FakeDate;

return OriginalDate;
}

module.exports = timeshift;
46 changes: 46 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* global it */
const { assert } = require('chai');
const timeshift = require('..');

const OriginalDate = Date;

it('travels throught time', () => {

const frenchRevolution = new Date('1789-07-14');

timeshift('1789-07-14');

// 5 ms margin error for slow machines
assert.approximately(new Date().getTime(), frenchRevolution.getTime(), 5);
});

it('can come back safe', () => {

timeshift();

assert.strictEqual(Date, OriginalDate);
});

// Test Date constructor with arguments after shifting
it('knows what happended last summer', () => {

const lastSummer = new Date('2016-07-14');

// 1 hour until the new millenium, best party ever
timeshift('2999-12-31T23:00:00');

assert.strictEqual(new Date('2016-07-14').getTime(), lastSummer.getTime());

timeshift();
});

// Test Date.now after shifting
it('savours the moment', () => {
// 1970-01-01 the begining of JavaScript time
timeshift(0);

// Let us witness the big bang of JavaScript
assert.approximately(Date.now(), 0, 5);

timeshift();
});

0 comments on commit 5cacd8b

Please sign in to comment.