I am currently testing an application using cypress. This application has a back-end(API) and a front-end. Cypress is used to test the front-end.
Before beginning a test, we initialize the DB of the back-end to have it in the state needed for the test. We achieve that by sending direct requests to the backend, using cy.request()
.
Here is the code of one of the request in cypress (this one is the problematic one):
Cypress.Commands.add("assignCityToTerritory", (territory, cityId) => {
return cy.request({
method: 'POST',
url: Cypress.env('apiUrl') + '/territories/' + territory.id + '/city-assignment',
headers: {
Authorization: 'Bearer ' + window.localStorage.getItem('jwt')
},
body: {
city: {
id: cityId
}
}
}).then(response => {
return response
})
})
Here comes the problem:
- If my backend runs locally and my front-end connects to my local back-end, this works fine.
- If I use the exact same version of the backend but on a distant server, the
cy.request()
times out and says it never receive any answer from the server - In the same test, other calls with
cy.request()
to the distant back-end work fine just before the problematic one. For example this one:
Cypress.Commands.add("create", (itemType, itemData) => {
cy.log("# Creating " + itemType)
return cy.request({
method: 'POST',
url: Cypress.env('apiUrl') + '/' + itemType + '/',
headers: {
Authorization: 'Bearer ' + window.localStorage.getItem('jwt')
},
body: itemData
}).then(response => {
return response
})
})
- The exact same request (same URL, same method, same payload) works well when sent through postman to the distant backend and the backend answers almost immediately.
- Even though cypress does not get the answer from distant the back-end, said back-end does execute and answers to cypress' request. It really looks like for some reason, cypress is not aware of the response.
Conclusion: I have no idea why this specific cy.request()
times out