mirror of
https://github.com/cypress-io/cypress.git
synced 2026-02-23 07:39:52 -06:00
fix: find waiting for cy.intercept when it has been overwritten by the user (#9596)
This commit is contained in:
@@ -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')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user