Clear session between same-level describes in a single spec when using cy.session()
See original GitHub issueWhat would you like?
A nice addition to cy.session()
would be an option to clear sessions at command between same-level describes. This is especially useful in cases where there are multiple describes for various viewports. An option like clearAtBeginning
or something would do.
Why is this needed?
I have a couple of specs that run the same describe over three different viewports. At least one test changes the state to the point it has to be reset at the beginning of the next describe. Without the ability to clear the state between describes my tests are coupled.
It looks roughly like this:
const viewports = ['iphone-x', 'macbook-15'];
viewports.forEach(viewport => {
describe('My tests', () => {
beforeEach(() => {
cy.login(); // my custom login command utilising cy.session();
// all the other required stuff
});
it('does something that changes the state', () => {
// this test will work for the first time, but fail the second time due to saved state
});
});
Other
Currently I’m using a workaround with cypress-localstorage-commands
which I want to deprecate in favour of cy.session()
to clean up my code. At the beginning of each describe new session is created and preserved throughout the whole iteration of the single forEach element run.
Like that:
const viewports = ['iphone-x', 'macbook-15'];
viewports.forEach(viewport => {
describe('My tests', () => {
before(() => {
// set up the whole state and save at the end
cy.saveLocalStorage();
});
beforeEach(() => {
cy.restoreLocalStorage();
// more code required at this point
});
it('does something that changes the state', () => {
// this test will work all the time with that solution
});
});
However this is messy now, as I have two different things to maintain instead of one and more redundant code.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
@wojciech-sygier No problem! I am going to close this out for now since you don’t be able to verify.
BTW, congratulations on the new job! 🎉
To be honest, I don’t remember. I think my last comment remains valid, as I didn’t really do anything regarding the second problem I had and the solution for the first one worked (again, look at my comment). Since then I also changed my job to game dev, so I’m no longer using Cypress, at least for now. Thus, I can’t really verify whether it’s still a thing.