I can see the answer worked for author, but in case anyone needs to share data between different test files, the solution is to use cy task method and store data in Node environment, e.g. in my case I needed to store user data:
// cypress/plugins/index.ts
export default (on, config) => {
on('task', {
setUserData: (userData: UserDataType) => {
global.userData = userData;
return null;
},
getUserData: () => {
return global.userData;
},
});
};
then in test case we can do:
// cypress/integration/login.spec.ts
describe('Login', () => {
it('should work', () => {
cy.visit('/login-page');
cy.intercept('api/login-endpoint').as('postLogin');
// login interactions
cy.wait('@postLogin').then((interception) => {
// intercept user data and store it in global variable
cy.task('setUserData', JSON.parse(interception.response.body));
});
// ... further assertions
});
});
later we can retrieve this data easily:
// cypress/integration/otherTest.spec.ts
describe('Other test', () => {
it('uses user data', () => {
cy.task('getUserData').then((userData: UserDataType) => {
console.log(userData);
// voila! Stored data between two .spec files
});
});
});
You'll also need to extend Node TS types for this, but this answer is long enough already.
Yes, I know it is not a great way of writing tests at all, as it makes them depend on each other, but sometimes a long interaction flow in application makes it necessary.