I would like to mock some of the API calls done by my SPA. Therefor I am using cypress.JS and checked how to do this by using following test.
it("Then it works", () => {
axios.defaults.baseURL = 'http://localhost';
cy.server()
cy.route("GET", "http://localhost/users/", true)
axios.get("/users/").then(response => {
console.log("received response: " + response)
expect(response.body).to.equal(true)
}).catch(error => console.log(error))
})
It does not work I get the error "Access to XMLHttpRequest at 'http://localhost/users/' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
Is there any way to prevent this error during tests? I don’t understand why Cypress is handling this simple test in a way that this error can occure.
http://localhost
andhttp://localhost:8080
are not the same origin. – Quentin