Cypress intercept not matching url

Viewed 2356

I am fetching data with a network call:

https://mydomain.xxx/third-party-service/pragma?perPage=15&page=1
Request Method: GET

Trying to intercept it with code:

// Overriden not to clear localStorage authentication tokens
const clear = Cypress.LocalStorage.clear;
Cypress.LocalStorage.clear = function(keys) {
  if (keys) {
    return;
  }
};

context('Navigation', () => {
  before(() => {
    cy.login();
    cy.visit('/');
  });
  beforeEach(() => {
    cy.get('[data-test=test-burger]').click();
  });

  it('Tests table', () => {
    cy.get('[data-test=invoices]').click();
    cy.intercept('**/pragma**').as('getPragmaDocuments');
    cy.wait('@getPragmaDocuments');
    //....assertions here after API call is waited
  });
});

However, it does not intercept the network request.

Error I get:

Timed out retrying after 5000ms: cy.wait() timed out waiting 5000ms for the 1st request to the route: getPragmaDocuments. No request ever occurred.Learn more

1 Answers

I see your problem, and I'm not sure why your "fix" worked, haha.

You need to start the cy.intercept() before the click, like this:

cy.intercept('**/pragma**').as('getPragmaDocuments');
cy.get('[data-test=invoices]').click();
cy.wait('@getPragmaDocuments');
Related