As a newcomer to Cypress, I find some aspects of it challenging despite its apparent simplicity. I am facing a specific issue:
const componentsRouteMatcher = {
pathname: '/component-management/api/components',
query: {
size: '5',
page: '0',
property: 'name',
direction: 'asc',
activated: 'true',
organisationId: '33'
}
};
beforeEach(() => {
cy.interceptStaticDataCalls(); // a custom command with interceptor registrations
cy.intercept(componentsRouteMatcher, {fixture: 'components/first-5.json'}).as('first5');
const composSecond5Route = Object.assign({}, componentsRouteMatcher, {query: {page: '1'}});
cy.intercept(composSecond5Route, {fixture: 'components/second-5.json'}).as('second5');
const composFirst50Route = Object.assign({}, componentsRouteMatcher, {query: {size: '50'}});
cy.intercept(composFirst50Route, {fixture: 'components/first-50.json'}).as('first50');
const composFirst20Route = Object.assign({}, componentsRouteMatcher, {query: {size: '20'}});
cy.intercept(composFirst20Route, {fixture: 'components/first-20.json'}).as('first20');
cy.intercept('/component-management/api/functional-areas', {fixture: 'functional-areas/functional-areas.json'}).as('fa');
});
it('should display all components based on a default filter', () => {
cy.visit("/");
cy.wait(['@first20', '@fa'], {timeout: 5000});
});
The problem lies in the last interceptor - although it loads a large JSON fixture file, it doesn't intercept properly. Surprisingly, it appears in Cypress's list of available routes. When I move this particular interceptor to the beginning of the beforeEach block, it works. It seems there might be an asynchronous process causing issues which is not very intuitive. Is there a reliable way to set up all these interceptors without having to guess their order?
I appreciate your help!
UPDATE:
// standard set of initializations required for all tests
Cypress.Commands.add('interceptStaticDataCalls', () => {
cy.intercept('/component-management/api/jira/project', { fixture: 'projects.json'});
cy.intercept('/component-management/api/appclientrelationships/apps', { fixture: 'apps.json'});
cy.intercept('/component-management/api/appclientrelationships/clients', { fixture: 'clients.json'});
cy.intercept('/component-management/api/appclientrelationships', { fixture: 'appClientRelationships.json'});
cy.intercept('/component-management/api/usergroups', { fixture: 'usergroups.json'});
cy.intercept('/component-management/api/organisations', { fixture: 'organisations.json'});
cy.intercept('/component-management/api/configs', { fixture: 'configs.json'});
//pathname used here to avoid intercepting current user request
cy.intercept({pathname: '/component-management/api/users'}, { fixture: 'users.json'});
})