https://cypress.io logo
Why request from previous describe block is cancel...
# i-need-help
n
Request from describe block 1 executes, when second block starts and sends same request, which cancels same request from describe block 1, how I can send multiple requests?
e
Can you provide more information and/or a code snippet of what you are trying to accomplish?
n
I have e2e test, which executes two request to same API, but with different params, and second request somehow cancels first request.
Service named pay under the hood calls to another API over HTTP to get ip address.
e
Right, this is supported. You can make multiple request calls against the same API as needed. Cypress wouldn't cancel a request it's sent, that would be handled by your BE. I was wondering if a code snippet might help to display implementation details. Perhaps you need to wait on the first request to finish before executing the second?
n
In first describe block I intercept to HTTP call, one for "ip", and one for "pay" for test one card (1), in second describe block I intercept to HTTP call, one for "ip", and one for "pay" (2). When I run test call to "ip" method in (1) in Network tab marked as cancelled, exactly after (2) "ip" method is called.
Maybe I must wait, I tried to use cy.wait('@alias'), but it fails with timeout error.
If I execute only one describe block 2 requests (one to "ip", and on for "pay") works as expected without errors.
Copy code
ts

describe('mode demo disabled', () => {

    let sessionId: string;
    let orderId: string;

    beforeEach(() => {

      cy.request<{ data: { sessionId: string; orderId: string; } }>({
        method: 'POST',
        url: `${MERCHANT_API_HOST}/init`,
        body: initParams,
      }).as('init').then(res => {
        sessionId = res.body.data.sessionId;
        orderId = res.body.data.orderId;
      });

      cy.intercept('GET', `**/api/ip`, { fixture: 'api.ip.json' }).as('ip');
      cy.intercept('POST', `**/purchase`, { fixture: 'success.json' }).as('purchase');

    });

    it('should display welcome message', () => {

      cy.visit(
        '',
        {
          onBeforeLoad(win: Cypress.AUTWindow) {
            cy.spy(win, 'postMessage').as('postMessage');
          },
        },
      );

      cy.get('[data-testid="card-number"]').type(TEST_CARD_WITH_3DS_PAYMENT.cardNumber);
      cy.get('[data-testid="expire"]').type(TEST_CARD_WITH_3DS_PAYMENT.expire);
      cy.get('[data-testid="cvv"]').type(TEST_CARD_WITH_3DS_PAYMENT.cvv);
      cy.get('[data-testid="name-on-card"]').type(TEST_CARD_WITH_3DS_PAYMENT.nameOnCard);
      cy.get('[data-testid="submit"]').click();

      cy.get('@postMessage').should('be.calledOnce');
    });


  });
This is first describe block, second block exactly the same, but with different params in request

https://cdn.discordapp.com/attachments/1098602319907667998/1098640925141512322/Screenshot_2023-04-20_at_22.06.23.png

first ip call is cancelled, second looks good, and token after ip called without problems
I expect 4 calls: ip, token for first describe block, and ip, token for second describe block
e
So it looks like you are trying to mock those endpoints, so they aren't "real requests" since you are trying to provide a mocked response. So I would try two things - If you run only the second test do you get the same behavior? Are you sure that your intercepts are actually catching all instances of the
ip
endpoint? - From your image above the canceled
ip
request has a different initiator
VM2523 ...
than the second
ip
request that comes from the pay service. Can we confirm that this is the same endpoint Also is there actually any issues with the second test or are you just wondering about the cancelled endpoint? It looks like maybe that endpoint is being called from some other source and maybe it shouldn't be? I don't know the BE at all but it might be worth just manually poking through the app and seeing if you see the same set of network calls as you do with Cypress. My guess is that you would see the same canceled endpoint just manually testing. If you don't then the only thing other thing I can think of is that Cypress refreshes the state of the app when it runs the second test and it could be cancelling any in-progress requests when it clears and revisits the app for the second test, which would be expected of course.
n
It's interesting, but now first ip call was success, but token cancelled
I inspected both ip requests, they are same

https://cdn.discordapp.com/attachments/1098602319907667998/1098645838907064411/Screenshot_2023-04-20_at_22.26.08.png

I mean headers, url and method are same in both ip calls
And I doesn't understand why initiator is different
How can I check what intercepts actually catch all instances of
ip
enpoint?
e
You can find all the route information in the test runner, on the left side toward the top. I've attached an image of mine, with all the route information removed. But you can easily see the method, the route, whether it's stubbed or not, and how many times the test actually found that api call. You can then use the rest of the of the command information to identify at what point in the test command list the API call was found or not.

https://cdn.discordapp.com/attachments/1098602319907667998/1098649132396195994/image.png

https://cdn.discordapp.com/attachments/1098602319907667998/1098649132647858226/image.png

n
Thank you for your help, I think I found what was an issue
e
Do you mind sharing in case anyone else runs into the same problem? I'm happy it's resolved 🙂
n
I think I misunderstood how to properly use cypress, in this case, I should not consider in which state was request in Network tab, just test what it have expected result.
cy.wait
was before submitting form (request sent after form is submitted), and wherefore it fails with timeout, in Network tab request was in cancelled state, and this situation sent me on the wrong path 😁
e
Happy it's resolved! 🙌