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

Response not coming through for every request with intercept #14522

Closed
Tom5om opened this issue Jan 13, 2021 · 23 comments
Closed

Response not coming through for every request with intercept #14522

Tom5om opened this issue Jan 13, 2021 · 23 comments

Comments

@Tom5om
Copy link

Tom5om commented Jan 13, 2021

Hi There,

I was upgrading cypress in our application and migrating to intercept.
It seems to work intermittently.

OLD

cy.server()
        .route('post', '**/landlord')
        .as('saveLandlord');

cy.contains('button', 'Next').click();

cy.wait('@saveLandlord').should('have.property', 'status', 200);

New

cy.intercept('POST', '**/landlord**').as('saveLandlord');

cy.contains('button', 'Next').click();

cy.wait('@saveLandlord')
        .its('response.statusCode')
        .should('eq', 200);

I have tried to log the response in between and the response is just not passed in.

This is the error I get, and you can see that it did not yield a response on the right:
image

Here is the response from the console:
image

The same code works for some other requests, I can't really see why this doesn't work for this instance.

@jennifer-shehane
Copy link
Member

Are you using the latest version of Cypress? Please update to the current version of Cypress if not - as we've fixed issues that look similar to this in more recent versions.

@cypress-bot cypress-bot bot added the stage: awaiting response Potential fix was proposed; awaiting response label Jan 13, 2021
@LoganFievez
Copy link

LoganFievez commented Jan 13, 2021

I downloaded the last version of cypress today (6.2.1) and I encountered the same issue

@MistertempDev
Copy link

We're having the exact same problem also! We're using Cypress 6.2.1 on Mac (and Windows).

@bahmutov
Copy link
Contributor

Can someone provide a reproducible example for us to see this behavior? We have cy.intercept recipe in https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/stubbing-spying__intercept that you can use as the basis for the reproduction.

@jaiswalkpraveen
Copy link

jaiswalkpraveen commented Jan 13, 2021

@bahmutov I'm intercepting graphQL request and verifying it's response. Let me know if we can connect right away
.
image

@bahmutov
Copy link
Contributor

Hi @jaiswalkpraveen can you not create a reproducible example we can run?

@bahmutov
Copy link
Contributor

bahmutov commented Jan 13, 2021 via email

@jaiswalkpraveen
Copy link

@bahmutov Got it. Unfortunately, my apps needs credentials and I can't share it.

@jaiswalkpraveen
Copy link

jaiswalkpraveen commented Jan 13, 2021

@bahmutov Just tried with external website, it's working intermittently. Please find script below.
Let me try to brief it.

  1. Launch buildkite website
  2. Go to pipelines page by clicking pipeline button(after filling start trial creds) which is like 'waiting for page to load' for more than 5-10 sec
  3. Then I'm getting error
it("validate same gql response ", () => {
    cy.intercept('POST', '_graphql/v1', (req) => {
        if (req.body.query.includes('Index_ViewerRelayQL')) {
            req.alias = 'IndexQuery'
        }

    })
    cy.visit('https://buildkite.com/')
    cy.contains('Start Trial').click()
    cy.get('.input').eq(0).type('Kumar')
    cy.get('.input').eq(1).type('Lavasa')
    cy.get('.input').eq(2).type('Praveen@mira.io')
    cy.get('.input').eq(3).type('Cypress*&^%')
    cy.contains('Get Started').click()
    cy.wait('@IndexQuery').then((APIResponse1) => {
        console.log(APIResponse1)
    })
    cy.contains('Pipelines').click()
    cy.wait('@IndexQuery').then((APIResponse2) => {
        console.log(APIResponse2)
    })
})

@Tom5om
Copy link
Author

Tom5om commented Jan 13, 2021

Hi There, yep I use version 6.2.1, apologies for not mentioning that.

does the example above suffice? I can't use our own application as an example, and like I said this doesn't happen for all requests in my application, but it always happen for a whole bunch of them. I can't see what the difference is.

I have tried providing an example with external facing websites, but was not able to find one.
Happy to jump on a call with someone or something to show the issues

@bahmutov bahmutov self-assigned this Jan 14, 2021
@Tom5om
Copy link
Author

Tom5om commented Jan 14, 2021

Hi @bahmutov I have been able to create an example where this happens:

