I wrote some integration tests for my React App using Cypress (v 4.12.1). I've got a CI/CD on AWS where I need to integrate them. The products involved are GitHub (source repository), AWS CodePipeline, AWS CodeBuild.
If I have to test the aspect of one page, the Cypress test succeeds. If the test involves an API call, and I have to use the cy.wait() function, the error I get is this one:
CypressError: `cy.wait()` timed out waiting `5000ms` for the 1st request to the route: `agreements`. No request ever occurred.
I checked my server logs and I see the API request received, so the problem is Cypress that doesn't understand if the API call has been made or not. Then I found this issue on GitHub https://github.com/cypress-io/cypress/issues/3427 but the suggestions I found are the one I'm using:
- Make sure cy.server() and cy.route() are defined BEFORE you cy.visit() or BEFORE you do the action that will cause the XHR request to go out.
- Make sure to define the method on the cy.route() if needed. cy.wait() automatically only listens for GET requests by default.
And here you can find the code written for my integration tests:
context("Test Page", () => {
before(() => {
cy.server();
cy.route({
method: "POST",
url: `https://myserver.com/${Cypress.env("ENV")}/login`,
status: 201,
}).as("login");
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
cy.visit("http://localtest.myserver.com:3000/test_page");
// Dismiss cookie message
cy.get("[data-cy=dialog-actions]").find("button").click();
cy.log('Cookie message dismissed');
});
it("landed on the agreements page", () => {
cy.bypassLogin().then((xhr) => {
LOGIN_ID = xhr.body["loginId"];
cy.route({
method: "GET",
url: `https://myserver.com/${Cypress.env("ENV")}/login/${LOGIN_ID}/agreements`,
status: 200,
}).as("agreements");
});
cy.wait("@agreements").then((xhr) => {
NUM_AGREEMENTS = xhr.response.body.length;
let requiredAgreements = [];
let notRequiredAgreements = [];
xhr.response.body.forEach((elem, idx) => {
elem.isRequired
? requiredAgreements.push(idx)
: notRequiredAgreements.push(idx);
});
IDX_REQUIRED_AGREEMENTS = requiredAgreements;
IDX_NOT_REQUIRED_AGREEMENTS = notRequiredAgreements;
expect(xhr.status).to.eq(200);
});
});
Can someone help me with this issue?