Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow 'this' to be used in overridden commands #18899

Merged
merged 2 commits into from Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/driver/cypress/integration/cypress/cy_spec.js
Expand Up @@ -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()
})
})
})
4 changes: 2 additions & 2 deletions packages/driver/src/cypress/commands.ts
Expand Up @@ -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

Expand All @@ -68,7 +68,7 @@ export default {

current.set('args', storedArgs)

return original.fn(...args)
return original.fn.apply(this, args)
}

const overridden = _.clone(original)
Expand Down