Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail tests that have unexpected API requests #910

Closed
Graham42 opened this issue Nov 14, 2017 · 5 comments
Closed

Fail tests that have unexpected API requests #910

Graham42 opened this issue Nov 14, 2017 · 5 comments
Labels
stage: ready for work The issue is reproducible and in scope type: feature New feature that does not currently exist

Comments

@Graham42
Copy link
Contributor

Is this a Feature or Bug?

Feature

Desired behavior:

I'd like a way to fail a test for any XHR calls that are not expected (ie. that no cy.route() has been set for). Current there's a force404, but that's not enough to be able to actually cause a test to fail

I had thought of using the onResponse option but it gets bypassed for the requests that force404 handles.

cy.server({
  force404: true,
  onResponse: xhr => {
    if (xhr.status === 404) {
      expect(true).to.equal(false);
    }
  }
});
@bryantabaird
Copy link

Still wasn't able to find a way to cause a test to fail if the force404:true is invoked, any updates on this issue?

@jennifer-shehane jennifer-shehane added type: feature New feature that does not currently exist stage: ready for work The issue is reproducible and in scope labels Nov 19, 2018
@TomPradat
Copy link

Any news about this, I'd love to see this happen. Is there a lot of work ? I can work on if not

@AlexDaniel
Copy link

So, am I missing something, or is there actually no way to check that no request during the test returned with 500 status code? If my backend responds with 500, then there's definitely an issue that I'd like to fix, so I want the test to fail.

@nwalters512
Copy link

I also ran into this. In my case, I had a test that was submitting a form, but due to a bug in the backend code under test, the server replied with a 403. Because I didn't actually assert anything about the response HTML, that test was passing when it shouldn't have. I can update the test to look for something specific in a correct response, but it'd be great to have the test automatically fail in that scenario.

@flotwig
Copy link
Contributor

flotwig commented Dec 15, 2022

Hi everyone, this is possible to do with cy.intercept(), because intercepts are matched in reverse order, it's possible to block all HTTP requests and only allow certain ones:

// block all HTTP requests
cy.intercept('*', (req) => {
  throw new Error(`Received an unexpected request to ${req.url}!`)
})
// allow some expected requests
cy.intercept('/api', (req) => {
  // call `req.continue()` to stop the request from propagating to the block handler
  req.continue()
})

Closing since this is now possible.

@flotwig flotwig closed this as completed Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: ready for work The issue is reproducible and in scope type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests

7 participants