Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

timekeeper

Package Overview
Dependencies
Maintainers
5
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

timekeeper - npm Package Compare versions

Comparing version 2.1.2 to 2.2.0

1

index.d.ts

@@ -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?

2

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc