intercept leaks between tests
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
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!****
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.
Alright sir 😄
Any updates about this issue?
@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();
})
});
});
});
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();
});
}
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.
This issue has been closed due to inactivity.