cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.07k stars 3.19k forks source link

Intercept doesn't work in beforeEach but it works when I add them to every it()// Some intercepts don't work at all #25718

Closed domipakmur closed 1 year ago

domipakmur commented 1 year ago

Current behavior

I defined intercept in beforeEach. Running my test the cypress can't see my intercept.

The intercepts work when I add them to every it section.

Desired behavior

Cypress should see the intercept from beforeEach. I migrated my cypress (and all tests) from the 9.7 version to 12.5.1; previously, I didn't have a problem with beforeEach.

Test code to reproduce

  beforeEach(() => {

    cy.restoreLocalStorage();
    cy.intercept("POST", "/api/utility/v2/buildings/*/referenceyears").as("referenceYears");

  });

same in it:

it("xxxxx2", function () {
    cy.intercept("POST", "/api/utility/v2/buildings/*/referenceyears").as("referenceYears");
.
.
.
.
}); 

Cypress Version

v12.5.1

Node version

v16.13.2

Operating System

Windows 10 21H2

Debug Logs

No response

Other

No response

mschile commented 1 year ago

Hi @domipakmur đź‘‹, thanks for logging this issue. Unfortunately, I wasn't able to recreate the issue with my reproduction project. The cy.intercept in the beforeEach functioned as expected. If you could please update the project to reproduce the issue that would help me investigate.

sanguinesonal commented 1 year ago

I am also facing the same issue when I upgraded the cypress from 10.11.0 to 12.5.1

mschile commented 1 year ago

@sanguinesonal, do you have or can you create a reproduction project that I can use to investigate? Feel free to use my reproduction project as a starting point.

domipakmur commented 1 year ago

I see that this is not only problem with beforeEach. I also have problem with intercept when I put them between my "IT". This is my intercept:

cy.intercept("POST", "api/calculation/*").as("changeValue");

image

I see that request went, but cypress didn't see that:

image

Some intercepts work, but some don't. I don't know what is the reason. Like I said before new version every intercept worked.

mschile commented 1 year ago

@domipakmur, right now there doesn't seem to be enough information to reproduce the problem on our end. Unless we receive a reliable reproduction, we'll eventually have to close this issue until we can reproduce it. This does not mean that your issue is not happening - it just means that we do not have a path to move forward. I provided my reproduction project above, if you could modify it to reproduce the issue I would be happy to look into it.

flotwig commented 1 year ago

Unfortunately we have to close this issue due to inactivity. Please comment if there is new information to provide concerning the original issue and we can reopen.

sanguinesonal commented 1 year ago

I have identified the problem. In the version after 10.11.0 cypress is reading the cookies differently and thus my post request were failing and get requests were passed. I change the intercepts written and it worked… Definitely not an issue with cypress @domipakmur happy to guide you as well..

alexandersdickinson commented 1 year ago

@sanguinesonal Would you be able to share what you changed to solve this problem? I am having the same issue.

sanguinesonal commented 1 year ago

@alexandersdickinson can you share your code snippet..as for me it was related to how cypress was reading the cookie which was set by my application…

alexandersdickinson commented 1 year ago

@sanguinesonal I realized that you have problems after cypress 10.11, but I was already on cypress 11, and this problem occurred after upgrading to cypress 12, so my problem may be unrelated. Then again, cypress 12 made some changes regarding test isolation defaults that might affect how cookies are handled. Either way, here is the code snippet:

    cy.intercept("POST", endpoint, (req) => {
      const queryName = gql`
        ${req.body.query}
      `<some accessors to get query name>;
      if (operationNames.includes(queryName)) {
        req.alias = queryName;
      }
    });

This is run to alias graphQL requests so cypress can wait on them. When run in the body of tests, there are no problems. In the beforeEach, the problem seems to be that req.body is an empty string, so the query name is not found, and an error is thrown.

sanguinesonal commented 1 year ago

@alexandersdickinson yes you are right this is a different problem..not related to mine..

lalicia commented 1 year ago

I'm having this error trying to work with the cypress-realworld-testing-blog from learn.cypress.io; in the network-requests.cy.js there's an intercept in the beforeEach and this error is coming up.

repo link

screenshot from test runner

nagash77 commented 1 year ago

Hi @lalicia can you please open an issue in the cypress-realworld-app repo detailing your problem?

lalicia commented 1 year ago

Hi @lalicia can you please open an issue in the cypress-realworld-app repo detailing your problem?

Hi @nagash77 - I'm unable to do this as the repo is archived and it won't allow me to open a new issue.

For further clarity I got to the repo by doing the courses on learn.cypress.io - it was linked in the final course I believe.

nagash77 commented 1 year ago

@lalicia This repository should not be archived (https://github.com/cypress-io/cypress-realworld-app).

fenech commented 1 year ago
    cy.intercept("POST", endpoint, (req) => {
      const queryName = gql`
        ${req.body.query}
      `<some accessors to get query name>;
      if (operationNames.includes(queryName)) {
        req.alias = queryName;
      }
    });

This is run to alias graphQL requests so cypress can wait on them. When run in the body of tests, there are no problems. In the beforeEach, the problem seems to be that req.body is an empty string, so the query name is not found, and an error is thrown.

@alexandersdickinson I am experiencing exactly the same issue intermittently when setting up intercepts for GraphQL POST requests in the beforeEach: sometimes the body is empty. Did you find a solution in the end, or open another issue?