diff --git a/README.md b/README.md index e5c939c..5fc9791 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# ProjectName +# Timeshift + +TODO: docs and open-source diff --git a/package.json b/package.json index 9142e29..d087786 100644 --- a/package.json +++ b/package.json @@ -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 (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", diff --git a/src/index.js b/src/index.js index 2fc084a..f5a7287 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,8 @@ function timeshift(...args) { FakeDate.now = () => OriginalDate.now() - diff; Date = FakeDate; + + return OriginalDate; } module.exports = timeshift; diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..755c41d --- /dev/null +++ b/test/index.js @@ -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(); +});