0

I'm using the new Cypress component test option for my frontend tests, and I absolutely love it. I'm using it as an integration test solution, mounting the root component of my react app and using Cypress to test extensive user interactivity with it. In nearly every way, it's working perfectly, despite its beta status.

For API calls, I'm using the Cypress intercept() feature. Before each test I use intercept to define the mocked API responses I need for the test. It works great.

What I've noticed, however, is if there is an API call that doesn't impact my test, but is still fired in the background, it'll cause a CONREFUSED error. In and of itself, this doesn't really impact my tests, it just outputs the error to the log. However, the completionist in me doesn't like this.

Ideally, I'm hoping there is an option in Cypress where if any CONREFUSED errors occur in an ajax call, it'll fail the test. This may be out of scope for what Cypress offers, and I'm not really sure how to even accomplish it. However, if there is a way, I would love to integrate it into my test suite.

halfer
  • 19,824
  • 17
  • 99
  • 186
craigmiller160
  • 5,751
  • 9
  • 41
  • 75

2 Answers2

2

Generally throwing an error will fail the test, try doing so in an intercept.

cy.intercept(url, (req) => {
  req.continue((res) => {
    if (res.body.statusCode === 502) {
      throw new Error('Conncetion refused')
    }
  })
})
TesterDick
  • 3,830
  • 5
  • 18
  • I like this in general, but it seems this solution would need to be applied to every single intercepted request, which is not ideal for my goals. – craigmiller160 Nov 15 '22 at 21:08
  • I presumed that it happens on a specific URL (***there is an API call that doesn't impact my test***), are you saying it can happen on any fetch? If that's the case, you have bigger problems. – TesterDick Nov 15 '22 at 21:47
  • 2
    BTW an intercept can be as general as you need, using minimatch syntax. – TesterDick Nov 15 '22 at 21:47
  • While yes, it happens on a specific URL, it's not that it's just one URL for the entire test suite. This occurs when there is an API call being made by the code that I forget to apply an intercept() for in the test. That's why I want to fail the test if there's ever a CONREFUSED, to catch these kind of mistakes. – craigmiller160 Nov 18 '22 at 16:34
0

You could write a rule based on the event that is triggered on CONREFUSED as suggested here in the docs.

To find out what the exact event trigger keyword is you could run a verbose debug session as described here in the docs and then add the resulting event into the exception handling?

onewaveadrian
  • 434
  • 1
  • 6
  • 17