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

cy.wait for alias registered to non-first matching cy.intercept does not have response property (body & status) #14205

Closed
core01 opened this issue Dec 16, 2020 · 9 comments
Labels
topic: cy.intercept() type: unexpected behavior User expected result, but got another

Comments

@core01
Copy link

core01 commented Dec 16, 2020

Hi guys, thank you so much for new intercept method, it is a great job!

But I have a problem, I need to have an opportunity to modify body for all requests to API to pass isTestData flag which will indicate that this data was created by cypress and it can be deleted. To do this I created beforeEach for all test cases and add cy.intercept for all POST requests, but It breaks the ability to receive a response body or status in cy.wait.

Current behavior

response property doesn't exist
image

Desired behavior

response property should always exist

Test code to reproduce

I created a repo with a small test case which show my problem https://github.com/core01/cypress-intercept/blob/main/cypress/integration/spec.js

And below you can see a code from a project I'm working on:

support/beforeEach.js:

beforeEach(() => {
  cy.intercept('POST', '*', (req) => {
    req.body.isTestData = true;
  });
});

support/commands.js:

Cypress.Commands.add('createOrganization', (organizationName) => {
  Cypress.log({
    name: 'createOrganization',
    message: 'Create a superuser and new organization',
  });

  cy.intercept('POST', '**/organizations').as('createOrganization');
  cy.intercept('GET', '**/organizations/*/profiles?pageSize=*').as('getProfileList');

  // visit organization creation page and fill form
  cy.visit('/create-organization');
  cy.get('[data-test=input-organization-name]').type(organizationName);
  cy.get('[data-test=btn-create-organization]').click();

  // check createOrganization request and assign an alias for organizationId
  cy.wait('@createOrganization').then((interception) => {
    console.log('aaaa', interception);
  }).its('response.body.id').as('organizationId');
});

support/index.js:

import './commands';
import './beforeEach';

Versions

Cypress 6.1.0
Chrome/Electron. Mac Big Sur

The same problem was described in this comment, but I think that it deserves a separate issue

Related issue - #8493

@avient
Copy link

avient commented Dec 16, 2020

Having the same issue on some requests with intercept(). But works as expected with route()

@vrknetha
Copy link

Same as this , already filed since the release 5.6.0

#9150

@jennifer-shehane
Copy link
Member

I can reproduce this with the given example. I believe this is because the alias is registered to the second intercept. We don't support overriding intercepts that match exactly, but are working on support: #9302 I'm not completely sure this will be resolved by that solution though, so I'm leaving this open.

I actually think this is this issue, but Kevin said his issue was resolved. #9262 So, I'll leave this issue open as the reproducible example.

Reproducible example

it('route should work', () => {
  cy.server()
  cy.route('GET', '**/comments/*')
  cy.route('GET', '**/comments/*').as('getComment')

  cy.visit('https://example.cypress.io/commands/waiting')

  cy.get('.network-btn').click()

  // ✅ passes 'status' does exist
  cy.wait('@getComment').its('status').should('be.oneOf', [200, 304])
})

it('intercept should work', () => {
  cy.intercept('GET', '**/comments/*')
  cy.intercept('GET', '**/comments/*').as('getComment')

  cy.visit('https://example.cypress.io/commands/waiting')

  cy.get('.network-btn').click()

  // ❗️ fails 'response' does not exist
  cy.wait('@getComment').its('response.statusCode').should('be.oneOf', [200, 304])
})

Screen Shot 2020-12-29 at 3 43 12 PM

@jennifer-shehane jennifer-shehane added the type: unexpected behavior User expected result, but got another label Dec 29, 2020
@cypress-bot cypress-bot bot added the stage: needs investigating Someone from Cypress needs to look at this label Dec 29, 2020
@jennifer-shehane jennifer-shehane changed the title It is not able to receive a response (body & status) in cy.wait for cy.intercept cy.wait for alias registered to non-first matching cy.intercept does not have response property (body & status) Dec 29, 2020
@cypress-bot cypress-bot bot added stage: ready for work The issue is reproducible and in scope and removed stage: needs investigating Someone from Cypress needs to look at this labels Jan 20, 2021
@VinitMaheswari
Copy link

@jennifer-shehane Any update on this bug fix? When shall we expect fix for this bug.

Thanks.

@VinitMaheswari
Copy link

VinitMaheswari commented Feb 4, 2021

As a work around, To get response from an intercept().

Intercept an API, Take the request from the intercepted method. Then use cy.request() for that intercepted URL and body (if present). And then find the response from cy.request().

It's not best practice. But can help as a work around in case we need response from the intercepted API.

@core01
Copy link
Author

core01 commented Feb 4, 2021

@VinitMaheswari could you please provide an example with code?

@VinitMaheswari
Copy link

VinitMaheswari commented Feb 4, 2021

HI @core01

Please find the below code and Let me know if you need any help. The same piece of code is working for me.

cy.intercept('POST', '/API Need to be intercpeted**').as('intercept');
// Click for an event to call API. For example: here clicking on a button
cy.findByRole('button', {
name: 'Search'
}).should('exist').click();
// Waiting for an alias
cy.wait('@intercept').then((res) => {
//Storing the request body and URL of an intercepted API
let body = JSON.stringify(res.request.body);
let URL = Path of the intercpeted API or find it.As done
for body.
console.log('URL ' + URL);
console.log('Body ' + body);
//Now calling the same API with request() method.
cy.request({
method: 'POST',
url: URL,
body: body,
}).then((response) => {
// response.body is automatically serialized into JSON
expect(response.status).to.eq(200);
console.log('Response Body ' + JSON.stringify(response));
});
});

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Mar 16, 2021

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

@flotwig flotwig closed this as completed Mar 16, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 5, 2021

Released in 7.0.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Apr 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
topic: cy.intercept() type: unexpected behavior User expected result, but got another
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants