How to use global intercept for all tests in Cypress 6?

Viewed 3318

I want to test every page of my site (Vue/Nuxt), but API calls should be mocked.

For that there is intercept() function (route() in previous Cypress versions):

homepage.spec.js:

describe('home page', () => {
  before(() => {
    cy.intercept('**/user-data', { fixture: 'user.json' })
    cy.visit('http://localhost:8080')
  })

  it('renders header, () => {
      cy.contains('#header', 'test');
  });

  // some other tests for homepage
});

Cool, it works, but I have this specific API call (getting logged user info) on every page. I don't want to repeat this intercept code in every test file, for example contact.spec.js. I cannot find a way to set this intercept globally? What is the correct way to mocking axios?

0 Answers
Related