From 49db3a685d77655dde17fa39e2211dcacd7d0323 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Fri, 11 Dec 2020 11:26:25 -0500 Subject: [PATCH] fix: find waiting for cy.intercept when it has been overwritten by the user (#9596) --- .../integration/commands/net_stubbing_spec.ts | 91 +++++++++++++++++++ packages/driver/src/cy/commands/waiting.js | 27 +++++- 2 files changed, 113 insertions(+), 5 deletions(-) diff --git a/packages/driver/cypress/integration/commands/net_stubbing_spec.ts b/packages/driver/cypress/integration/commands/net_stubbing_spec.ts index 7f187d1b80..b098efd8d0 100644 --- a/packages/driver/cypress/integration/commands/net_stubbing_spec.ts +++ b/packages/driver/cypress/integration/commands/net_stubbing_spec.ts @@ -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') + }) + }) }) }) diff --git a/packages/driver/src/cy/commands/waiting.js b/packages/driver/src/cy/commands/waiting.js index cf0a16ec53..d05017928b 100644 --- a/packages/driver/src/cy/commands/waiting.js +++ b/packages/driver/src/cy/commands/waiting.js @@ -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