fix: find waiting for cy.intercept when it has been overwritten by the user (#9596)

This commit is contained in:
Gleb Bahmutov
2020-12-11 11:26:25 -05:00
committed by GitHub
parent 6c3c0eea09
commit 49db3a685d
2 changed files with 113 additions and 5 deletions

View File

@@ -2277,5 +2277,96 @@ describe('network stubbing', { retries: 2 }, function () {
.then(testResponse('something different', done))
})
})
// @see https://github.com/cypress-io/cypress/issues/9580
context('overwrite cy.intercept', () => {
it('works with an alias by default', () => {
// sanity test before testing it with the cy.intercept overwrite
cy.intercept('/foo', 'my value').as('netAlias')
.then(() => {
return $.get('/foo')
})
cy.wait('@netAlias').its('response.body').should('equal', 'my value')
})
it('works with an alias and function', () => {
let myInterceptCalled
cy.spy(cy, 'log')
Cypress.Commands.overwrite('intercept', function (originalIntercept, ...args) {
return cy.log('intercept!').then(() => {
myInterceptCalled = true
return originalIntercept(...args)
})
})
cy.intercept('/foo', 'my value').as('netAlias')
.then(() => {
return $.get('/foo')
})
.then(() => {
expect(myInterceptCalled, 'my intercept was called').to.be.true
expect(cy.log).to.have.been.calledWith('intercept!')
})
cy.wait('@netAlias').its('response.body').should('equal', 'my value')
})
it('works with an alias and arrow function', () => {
let myInterceptCalled
cy.spy(cy, 'log')
Cypress.Commands.overwrite('intercept', (originalIntercept, ...args) => {
return cy.log('intercept!').then(() => {
myInterceptCalled = true
return originalIntercept(...args)
})
})
cy.intercept('/foo', 'my value').as('netAlias')
.then(() => {
return $.get('/foo')
})
.then(() => {
expect(myInterceptCalled, 'my intercept was called').to.be.true
expect(cy.log).to.have.been.calledWith('intercept!')
})
cy.wait('@netAlias').its('response.body').should('equal', 'my value')
})
it('works with dynamic alias', () => {
let myInterceptCalled
cy.spy(cy, 'log')
Cypress.Commands.overwrite('intercept', (originalIntercept, ...args) => {
return cy.log('intercept!').then(() => {
myInterceptCalled = true
return originalIntercept(...args)
})
})
cy.intercept('/foo', (req) => {
req.alias = 'netAlias'
req.reply('my value')
})
.then(() => {
return $.get('/foo')
})
.then(() => {
expect(myInterceptCalled, 'my intercept was called').to.be.true
expect(cy.log).to.have.been.calledWith('intercept!')
})
cy.wait('@netAlias').its('response.body').should('equal', 'my value')
})
})
})
})

View File

@@ -156,11 +156,28 @@ module.exports = (Commands, Cypress, cy, state) => {
log.set('referencesAlias', aliases)
}
if (command && !['route', 'route2', 'intercept'].includes(command.get('name'))) {
$errUtils.throwErrByPath('wait.invalid_alias', {
onFail: options._log,
args: { alias },
})
const isNetworkInterceptCommand = (command) => {
const commandsThatCreateNetworkIntercepts = ['route', 'route2', 'intercept']
const commandName = command.get('name')
return commandsThatCreateNetworkIntercepts.includes(commandName)
}
const findInterceptAlias = (alias) => {
const routes = cy.state('routes') || {}
return _.find(_.values(routes), { alias })
}
const isInterceptAlias = (alias) => Boolean(findInterceptAlias(alias))
if (command && !isNetworkInterceptCommand(command)) {
if (!isInterceptAlias(alias)) {
$errUtils.throwErrByPath('wait.invalid_alias', {
onFail: options._log,
args: { alias },
})
}
}
// create shallow copy of each options object