cy.request(...) fails if server is not available, with no option to return or catch the connection error

See original GitHub issue

Current behavior

When you use cy.request(...) to connect to a server that isn’t running, it throws a CypressError that is not possible to catch or otherwise handle gracefully:

CypressError: cy.request() failed trying to load:

http://localhost:5338/percy/healthcheck

We attempted to make an http request to this URL but the request failed without a response.

We received this error at the network level:

  > Error: connect ECONNREFUSED 127.0.0.1:5338

Desired behavior

I would expect that something similar to failOnStatusCode: false exists to allow me to handle the “connection refused” case in the same way as I can handle a, say, 404 code.

An alternative could be a catch method to be chained off of cy.request(...).

Context

I’m adding new functionality to the Percy Cypress plugin, which is implemented as a custom Cypress command (https://github.com/percy/percy-cypress). I need to do a check to see if the local percy agent service is running, before proceeding with any more work. Our users typically want to be able to run their test suite with or without “Percy mode”, so when the local percy service is not running, we’d like to issue a warning but otherwise allow tests to proceed normally.

Because I want to avoid CSP or other security issues in the browser, I want to use cy.request(...) or some other capability that allows me to do HTTP requests from outside of the browser context.

If I was doing this for my own test suite, I could use a new cy.task(...) to implement this healthcheck, but in the context of a Cypress plugin, that would require users to do additional setup in their plugins/index.js, in addition to what they already have to do in support/commands.js.

An alternate solution to this problem would be exposing an API in Cypress that allows me to programmatically add a cy.task(...) that I can then use in my custom command.

Another alternative would be a hook to catch this CypressError within a command, and the ability to inspect it and re-throw it if appropriate.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:5
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
flotwigcommented, Aug 20, 2019

I was thinking we could add a new option to cy.request:

cy.request({
	url: 'http://something.invalid',
    failOnNetworkError: false // default: true
})
.then(res => {
	// res is an object with the Node.js Error `code` and `message` attached
    // should probably either have `res.error = true` or `res.error = { code, message }`
    // so users can tell it apart from a success
})

@Robdel12 If you want to open a PR to add this feature, that would be great! I believe you just need to update commands/request.coffee to accept the new option and then add a condition in the .catch for cy.request here:

https://github.com/cypress-io/cypress/blob/beaa105b50a2a5a1b989688396b25c58b2013772/packages/driver/src/cy/commands/request.coffee#L244-L253

Any new tests belong in in request_spec.coffee

6reactions
stock-ddcommented, Aug 13, 2020

Absolutely terrible. Cypress disallows using your own REST client or everything breaks. Meanwhile they don’t allow to catch errors? Currently at work in a situation where a cy.request breaks and there is no way to find out how. Please fix this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress cy.request expect to throw/fail - Stack Overflow
request is doing it's job as by default it is configured to fail the test when the request fails.
Read more >
request - Cypress Documentation
A request body to be sent in the request. Cypress sets the Accepts request header and serializes the response body by the encoding...
Read more >
Network Requests | Cypress examples (v9.5.0) - Gleb Bahmutov
The cy.request command automatically fails if the server responds with an error code ... 404) // our server returns an empty object if...
Read more >
A Practical Guide to Intercepting Network Requests in Cypress
If you have worked with network in Cypress before, you are probably aware of the limitation of .route() command that is a predecessor...
Read more >
500 Internal Server Error - HTTP - MDN Web Docs - Mozilla
This error response is a generic "catch-all" response. Usually, this indicates the server cannot find a better 5xx error code to response.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found