From 2a06bda84b765a97cc478897655ec187e8c98d37 Mon Sep 17 00:00:00 2001 From: BlueWinds Date: Fri, 12 Nov 2021 12:37:15 -0800 Subject: [PATCH] Allow 'this' to be used in overridden commands --- .../driver/cypress/integration/cypress/cy_spec.js | 15 +++++++++++++++ packages/driver/src/cypress/commands.ts | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/driver/cypress/integration/cypress/cy_spec.js b/packages/driver/cypress/integration/cypress/cy_spec.js index 2d9460d41a86..6112ef7cf3d0 100644 --- a/packages/driver/cypress/integration/cypress/cy_spec.js +++ b/packages/driver/cypress/integration/cypress/cy_spec.js @@ -476,5 +476,20 @@ describe('driver/src/cypress/cy', () => { expect(cy.state('current').get('prev').get('args')[0].foo).to.equal('foo') }) }) + + // https://github.com/cypress-io/cypress/issues/18892 + it('passes this through to overwritten command', () => { + Cypress.Commands.add('bar', function () { + expect(this.test.title).to.exist + }) + + cy.bar() + + Cypress.Commands.overwrite('bar', function (originalFn) { + return originalFn.call(this) + }) + + cy.bar() + }) }) }) diff --git a/packages/driver/src/cypress/commands.ts b/packages/driver/src/cypress/commands.ts index 9a1c7a8240e4..574b62a6ad78 100644 --- a/packages/driver/src/cypress/commands.ts +++ b/packages/driver/src/cypress/commands.ts @@ -58,7 +58,7 @@ export default { // store the backup again now commandBackups[name] = original - const originalFn = (...args) => { + function originalFn (...args) { const current = state('current') let storedArgs = args @@ -68,7 +68,7 @@ export default { current.set('args', storedArgs) - return original.fn(...args) + return original.fn.apply(this, args) } const overridden = _.clone(original)