From 775312f1c5d657f45f3414e24db81be0b30c5bcd Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Thu, 2 May 2019 16:52:29 -0400 Subject: [PATCH 1/3] Return empty arrays for performance.getEntries, other relevant methods --- src/lolex-src.js | 8 +++++++- test/lolex-test.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/lolex-src.js b/src/lolex-src.js index 27d0b6ec..658a6485 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -27,6 +27,7 @@ function withGlobal(_global) { // see https://github.com/cjohansen/Sinon.JS/pull/436 var NOOP = function () { return undefined; }; + var NOOP_ARRAY = function () { return []; }; var timeoutResult = _global.setTimeout(NOOP, 0); var addTimerReturnsObject = typeof timeoutResult === "object"; var hrtimePresent = (_global.process && typeof _global.process.hrtime === "function"); @@ -849,7 +850,12 @@ function withGlobal(_global) { Object .getOwnPropertyNames(proto) .forEach(function (name) { - clock.performance[name] = NOOP; + if (name.slice(0, 10) === "getEntries") { + // match expected return type for getEntries functions + clock.performance[name] = NOOP_ARRAY; + } else { + clock.performance[name] = NOOP; + } }); } diff --git a/test/lolex-test.js b/test/lolex-test.js index 15dc89ef..0a773a77 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -2084,6 +2084,24 @@ describe("lolex", function () { delete Performance.prototype.someFunc2; delete Performance.prototype.someFunc3; }); + + it("should replace the getEntries, getEntriesByX methods with noops that return []", function () { + Performance.prototype.getEntries = + Performance.prototype.getEntriesByName = + Performance.prototype.getEntriesByType = function () { return ["foo"]; }; + + this.clock = lolex.install(); + + assert.equals(performance.getEntries(), []); + assert.equals(performance.getEntriesByName(), []); + assert.equals(performance.getEntriesByType(), []); + + this.clock.uninstall(); + + delete Performance.prototype.getEntries; + delete Performance.prototype.getEntriesByName; + delete Performance.prototype.getEntriesByTime; + }); } if (Object.getPrototypeOf(global)) { From 42c86e175be20d651229a43d1638ab932869fea6 Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Fri, 3 May 2019 12:50:51 -0400 Subject: [PATCH 2/3] add restore test --- test/lolex-test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/lolex-test.js b/test/lolex-test.js index 0a773a77..21a37599 100644 --- a/test/lolex-test.js +++ b/test/lolex-test.js @@ -2098,6 +2098,10 @@ describe("lolex", function () { this.clock.uninstall(); + assert.equals(performance.getEntries(), ["foo"]); + assert.equals(performance.getEntriesByName(), ["foo"]); + assert.equals(performance.getEntriesByType(), ["foo"]); + delete Performance.prototype.getEntries; delete Performance.prototype.getEntriesByName; delete Performance.prototype.getEntriesByTime; From 61cd69005fe770805eb8b8ce615d38b54f8c4f96 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 5 May 2019 10:48:51 -0400 Subject: [PATCH 3/3] Update src/lolex-src.js Co-Authored-By: flotwig --- 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 658a6485..a29d0db0 100644 --- a/src/lolex-src.js +++ b/src/lolex-src.js @@ -850,7 +850,7 @@ function withGlobal(_global) { Object .getOwnPropertyNames(proto) .forEach(function (name) { - if (name.slice(0, 10) === "getEntries") { + if (name.indexOf("getEntries") === 0) { // match expected return type for getEntries functions clock.performance[name] = NOOP_ARRAY; } else {