From 966e206c88a72c36de9561262c2f64053a2c2300 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 13 Jul 2017 18:31:47 +0300 Subject: [PATCH 1/2] error on incorrect install use --- src/lolex-src.js | 4 ++++ test/lolex-test.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/lolex-src.js b/src/lolex-src.js index 5cfb465c..89b1e9e6 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -637,6 +637,10 @@ exports.createClock = createClock; * @param config.loopLimit {number} the maximum number of timers that will be run when calling runAll() */ exports.install = function install(config) { + if (typeof config !== "object" && typeof config !== "undefined") { + throw new TypeError("lolex.install called with " + String(config) + + " lolex 2.0+ requires an object parameter - see https://github.com/sinonjs/lolex"); + } config = typeof config !== "undefined" ? config : {}; var i, l; diff --git a/test/lolex-test.js b/test/lolex-test.js index ff2d85df..a7573c4e 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -1560,6 +1560,20 @@ describe("lolex", function () { assert.same(Date.clock, this.clock); }); + it("takes an object parameter", function () { + this.clock = lolex.install({}); + }); + + it("throws a TypeError on a number parameter", function () { + try { + this.clock = lolex.install(0); + } catch (e) { + assert(e instanceof TypeError); + return; + } + throw new Error("Installing with a number parameter doesn't throw"); + }); + it("sets initial timestamp", function () { this.clock = lolex.install({ now: 1400 }); @@ -1835,6 +1849,7 @@ describe("lolex", function () { }, 2500); clock.tick(5000); }); + }); } if (hrtimePresent) { @@ -1876,6 +1891,7 @@ describe("lolex", function () { assert.same(result[0], 0); assert.same(result[1], 0); }); + }); } }); From 955a44693a4c4e750ea15eace054ca5822b739c5 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sun, 16 Jul 2017 10:11:37 +0300 Subject: [PATCH 2/2] Alter type check to requested format --- src/lolex-src.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lolex-src.js b/src/lolex-src.js index f0a48819..bc7395b2 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -647,7 +647,7 @@ exports.createClock = createClock; * @param config.advanceTimeDelta {Number} increment mocked time every <> ms (default: 20ms) */ exports.install = function install(config) { - if (typeof config !== "object" && typeof config !== "undefined") { + if ( arguments.length > 1 || config instanceof Date || Array.isArray(config) || typeof config === "number") { throw new TypeError("lolex.install called with " + String(config) + " lolex 2.0+ requires an object parameter - see https://github.com/sinonjs/lolex"); }