Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static interception delayMs option measures time from "cy.intercept" command, not from the request timestamp #14511

Closed
bahmutov opened this issue Jan 12, 2021 · 3 comments · Fixed by #14708
Labels
topic: cy.intercept() type: bug type: duplicate This issue or pull request already exists

Comments

@bahmutov
Copy link
Contributor

Cypress v6.2.1 using cy.intercept to send a static response. My goal is to clearly see the loading indicator in the UI, thus I want to slow down the server response.

Situation

Without intercept, the request fires 3 seconds after the page loads

it('use positive then negative assertion', () => {
  cy.visit('/?delay=3000')
  // first, make sure the loading indicator shows up (positive assertion)
  cy.get('.loading').should('be.visible')
  // then assert it goes away (negative assertion)
  cy.get('.loading').should('not.be.visible')
})

Notice how quickly the loading message appears and disappears without network control. In fact it passes on the first run, and fails to "catch" the quickly appearing loading element on the second re-run.

loading.mp4

Of course, this is a flakey test and we could use test retries to solve it. But let's make the test better using network control.

Attempt

So I want to slow down the network request to GET /todos to always have the loading indicator stay visible for a period of time.

it('slows down the network response (does not work)', () => {
  cy.intercept('/todos', {
    body: [],
    delayMs: 2000
  })
  cy.visit('/?delay=3000')
  // first, make sure the loading indicator shows up (positive assertion)
  cy.get('.loading').should('be.visible')
  // then assert it goes away (negative assertion)
  cy.get('.loading').should('not.be.visible')
})

Hmm, it looks wrong! It looks like the delayMs wasn't even applied

delay-not-applied.mp4

Code

Looking at the code in

I see that we compute the response time using the time when cy.intercept() command runs

if (staticResponse.delayMs) {
  backendStaticResponse.continueResponseAt = Date.now() + staticResponse.delayMs
}

Ok, let's see. If our application makes the request after 3 seconds, let's set the response to be sent after 5 seconds

delay-5.mp4

yeah, this works, but is so counterintuitive

Workaround

Use programmatic reply and not a static response

it('slows down the network response (programmatic)', () => {
  cy.intercept('/todos', req => {
    req.reply({
      body: [],
      delayMs: 2000
    })
  })
  cy.visit('/?delay=3000')
  // first, make sure the loading indicator shows up (positive assertion)
  cy.get('.loading').should('be.visible')
  // then assert it goes away (negative assertion)
  cy.get('.loading').should('not.be.visible')
})

BUT this solution also has a bug noted in #14446 - it uses the first time req.reply runs to "remember" the response delay :)

Desired output

when using static intercept, start counting the delay from the moment the network request starts

@flotwig
Copy link
Contributor

flotwig commented Jan 22, 2021

Duplicate of #14446

@flotwig flotwig marked this as a duplicate of #14446 Jan 22, 2021
@flotwig flotwig closed this as completed Jan 22, 2021
@flotwig flotwig added the type: duplicate This issue or pull request already exists label Jan 22, 2021
@cypress-bot cypress-bot bot added stage: pending release and removed stage: ready for work The issue is reproducible and in scope labels Jan 29, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jan 29, 2021

The code for this is done in cypress-io/cypress#14708, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@jennifer-shehane
Copy link
Member

Released in 6.4.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v6.4.0, please open a new issue.

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Feb 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
topic: cy.intercept() type: bug type: duplicate This issue or pull request already exists
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants