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

[7.0.0] cy.intercept(): stubbed responses still send request to server #15841

Closed
risa-hdqc opened this issue Apr 7, 2021 · 8 comments · Fixed by #15942
Closed

[7.0.0] cy.intercept(): stubbed responses still send request to server #15841

risa-hdqc opened this issue Apr 7, 2021 · 8 comments · Fixed by #15942

Comments

@risa-hdqc
Copy link

Current behavior

After upgrading from 6.7.1 to 7.0.0, I noticed my tests are still working as expected, but my server logs show requests being made to my server for intercepted requests using fixtures, objects, or strings.

    cy.intercept('/api/getThings', { fixture: 'getThings' });
    cy.intercept('/api/getAnotherThing', 'AnotherThing');
    cy.intercept('/api/getYetAnotherThing', { text: 'YetAnotherThing' });

Each of these intercepts will return the stubbed response, but the requests are still being made afterward.

Example server log:

GET /api/getThings 200 4.389 ms - -
error: GET - 500 - /api/getThings -- some error because I'm not using a live service
GET /api/getAnotherThing 200 2.312 ms - -
error: GET - 500 - /api/getThings -- some error because I'm not using a live service
GET /api/getYetAnotherThing 200 4.231 ms - -
error: GET - 500 - /api/getYetAnotherThing -- some error because I'm not using a live service

Desired behavior

The requests should NOT go to the server after returning the stubbed response.

GET /api/getThings 200 4.389 ms - -
GET /api/getAnotherThing 200 2.312 ms --
GET /api/getYetAnotherThing 200 4.231 ms --

Current workaround

req.reply works as expected, blocking the requests to the server.

    cy.intercept('/api/getThings', (req) => req.reply({ fixture: 'getThings' }));
    cy.intercept('/api/getAnotherThing', (req) => req.reply('AnotherThing'));
    cy.intercept('/api/getYetAnotherThing', (req) => req.reply({ text: 'YetAnotherThing' }));

Versions

7.0.0

@bahmutov
Copy link
Contributor

bahmutov commented Apr 7, 2021

Hmm, I do see it in v7.0.0

Reproduction: https://github.com/cypress-io/testing-workshop-cypress/tree/intercept-calls-server-15841

  • clone the repo
  • checkout branch intercept-calls-server-15841
  • npm install
  • npm run dev starts the server and Cypress
  • click the spec 05-xhr/answer.js it has a single test it.only

The test stubs the GET /todos with a fixture

it.only('starts with zero items (fixture)', () => {
  // stub route `GET /todos`, return data from fixture file
  // THEN visit the page
  cy.intercept('GET', '/todos', { fixture: 'empty-list.json' }).as('todos')
  cy.visit('/')
  cy.wait('@todos') // wait for `GET /todos` response
    // inspect the server's response
    .its('response.body')
    .should('have.length', 0)
  // then check the DOM
  cy.get('li.todo').should('have.length', 0)
})

I see the stub working - but I also see the server showing GET /todos 200 message in the terminal

Screen Shot 2021-04-07 at 10 37 44 AM

@flotwig
Copy link
Contributor

flotwig commented Apr 7, 2021

@risa-hdqc did this only start happening with 7.0.0?

@flotwig
Copy link
Contributor

flotwig commented Apr 7, 2021

FYI, the stdout printed by Cypress will show all requests, regardless of they are intercepted or not, so you can't rely on that to tell if the request is going to the destination server or not

@bahmutov
Copy link
Contributor

bahmutov commented Apr 7, 2021

@flotwig

I tried testing-workshop example in v6.8.0 and with separate terminals to avoid the confusion. Did NOT show the request reaching the server

When switching to Cypress v7.0.0 the server IS showing GET /todos request, even when stubbed

@Alex-passiv
Copy link

Hello,

Just adding to the pot saying I have been experiencing the same issue. All my stubs have now been reaching the server. Commenting so I can stay updated for a fix. Thanks!

@flotwig
Copy link
Contributor

flotwig commented Apr 8, 2021

I've bisected the issue down to this PR: #14543 I'll keep looking at it tomorrow.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 12, 2021

The code for this is done in cypress-io/cypress#15942, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 12, 2021

Released in 7.1.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v7.1.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Apr 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants