How to test an API has NOT been called. #27470
-
I have created a quick sample if you read the tests, the first 3 tests should fail but the next 3 should pass. However, the results seem to be random. describe("Test", () => {
[true, false].forEach(condition => {
it(`should not call ${condition ? "but does" : "and does not"} call first way`, () => {
// Arrange
cy.intercept(
{
method: "POST",
url: "/route",
},
cy.spy().as("theSpy")
).as("post");
// Code to test
const click = async () => {
if (condition)
await fetch(
"/route",
{
method: "POST",
body: "{'a': 1}",
});
};
// Act
cy.mount(
<div>
<button id="btn" type="button" onClick={click}>Test</button>
</div>
);
cy.get("#btn").click();
// Assert
cy.get("@theSpy").should("not.have.been.called");
});
it(`should not call ${condition ? "but does" : "and does not"} call second way`, (done) => {
// Arrange
const theSpy = cy.spy().as("theSpy");
cy.intercept(
"POST",
"/route",
() => {
theSpy();
}
).as("post");
// Code to test
const click = async () => {
if (condition)
await fetch(
"/route",
{
method: "POST",
body: "{'a': 1}",
});
};
// Act
cy.mount(
<div>
<button id="btn" type="button" onClick={click}>Test</button>
</div>
);
cy.get("#btn").click();
// Assert
setTimeout(() => {
expect(theSpy).to.have.not.been.called;
done();
}, 100);
});
it(`should not call ${condition ? "but does" : "and does not"} call 3rd way`, () => {
// Arrange
const theSpy = cy.spy().as("theSpy");
cy.intercept(
"POST",
"/route",
() => {
theSpy();
}
).as("post");
// Code to test
const click = async () => {
if (condition)
await fetch(
"/route",
{
method: "POST",
body: "{'a': 1}",
});
};
// Act
cy.mount(
<div>
<button id="btn" type="button" onClick={click}>Test</button>
</div>
);
cy.get("#btn").click();
// Assert
expect(theSpy).to.have.not.been.called;
});
});
}); So what am I doing wrong and how can I test what I needed? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Testing that an API call has not been made can sometimes be tricky due to the asynchronous nature of the test environment. It's possible that the randomness you're experiencing could be due to various factors, such as timing issues or the specific interactions between Cypress,
Remember that timing and the interactions between different Cypress commands can sometimes lead to unpredictable behavior. By using a combination of strategies like resetting spies, adding |
Beta Was this translation helpful? Give feedback.
Testing that an API call has not been made can sometimes be tricky due to the asynchronous nature of the test environment. It's possible that the randomness you're experiencing could be due to various factors, such as timing issues or the specific interactions between Cypress,
cy.spy()
, andcy.intercept()
. Let's discuss some potential improvements to your test setup to ensure more reliable results.Ensure Spies are Reset:
Before each test, it's a good practice to reset the spies to ensure that any state from previous tests doesn't carry over and affect the current test.
Using
.wait()
Command:Sometimes, adding a
.wait()
command after the…