Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/driver/cypress/integration/commands/net_stubbing_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
})
27 changes: 22 additions & 5 deletions packages/driver/src/cy/commands/waiting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down