fix: set the test context when overwriting .then command (#9589)

This commit is contained in:
Gleb Bahmutov
2020-12-11 10:43:39 -05:00
committed by GitHub
parent c1daab066d
commit 6c3c0eea09
2 changed files with 114 additions and 1 deletions

View File

@@ -316,6 +316,119 @@ describe('src/cy/commands/aliasing', () => {
})
})
})
// @see https://github.com/cypress-io/cypress/issues/5101
context('works with command overwrite', () => {
it('works without overwrite', () => {
// sanity check without command overwrite
cy.wrap('alias value').as('myAlias')
.then(() => {
expect(cy.getAlias('@myAlias'), 'alias exists').to.exist
expect(cy.getAlias('@myAlias'), 'alias value')
.to.have.property('subject', 'alias value')
})
.then(() => {
// cy.get returns the alias
cy.get('@myAlias').should('be.equal', 'alias value')
})
})
it('works with wrap', () => {
let wrapCalled
Cypress.Commands.overwrite('wrap', (wrapFn, value) => {
wrapCalled = true
return wrapFn(value)
})
cy.wrap('alias value').as('myAlias')
.then(() => {
expect(wrapCalled, 'overwrite was called').to.be.true
expect(cy.getAlias('@myAlias'), 'alias exists').to.exist
expect(cy.getAlias('@myAlias'), 'alias value')
.to.have.property('subject', 'alias value')
})
.then(() => {
// verify cy.get works in arrow function
cy.get('@myAlias').should('be.equal', 'alias value')
})
.then(function () {
// verify cy.get works in regular function
cy.get('@myAlias').should('be.equal', 'alias value')
})
})
it('works with wrap.then', () => {
let wrapCalled
let thenCalled
Cypress.Commands.overwrite('wrap', (wrapFn, value) => {
wrapCalled = true
return cy.log(`wrapped "${value}"`).then(() => {
thenCalled = true
return wrapFn(value)
})
})
cy.wrap('alias value').as('myAlias')
.then(() => {
expect(wrapCalled, 'overwrite was called').to.be.true
expect(thenCalled, 'then was called').to.be.true
expect(cy.getAlias('@myAlias'), 'alias exists').to.exist
expect(cy.getAlias('@myAlias'), 'alias value')
.to.have.property('subject', 'alias value')
})
.then(() => {
// verify cy.get works in arrow function
cy.get('@myAlias').should('be.equal', 'alias value')
})
.then(function () {
// verify cy.get works in regular function
cy.get('@myAlias').should('be.equal', 'alias value')
})
})
it('passes this to then callback function', () => {
// sanity test before the next one
cy.wrap(1).as('myAlias')
cy.wrap(2).then(function (subj) {
expect(subj, 'subject').to.equal(2)
expect(this, 'this is defined').to.not.be.undefined
expect(this.myAlias, 'this has the alias as a property').to.eq(1)
})
})
it('works with .then function overwrite', () => {
// use explicit arguments and Function.prototype.call
Cypress.Commands.overwrite('then', function (originalCommand, subject, fn, options = {}) {
return originalCommand.call(null, subject, options, fn)
})
cy.wrap(1).as('myAlias')
cy.wrap(2).then(function (subj) {
expect(subj, 'subject').to.equal(2)
expect(this, 'this is defined').to.not.be.undefined
expect(this.myAlias).to.eq(1)
})
})
it('works with .then arrow overwrite', () => {
// make sure we can pass arrow functions
Cypress.Commands.overwrite('then', (originalCommand, ...args) => {
return originalCommand(...args)
})
cy.wrap(1).as('myAlias')
cy.wrap(2).then(function (subj) {
expect(subj, 'subject').to.equal(2)
expect(this, 'this is defined').to.not.be.undefined
expect(this.myAlias).to.eq(1)
})
})
})
})
context('#replayCommandsFrom', () => {

View File

@@ -47,7 +47,7 @@ module.exports = function (Commands, Cypress, cy, state) {
// until they're 'really' resolved, so naturally this API
// supports nesting promises
const thenFn = function (subject, userOptions, fn) {
const ctx = this
const ctx = state('ctx')
if (_.isFunction(userOptions)) {
fn = userOptions