question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cy.intercept() not intercepting fetch requests every 2nd or 3rd request

See original GitHub issue

Current behavior

I noticed that while most of the time fetch requests were being intercepted, other times they were not, causing my tests to fail when wait timed out.

I ended up taking one of the tests and copying it many times to see if there was any pattern.

Each test has 2 fetch requests (only 1 of which is attempted to be intercepted), and at least every 3rd tests fails, meaning no more than 2 tests run successfully one after the other.

image

Running the test 22 times: 14 passed, 8 failed with Timed out retrying after 5000ms: cy.wait() timed out waiting 5000ms for the 1st request to the route: foo. No request ever occurred. even though the logs show the request was in fact made

Desired behavior

cy.intercept() should intercept all matched routes from fetch requests

Test code to reproduce

// api.js
module.exports = {
  create: () => {
    return fetch('/foo', {
      method: 'POST',
      body: JSON.stringify({
        qux: 'qux'
      }),
      headers: {
        'Content-Type': 'application/json'
      }
    }
  },

  delete: ({ id } ) => {
    return fetch(`/foo/${id}`, {
      method: 'DELETE',
    }
  }
}
describe('Intercept fetch', () => {
  it('Test intercept fetch', () => {
    cy.intercept('POST', '/foo').as('foo')
    api.create()

    // do some awesome test

    cy.wait('@foo').then(interception => {
      api.delete({ id: interception.response.body.id })
    })
  })

  // repeat test at least 2 more times
})

Cypress Version

10.3.0

Other

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AtofStrykercommented, Aug 4, 2022

So either the documentation is wrong or cy.then() is invalid, but in either case, the documentation should be updated to reflect that.

To your point, I do not think this is well documented. FWIW we usually encourage things like this to be chained off of the existing command. I’ll talk with the team to see if this is something we want to be more of a known behavior.

0reactions
ezrag26commented, Aug 4, 2022

I was just confused because you were saying to use cy.then() which I thought was invalid since according to the documentation:

.then() requires being chained off a previous command.

I see an older issue (#466) where it was planned to convert .then into a child command, but this never seemed to have actually happened.

So either the documentation is wrong or cy.then() is invalid, but in either case, the documentation should be updated to reflect that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress cy.intercept Problems - Gleb Bahmutov
The command cy.intercept can match requests using a substring, a minimatch, or a regular expression. By default, it intercepts requests matching ...
Read more >
intercept - Cypress Documentation
cy.intercept() cannot be debugged using cy.request() ! Cypress only intercepts requests made by your front-end application.
Read more >
Cypress test - Do not intercept api request - Stack Overflow
Doing the cy.wait() right after the cy.intercept() is not going to work. Whatever triggers the API calls (a cy.visit() or a .click() )...
Read more >
How to intercept cy.request? Is it possible? #18706 - GitHub
Hi all,. I am trying to intercept a request from cy.request(). But the URL does not match. Here is a simple example. The...
Read more >
How to intercept API calls with cy.intercept() method in ...
... method cy. intercept() in Cypress 6 to intercept API requests and responses and provide sub for request or response body during API...
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