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

Delay for intercepted HTTP routes only works for the first call on that route #14446

Closed
djungowski opened this issue Jan 7, 2021 · 3 comments · Fixed by #14708
Closed

Delay for intercepted HTTP routes only works for the first call on that route #14446

djungowski opened this issue Jan 7, 2021 · 3 comments · Fixed by #14708

Comments

@djungowski
Copy link

djungowski commented Jan 7, 2021

Current behavior

When using .intercept() for a HTTP route and stubbing it with a static response with a defined delay, that delay is only used on the first ever occurrence of this call.

Desired behavior

Every call of this route is delayed

Test code to reproduce

Given the following HTML:

<!DOCTYPE html>
<head>
    <title>msDelay Test</title>
</head>
<body>
<button id="click-me">Click me</button>
<p id="difference">0</p>
<script type="text/javascript">
    const difference = document.getElementById('difference');
    document.getElementById('click-me').addEventListener('click', () => {
        const before = new Date().getTime();
        fetch('/api/something').then(() => {
            const after = new Date().getTime();
            const diff = after - before;
            difference.innerHTML = `${diff / 1000}`;
        });
    });
</script>
</body>

the following Cypress test should work:

describe('Delaying Responses', () => {
  it('should delay every response', () => {
    cy.intercept('GET', '/api/something', { body: {}, delayMs: 5000 }).as('apiRoute');
    cy.visit('msDelay.html');

    cy.get('#click-me').click();
    cy.wait('@apiRoute');
    cy.get('#difference').invoke('text').then(text => expect(parseFloat(text)).to.be.closeTo(5, 0.5));

    cy.get('#click-me').click();
    cy.wait('@apiRoute');
    cy.get('#difference').invoke('text').then(text => expect(parseFloat(text)).to.be.closeTo(5, 0.5));
  });
});

However the first assertion goes through just fine and the second one fails with expected 0.009 to be close to 5 +/- 0.5

Versions

6.2.1

Additional info

From what I can see, this is because in https://github.com/cypress-io/cypress/blob/f43f1c1f26290fc8387697c8122f0f50b6c7cb55/packages/driver/src/cy/net-stubbing/events/response-received.ts the delay is defined as follows and probably is only called on the first occurrence:

continueFrame.continueResponseAt = Date.now() + delayMs
@jennifer-shehane
Copy link
Member

@djungowski I can recreate this issue as described. This is likely an oversight and not the intended behavior.

@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.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Feb 1, 2021

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-bot cypress-bot bot locked as resolved and limited conversation to collaborators Feb 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants