Cypress handling big/long fixtures

Viewed 42

Context:

I now need to start implementing E2E tests for a webapp that uses a "complicated" api. So for testing one site I need to intercept about 15 REST requests. That API is obviously not well designed, but I can't change it because it is from a third party and my webapp relies on that.

Questions:

  1. How should I handle the cy.intercept() calls? Because just for one test it quickly becomes hard to read what actually happens, because the interception of the requests takes 50 lines of code.

  2. Should I keep the cy.intercept() calls in my test file or should I outsource them like this:

fixtures/car-list.ts

export function mockCarList() {
 cy.intercept()
 cy.intercept()
 ...
}

integration/car-list.spec.ts

import { mockCarList } from '../fixtures/car-list'

describe('Test car', () => {
  it('Should test car list', () => {
    mockCarList();
    
    do test stuff
  });
});
1 Answers

Your approach is valid, but don't put the function in fixtures because it's not really a fixture (i.e. data).

Put it in /cypress/support/commands.js and make it a custom command so that you don't need to import it into the test.

// command.js

Cypress.Commands.add('mockCarList', function() {
  cy.intercept()
  cy.intercept()
  ...
}

Test

describe('Test car', () => {
  it('Should test car list', () => {
    cy.mockCarList();
    
    do test stuff
  });
});
Related