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 the request

See original GitHub issue

Current behavior

The XHR request finishes with Failed to fetch even though the request is stubbed in beforeEach. (See screenshot, showing the routes being stubbed on the left, and the failed request in the network inspector)

image

Notes:

  • This is flaky, re-running the test suite might end in a success.
  • The stubbed request is for a domain different than the app

Desired behavior

Stubbed request should properly return the value they are given as a stub.

Test code to reproduce

This is the relevant part of the setup. I am well aware this is not a reproduction but there is no easy way to make a small reproduction.

Any pointers, tips, suggestion is more than welcome. I’ve been trying to solve this for a while now

const iofogControllerInfo = {
  baseURL: 'http://localhost:51121',
  credentials: {
    username: '',
    password: ''
  }
}

const applications = [{
  name: 'Cypress application',
  id: 1,
  routes: [],
  microservices: [],
  isActivated: true
}]

const microservices = []

const GET_APP_MICROSERVICES = (name) => `/microservices?application=${encodeURIComponent(name)}`


beforeEach(() => {
    //  ... Bunch of other intercepts
    cy.intercept('GET', `${iofogControllerInfo.baseURL}${GET_APP_MICROSERVICES(applications[0].name)}`, { statusCode: 200, body: { microservices } }).as('getMicroservices')
})

Cypress Version

7.9.0 ~ 8.2.0 (Tried them all)

Other

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

2reactions
Surm4commented, Aug 18, 2021

Got the same problem. I use intercept and req.reply to stub the response and it sometimes succeeds, sometimes not test preview:

    it('edits the code', () => {
      cy.intercept('PATCH', `code/${codeId}`, req => {
        const body = JSON.parse(req.body)

        req.reply({
          ...body,
          id: codeId,
        })
      }).as('codeUpdated')

      cy.get('.monaco-editor').type('my new code')
      cy.get('.codeDialog').contains('Save').click()
      cy.wait('@codeUpdated')

      cy.get('.codeDialog').should('not.exist')
    })
0reactions
christo8989commented, Jun 12, 2022

Mine fails because cypress isn’t recognizing that the request has returned.

The first test succeeds but then other tests fail.

image
Read more comments on GitHub >

github_iconTop Results From Across the Web

cy.intercept() not stubbing API in Cypress - Stack Overflow
I managed to get stubbing working with the pattern @eric99 gave, ... cy.intercept() will not work as the API call is done server-side....
Read more >
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
The lifecycle of a cy.intercept() interception begins when an HTTP request is sent from your app that matches one or more registered cy.intercept()...
Read more >
Unable to intercept request using cy.intercept #20536 - GitHub
I've read the docs repeatedly and tried different versions of the interceptor syntax, but it never intercepts. Is this a bug? Desired behavior....
Read more >
A Practical Guide to Intercepting Network Requests in Cypress
Our .intercept() command is now matching any request that contains the url ... command does not actually wait for our network request to...
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