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

intercept leaks between tests #27309

Closed
hamidmayeli opened this issue Jul 15, 2023 · 8 comments
Closed

intercept leaks between tests #27309

hamidmayeli opened this issue Jul 15, 2023 · 8 comments
Labels
stale no activity on this issue for a long period

Comments

@hamidmayeli
Copy link

Current behavior

When you have multiple tests reaching out to the same path, intercepts leak between tests.
There are a few close related to same thing and they suggest it should be fixed in V6.

Desired behavior

Tests be independent.

Test code to reproduce

describe("test", () => {
    beforeEach(() =>{
    });
    
    it("will work", (done) => {
        cy.intercept({
            method: "GET",
            url: "**/path",
        }, {
            statusCode: 200,
            body: { a: 1 },
            headers: {
                "content-type": "application/json"
            }
        }).as("call");

        fetch("http://localhost:400/root/path").then(response => { 
            response.json().then(json =>{
                expect(json).to.deep.equal({a: 1});
                done();
            })
        });

        cy.wait("@call");
    });
    
    
    it("will work 2", (done) => {
        cy.intercept({
            method: "GET",
            url: "**/path",
        }, {
            statusCode: 300,
            body: { message: "the-message" },
            headers: {
                "content-type": "application/json"
            }
        }).as("call");

        fetch("http://localhost:400/root/path").then(response => { 
            response.json().then(json =>{
                expect(json).to.deep.equal({ message: "the-message" });
                done();
            })
        });

        cy.wait("@call");
    });
    
    it("will fail", (done) => {
        cy.intercept({
            method: "GET",
            url: "**/path",
        }, {
            statusCode: 404,
        }).as("call");

        fetch("http://localhost:400/root/path").then(response => { 
            expect(response.status).to.be.equal(404)
            response.json().catch(_ =>{
                done();
            })
        });

        cy.wait("@call");
    });
});

Cypress Version

^12.17.1

Node version

v20.3.1

Operating System

Windows 10 x64 Version 10.0.19045 Build 19045

Debug Logs

I couldn't add the logs as it was too long.

Other

image

@DevJSter
Copy link

Hey there,

I'm a bit late but I noticed you're having a bit of trouble with your Cypress tests, where intercepts seem to be causing issues between different test cases. Not to worry, this is a common challenge that we can easily tackle.

You mentioned that you're using a beforeEach hook to set up your intercepts, but it looks like this might be causing the problem. The beforeEach hook can sometimes lead to intercepts not being isolated to a specific test, allowing them to affect other tests running in parallel.

To ensure that each test case runs independently without interference, let's make a small adjustment. Instead of setting up the intercepts in a shared beforeEach hook, we'll set them up directly within each test case.

Here's how you can do it:

describe("test", () => {
    it("will work", (done) => {
        // Set up the intercept for this specific test
        cy.intercept({
            method: "GET",
            url: "**/path",
        }, {
            statusCode: 200,
            body: { a: 1 },
            headers: {
                "content-type": "application/json"
            }
        }).as("call");

        // Perform your fetch and assertions
        fetch("http://localhost:400/root/path").then(response => { 
            response.json().then(json => {
                expect(json).to.deep.equal({a: 1});
                done();
            })
        });

        // Wait for the intercept to complete
        cy.wait("@call");
    });

    // Repeat the same pattern for other test cases
    // ...

});

By placing the cy.intercept directly within each test case, you're ensuring that the intercept is only active during that specific test's scope. This way, intercepts won't leak between tests, and each test case will run independently.

I hope this helps you achieve the desired behavior in your Cypress tests. Don't hesitate to reach out if you have any more questions or need further assistance. Happy testing!****

@hamidmayeli
Copy link
Author

Hi @DevJSter, I have already placed the intercepts in each tests. Please, have a closer look to the beforeEach, it's empty. But thank you anyway.

@DevJSter
Copy link

Alright sir 😄

@Renkoru
Copy link

Renkoru commented Oct 27, 2023

Any updates about this issue?

@hamidmayeli
Copy link
Author

@Renkoru, I manage to fix the issue by making the tests async by moving them in then. However, I am not sure if it would fix your issue too.

  it("will work 2", done => {
        cy.intercept({
            method: "GET",
            url: "**/path",
        }, {
            statusCode: 300,
            body: { message: "the-message" },
            headers: {
                "content-type": "application/json"
            }
        }).then(() => {
          fetch("http://localhost:400/root/path").then(response => { 
              response.json().then(json =>{
                  expect(json).to.deep.equal({ message: "the-message" });
                  done();
              })
          });
        });
    });

@Renkoru
Copy link

Renkoru commented Oct 30, 2023

Thank you @hamidmayeli for your reply. I have fixed my issue with disabling cache:

        (req) => {
          req.headers['Cache-Control'] = 'no-cache';
          req.continue((res) => {
            // ....
            res.send();
          });
        }

@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Apr 28, 2024
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale no activity on this issue for a long period
Projects
None yet
Development

No branches or pull requests

5 participants