1

I am able to use cy.intercept() to intercept a backend API. But this backend API internally makes a call to a third party server. I want to intercept this internal call and stub it, but it's not happening. This internal call cannot be 'seen' in the Network requests of a browser, but it's definitely happening since it's coded in the backend API.

So to summarize, I can intercept a request but not the second request that the first request makes internally. How do I intercept this second internal request?

Many thanks in advance!

rojosa
  • 55
  • 2
  • 11

1 Answers1

3

You cannot with Cypress commands, they will only work with requests sent from the browser.

Cypress sets up a network proxy in the browser and catch requests made from the app and response back to the browser.

You can add a mock reply to the frontend intercept in order to cut out the backend API altogether.

This makes sense if you are testing the browser app, since you do not care what happens outside browser.

If you also wish to test then backend API, then call it directly (in a different test) and check it's response. See Example API Test

context("GET /users", () => {
  it("gets a list of users", () => {
    cy.request("GET", "/users").then((response) => {
      expect(response.status).to.eq(200)
      expect(response.body.results).length.to.be.greaterThan(1)
    })
  })
})

See the discussion here which asks the same question.