it('Login page shows', () => {
            cy.visit('app.staging.flkitover.com');
            cy.clearCookies();
            cy.location('pathname').should('equal', '/');
            cy.get('h1').contains('Log In');

            cy.intercept('POST', '/login').as('login');
            cy.get('[name=email]').type('something@something.com');
            cy.get('[name=password]').type('Test1234');
            cy.contains('button', 'Log in').click();

            cy.wait('@login')
                .its('response.statusCode')
                .should('eq', 401);

            cy.intercept('POST', '/login').as('login2');
            cy.get('[name=password]').type('Test1234');
            cy.contains('button', 'Log in').click();

            cy.wait('@login2')
                .its('response.statusCode')
                .should('eq', 401);
        });

@bahmutov
Copy link
Contributor

@Tom5om great reproduction, thank you so much. I can see it in reproduction https://github.com/cypress-io/cypress-test-tiny/tree/response

There are two spy intercepts.

First we define login spy intercept and test it

So the Ajax call fails

Screen Shot 2021-01-14 at 12 25 38 PM

If we ask the first spy, it has the right status code and response

Screen Shot 2021-01-14 at 12 28 58 PM

Then we define the second spy intercept for the same route

and make the Ajax call again

Since these are spies, the first spy intercept is called again (so it has count 2), and the second spy intercept is only called once

Screen Shot 2021-01-14 at 12 29 33 PM

The error response from the server goes through the first intercept, and when we wait for second intercept it gets no response object

Screen Shot 2021-01-14 at 12 25 52 PM

Seems we need to make sure spy intercepts pass everything correctly down the line if the network call had an error

@bahmutov bahmutov added type: bug stage: ready for work The issue is reproducible and in scope topic: cy.intercept() and removed stage: awaiting response Potential fix was proposed; awaiting response labels Jan 14, 2021
@Tom5om
Copy link
Author

Tom5om commented Jan 14, 2021

@bahmutov @flotwig just want to clarify, that this is how I was able to reproduce it.

I couldn't give you examples, but it happens as well when I intercept different routes (but multiple) and also when there are 200 statuscode responses

@DominikPalo
Copy link

DominikPalo commented Jan 19, 2021

Same issue

I replaced the old

    cy
      .route({
        method: 'POST',
        url: '**/oauth/token?grant_type=password&**'
      }).as('authentication');

with

    cy
       .intercept('POST', '**/oauth/token?grant_type=password&**')
       .as('authentication');

and now the yielded intercept object does not contain the response.

    cy
      .log('authentication request successful')
      .wait('@authentication')
      .then((auth) => {
        const accessToken = auth.response.body['access_token'];
     });

fails with
Cannot read property 'body' of undefined

because the actual intercept object contains only the request object:

{
  id: "interceptedRequest82"
  log: {get: ƒ, unset: ƒ, invoke: ƒ, toJSON: ƒ, set: ƒ,}
  request: {headers: {}, body: {}, url: "...", method: "POST", httpVersion: "1.1"}
  requestWaited: true
  responseWaited: true
  routeHandlerId: "1611048999315-1637"
  state: "Complete"
  __proto__: Object
}

I'm on the latest Cypress version 6.2.1

@kpurdon
Copy link

kpurdon commented Jan 21, 2021

Does anybody have a workaround on 6+ for this issue?

@Tom5om
Copy link
Author

Tom5om commented Jan 27, 2021

@kpurdon the workaround is to use the old way:

cy.server()
        .route('post', '**/landlord')
        .as('saveLandlord');

cy.contains('button', 'Next').click();

cy.wait('@saveLandlord').should('have.property', 'status', 200);

@danisal
Copy link

danisal commented Feb 10, 2021

Facing the same issue in version 6.4.0

@CypressHarry
Copy link

I believe this is a duplicate of #14205

@sainthkh sainthkh self-assigned this Mar 2, 2021
@sainthkh
Copy link
Contributor

sainthkh commented Mar 2, 2021

I partially solved the bug. Current problem is that it breaks other features. I'm trying to find the solution.

@jprust
Copy link

jprust commented Mar 15, 2021

@flotwig would it be possible to review Sainthkh's PR and include this fix in the next release?

@flotwig
Copy link
Contributor

flotwig commented Mar 16, 2021

@jprust the fix will be included with 7.0.0.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Mar 16, 2021

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

@flotwig flotwig closed this as completed Mar 16, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 5, 2021

Released in 7.0.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Apr 5, 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.