fix: wait on alias from custom cy.route command overwrite (#14202)

This commit is contained in:
Gleb Bahmutov
2020-12-16 12:37:16 -05:00
committed by GitHub
parent 2c4349ea71
commit 76a33bb1a1
3 changed files with 91 additions and 2 deletions
@@ -237,7 +237,7 @@ describe('src/cy/commands/waiting', () => {
describe('errors', {
defaultCommandTimeout: 100,
}, () => {
it('throws when alias doesnt match a route', (done) => {
it('throws when alias does not match a route (DOM element)', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.wait()` only accepts aliases for routes.\nThe alias: `b` did not match a route.')
expect(err.docsUrl).to.eq('https://on.cypress.io/wait')
@@ -248,6 +248,17 @@ describe('src/cy/commands/waiting', () => {
cy.get('body').as('b').wait('@b')
})
it('throws when alias does not match a route (wrapped value)', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.wait()` only accepts aliases for routes.\nThe alias: `b` did not match a route.')
expect(err.docsUrl).to.eq('https://on.cypress.io/wait')
done()
})
cy.wrap('my value').as('b').wait('@b')
})
it('throws when route is never resolved', {
requestTimeout: 100,
}, (done) => {
@@ -0,0 +1,63 @@
// @see https://github.com/cypress-io/cypress/issues/3890
const { $ } = Cypress
describe('issue 3890 overwriting cy.route command', () => {
before(() => {
cy
.visit('/fixtures/jquery.html')
.then(function (win) {
const h = $(win.document.head)
h.find('script').remove()
this.head = h.prop('outerHTML')
this.body = win.document.body.outerHTML
})
})
beforeEach(function () {
const doc = cy.state('document')
$(doc.head).empty().html(this.head)
$(doc.body).empty().html(this.body)
})
it('stores route as an alias', () => {
// sanity test before overwriting cy.route in the next test
cy
.server()
.route(/foo/, 'my value').as('getFoo')
.window().then((win) => {
win.$.get('foo')
return null
})
.wait('@getFoo').its('responseBody').should('equal', 'my value')
})
it('stores route as an alias after overwrite', () => {
let routeCalled
Cypress.Commands.overwrite('route', (route, ...args) => {
routeCalled = true
return cy.log(`cy.route ${args.join(' ')}`)
.then(() => {
return route(...args)
})
})
cy
.server()
.route(/foo/, 'my value').as('getFoo')
.window().then((win) => {
win.$.get('foo')
return null
})
.wait('@getFoo').its('responseBody').should('equal', 'my value')
.then(() => {
expect(routeCalled, 'route overwrite was called').to.be.true
})
})
})
+16 -1
View File
@@ -171,8 +171,23 @@ module.exports = (Commands, Cypress, cy, state) => {
const isInterceptAlias = (alias) => Boolean(findInterceptAlias(alias))
const isRouteAlias = (alias) => {
// has all aliases saved using cy.as() command
const aliases = cy.state('aliases') || {}
const aliasObject = aliases[alias]
if (!aliasObject) {
return false
}
// cy.route aliases have subject that has all XHR properties
// let's check one of them
return aliasObj.subject && Boolean(aliasObject.subject.xhrUrl)
}
if (command && !isNetworkInterceptCommand(command)) {
if (!isInterceptAlias(alias)) {
if (!isInterceptAlias(alias) && !isRouteAlias(alias)) {
$errUtils.throwErrByPath('wait.invalid_alias', {
onFail: options._log,
args: { alias },