-1

In my beforeEach I have

cy.intercept('GET', '**/api/v2/invoices**').as('getPaidInvoices');

I have some tests where I use getPaidInvoices with exactle same path. But then I have different test

cy.intercept('GET', '**/api/v2/invoices**').as('getInvoices');
cy.wait('@getInvoices').then((xhr) => {
expect(xhr.response.statusCode).to.equal(200);

Then I have cy with each loop for options in drop-down menu. By clicking on each option inside, I await request different for each option. The address is almost the same, except the last two ** will get different arguments.

The problem seems Cypress doesn't like to share the value of getPaidInvoices with GetInvoices. On the line with

cy.intercept('GET', '**/api/v2/invoices**').as('getInvoices');

it says "This request matched: cy.intercept() spy with alias @getPaidInvoices

Is there any workaround? I need "/api/v2/invoices" to be used my multiple aliases. Or is there option to do it without aliases?

Thank you

enter image description here

Klea
  • 169
  • 7
Robert VeselĂ˝
  • 219
  • 3
  • 11

1 Answers1

1

This part 'GET', '**/api/v2/invoices**' is the "routeMatcher". When two intercepts use the same routeMatcher, the last one defined is supposed to be the one that handles the call.

However, from your description, it seems to be the other way round.

There's a couple of ways to handle it, the best IMO is to use a function and assign the alias within it

From Aliasing individual requests

cy.intercept('GET', '**/api/v2/invoices**', (req) => {

  // test the url string
  if (req.url.includes('paid')) {  // check the arguments
    req.alias = 'getPaidInvoices'
  } else {
    req.alias = 'getInvoices'
  }
})
Fody
  • 23,754
  • 3
  • 20
  • 37