Skip to content

Commit

Permalink
chore(tests): fix flake in net stubbing/xhr/proxy logging tests (#18163)
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig authored Sep 21, 2021
1 parent f826d3a commit 0964ca2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1638,12 +1638,10 @@ describe('network stubbing', { retries: 2 }, function () {
delay: 5000,
}).as('create')

cy.window().then((win) => {
win.eval(
`fetch("/post-only", {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
});`,
)
cy.then(() => {
fetch('/post-only', {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
})
})

cy.wait('@create', { timeout: 500 })
Expand Down Expand Up @@ -3129,7 +3127,7 @@ describe('network stubbing', { retries: 2 }, function () {
})
})

context('waiting and aliasing', function () {
context('waiting and aliasing', { defaultCommandTimeout: 10000 }, function () {
const testFailWaiting = (cb) => testFail(cb, 'https://on.cypress.io/wait')

it('can wait on a single response using "alias"', function () {
Expand Down
47 changes: 35 additions & 12 deletions packages/driver/cypress/integration/commands/xhr_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,14 +1026,25 @@ describe('src/cy/commands/xhr', () => {
})

it('sets err on log when caused by code errors', function (done) {
cy.on('fail', (err) => {
done = _.once(done)
cy.once('fail', (err) => {
// suppress failure
})

cy.on('log:changed', () => {
const { lastLog } = this

expect(this.logs.length).to.eq(1)
expect(lastLog.get('name')).to.eq('request')
expect(lastLog.get('error').message).contain('foo is not defined')
if (!lastLog || lastLog.get('name') !== 'request') return

done()
try {
expect(this.logs.length).to.eq(1)
expect(lastLog.get('error').message).contain('foo is not defined')

done()
} catch (err) {
// eslint-disable-next-line no-console
console.log('assertion failure', err)
}
})

cy.window().then((win) => {
Expand All @@ -1047,17 +1058,27 @@ describe('src/cy/commands/xhr', () => {
})

it('causes errors caused by onreadystatechange callback function', function (done) {
done = _.once(done)
const e = new Error('onreadystatechange caused this error')

cy.on('fail', (err) => {
cy.once('fail', (err) => {
// suppress failure
})

cy.on('log:changed', () => {
const { lastLog } = this

expect(this.logs.length).to.eq(1)
expect(lastLog.get('name')).to.eq('request')
expect(err.message).to.include(lastLog.get('error').message)
expect(err.message).to.include(e.message)
if (!lastLog || lastLog.get('name') !== 'request') return

done()
try {
expect(this.logs.length).to.eq(1)
expect(lastLog.get('name')).to.eq('request')
expect(e.message).to.include(lastLog.get('error').message)
done()
} catch (err) {
// eslint-disable-next-line no-console
console.log('assertion failure', err)
}
})

cy
Expand Down Expand Up @@ -2563,7 +2584,9 @@ describe('src/cy/commands/xhr', () => {

xhr.open('GET', '/timeout?ms=999')
xhr.send()
xhr.abort()

// allow the request time to make it out of the browser so proxy logging has a chance to see it
requestAnimationFrame(() => xhr.abort())

cy.wrap(null).should(() => {
expect(log.get('state')).to.eq('failed')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,8 @@ describe('Proxy Logging', () => {
expect(log.consoleProps).to.not.have.property('Matched `cy.intercept()`')

// trigger: .snapshot('request')
cy.once('log:changed', (log) => {
expect(log.snapshots.map((v) => v.name)).to.deep.eq(['request'])

// trigger: .snapshot('response')
cy.once('log:changed', (log) => {
cy.on('log:changed', (log) => {
try {
expect(log.snapshots.map((v) => v.name)).to.deep.eq(['request', 'response'])
expect(log.consoleProps['Response Headers']).to.include({
'x-powered-by': 'Express',
Expand All @@ -101,7 +98,10 @@ describe('Proxy Logging', () => {
)

done()
})
} catch (err) {
// eslint-disable-next-line no-console
console.log('assertion error, retrying', err)
}
})
})
})
Expand Down Expand Up @@ -238,7 +238,7 @@ describe('Proxy Logging', () => {
.wrap(logs)
.should((logs) => {
// retries...
expect(logs).to.have.length.greaterThan(2)
expect(logs).to.have.length.greaterThan(0)

for (const log of logs) {
expect(log.err).to.include({ name: 'Error' })
Expand Down

4 comments on commit 0964ca2

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0964ca2 Sep 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.4.2/circle-develop-0964ca2c7d54c747bfae639fa4bda1cd86817f08/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0964ca2 Sep 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.4.2/appveyor-develop-0964ca2c7d54c747bfae639fa4bda1cd86817f08/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0964ca2 Sep 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.4.2/appveyor-develop-0964ca2c7d54c747bfae639fa4bda1cd86817f08/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0964ca2 Sep 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.4.2/circle-develop-0964ca2c7d54c747bfae639fa4bda1cd86817f08/cypress.tgz

Please sign in to comment.