I have a local stub server which has a PATCH request. When I call this local endpoint inside cypress, it fails. My cypress code
Cypress.Commands.add('callLocalAPI', (id: string = '1') => {
const options = {
method: 'PATCH',
url: `${Cypress.env().baseUrl}test/api`,
// failOnStatusCode: false,
retryOnStatusCodeFailure: true,
log: true,
body: {
id,
},
}
// we need cy to visit the current URL so it grabs cookie
// for our request
cy.visit('/')
cy.request(options).then((response) => {
if (!String(response.body.id)) {
console.error(response)
throw Error(`Fail to get response with ${id}`)
}
})
})
then calling this custom cypress command
cy.callLocalAPI('2')
When I access this endpoint using postman (http://localhost:8882/test/api), I get the expected response with a status code of 200 so there is no issues with the mock server.
But when I run the cypress tests, I always get a 404.
Status: 404 - Not Found
Headers: {
"server": "stubby/5.0.0 node/v12.19.0 (darwin x64)",
"date": "Fri, 03 Sep 2021 03:36:49 GMT",
"connection": "keep-alive",
"keep-alive": "timeout=5",
"transfer-encoding": "chunked"
}
Try
const options = {
method: 'PATCH',
url: 'test/api',
...
If you make a cy.request() after visiting a page, Cypress assumes the url used for the cy.visit() is the host.
cy.visit('http://localhost:8080/app')
cy.request('users/1.json') // url is http://localhost:8080/users/1.json
It may simply be that this line is missing a path separator
url: `${Cypress.env().baseUrl}test/api`
Cypress.env().baseUrl
Note that the value for this string is
taken from the file cypress.env.json
(not cypress.json
)
{
"baseUrl": "http://localhost:8882/"
}
or overridden in the command line
cypress open --env baseUrl=http://localhost:8882/