From 9381d2c815d573e57c0740853d0adedd9675d1ec Mon Sep 17 00:00:00 2001 From: Andy Edwards Date: Thu, 17 Dec 2020 23:34:42 -0600 Subject: [PATCH] fix: use better-performing means of setting up chainable methods --- src/index.js | 24 ++++++++++-------------- test/index.js | 2 +- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index 31f63ed..fcae79b 100644 --- a/src/index.js +++ b/src/index.js @@ -92,28 +92,24 @@ module.exports = (chai, utils) => { if (isChainableMethod(getterName)) { Object.defineProperty(WaitFor.prototype, getterName, { get() { - const obj = new WaitFor(this.options, () => { + const gotten = new WaitFor(this.options, () => { const assertion = this.buildAssertion() return assertion[getterName] }) - function chainable() { + function chainableMethodWrapper() { return new WaitFor(this.options, () => { const assertion = this.buildAssertion() return assertion[getterName].apply(assertion, arguments) }) } - for (const methodName of methodNames) { - chainable[methodName] = obj[methodName].bind(obj) - } - for (const getterName of getterNames) { - Object.defineProperty(chainable, getterName, { - get() { - return obj[getterName] - }, - configurable: true, - }) - } - return chainable + // Inherit all properties from the object by replacing the `Function` prototype + var prototype = Object.create(gotten) + // Restore the `call` and `apply` methods from `Function` + prototype.call = Function.prototype.call + prototype.apply = Function.prototype.apply + Object.setPrototypeOf(chainableMethodWrapper, prototype) + + return chainableMethodWrapper }, configurable: true, }) diff --git a/test/index.js b/test/index.js index 6032968..54a912c 100644 --- a/test/index.js +++ b/test/index.js @@ -68,7 +68,7 @@ describe('waitFor', function () { '2', '3' ), - clock.tickAsync(501), + clock.tickAsync(1001), ]) expect(i).to.equal(4) })