timekeeper
Advanced tools
Comparing version 2.1.2 to 2.2.0
@@ -8,2 +8,3 @@ export as namespace timekeeper; | ||
isKeepingTime(): boolean; | ||
withFreeze<T>(date: Date | number | string, callback: ()=>T): T; | ||
} | ||
@@ -10,0 +11,0 @@ |
@@ -177,2 +177,35 @@ /** | ||
/** | ||
* Set current DateTime and reset. | ||
* | ||
* @param {Object|String|Number} date. | ||
* @param {Function} callback. | ||
* @api public | ||
*/ | ||
timekeeper.withFreeze = function (date, callback) { | ||
// Freeze time | ||
timekeeper.freeze(date); | ||
// Call callback | ||
var result; | ||
try { | ||
result = callback(); | ||
} catch (err) { | ||
// Reset | ||
timekeeper.reset(); | ||
throw err; | ||
} | ||
// If callback is async function | ||
if (result !== null && typeof result === 'object' && typeof result.then === 'function') { | ||
result.then( | ||
function() { timekeeper.reset(); }, | ||
function() { timekeeper.reset(); } | ||
); | ||
return result; | ||
} else { | ||
// Reset | ||
timekeeper.reset(); | ||
return result; | ||
} | ||
}; | ||
/** | ||
@@ -179,0 +212,0 @@ * Reflection: Are we currently modifying the native Date object? |
{ | ||
"name": "timekeeper", | ||
"description": "Easy testing of time-dependent code.", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "fake date", |
@@ -110,2 +110,60 @@ /** | ||
describe('withFreeze', function() { | ||
it('should freeze and reset', function() { | ||
tk.withFreeze(new Date(), function() { | ||
tk.isKeepingTime().should.be.eql(true); | ||
}); | ||
tk.isKeepingTime().should.be.eql(false); | ||
}); | ||
it('should freeze and reset when returning null', function() { | ||
tk.withFreeze(new Date(), function() { | ||
tk.isKeepingTime().should.be.eql(true); | ||
return null; | ||
}); | ||
tk.isKeepingTime().should.be.eql(false); | ||
}); | ||
it('should freeze and reset with error', function() { | ||
try { | ||
tk.withFreeze(new Date(), function () { | ||
throw new Error("error on purpose"); | ||
}); | ||
} catch (err) { | ||
// Ignore the error | ||
} finally { | ||
tk.isKeepingTime().should.be.eql(false); | ||
} | ||
}); | ||
if(typeof Promise !== 'undefined') { | ||
it('should freeze and reset with async function', function () { | ||
tk.withFreeze(new Date(), function () { | ||
return new Promise(function (resolve, reject) { | ||
tk.isKeepingTime().should.be.eql(true); | ||
resolve(12345); | ||
}); | ||
}).then(function (res) { | ||
res.should.be.eql(12345); | ||
tk.isKeepingTime().should.be.eql(false); | ||
}); | ||
}); | ||
it('should freeze and reset with async function and error', function () { | ||
tk.withFreeze(new Date(), function () { | ||
return new Promise(function (resolve, reject) { | ||
tk.isKeepingTime().should.be.eql(true); | ||
reject(new Error("error on purpose")); | ||
}); | ||
}).catch(function (err) { | ||
tk.isKeepingTime().should.be.eql(false); | ||
}); | ||
}); | ||
} | ||
afterEach(function() { | ||
tk.reset(); | ||
}); | ||
}); | ||
describe('inheritance', function() { | ||
@@ -112,0 +170,0 @@ describe('should create an instance of Date', function() { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16767
420