Use lolex for faking date in tests (#491)

Sinon.JS provides a project called lolex to handle faking dates. This
change replaces the fakeDate utility with the equivalent Sinon.JS code.
This commit is contained in:
Tim Oram
2018-06-16 18:36:05 +02:00
committed by Eduardo Diaz
parent 7e40174623
commit cc61447047
3 changed files with 5 additions and 36 deletions
+2 -2
View File
@@ -4,7 +4,7 @@
"description": "JSON Web Token implementation (symmetric and asymmetric)",
"main": "index.js",
"scripts": {
"test": "nyc --reporter=html --reporter=text mocha --require test/util/fakeDate && nsp check && cost-of-modules"
"test": "nyc --reporter=html --reporter=text mocha && nsp check && cost-of-modules"
},
"repository": {
"type": "git",
@@ -37,7 +37,7 @@
"mocha": "^2.1.0",
"nsp": "^2.6.2",
"nyc": "^11.8.0",
"sinon": "^1.15.4"
"sinon": "^6.0.0"
},
"engines": {
"npm": ">=1.4.28",
+3 -2
View File
@@ -5,6 +5,7 @@ var path = require('path');
var expect = require('chai').expect;
var assert = require('chai').assert;
var ms = require('ms');
var sinon = require('sinon');
function loadKey(filename) {
return fs.readFileSync(path.join(__dirname, filename));
@@ -140,14 +141,14 @@ describe('Asymmetric Algorithms', function(){
it('should valid when date are equals', function (done) {
Date.fix(1451908031);
var fakeClock = sinon.useFakeTimers({now: 1451908031});
token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm, notBefore: 0 });
jwt.verify(token, pub, function (err, decoded) {
fakeClock.uninstall();
assert.isNull(err);
assert.isNotNull(decoded);
Date.unfix();
done();
});
});
-32
View File
@@ -1,32 +0,0 @@
var oldDate = global.Date;
/*
* fix new Date() to a fixed unix timestamp.
*/
global.Date.fix = function (timestamp) {
var time = timestamp * 1000;
if (global.Date.unfake) {
global.Date.unfake();
}
global.Date = function (ts) {
return new oldDate(ts || time);
};
global.Date.prototype = Object.create(oldDate.prototype);
global.Date.prototype.constructor = global.Date;
global.Date.prototype.now = function () {
return time;
};
global.Date.now = function () {
return time;
};
global.Date.unfix = function () {
global.Date = oldDate;
};
};