question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clear session between same-level describes in a single spec when using cy.session()

See original GitHub issue

What 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:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
emilyrohrboughcommented, Nov 16, 2022

@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! 🎉

1reaction
wojciech-sygiercommented, Nov 16, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

session - Cypress Documentation
Because cy.session() clears the page and all session data before running setup , you can use it to easily switch between sessions without...
Read more >
Cypress resets to cy.visit() after running each describe section
The new cy.session() command solves problem by caching and restoring cookies, localStorage and sessionStorage after a successful login.
Read more >
Login using Cypress Session Command before every test
This article is about how to login into app before every test using cy.session API command. Usually in Test Automation, we may repeat...
Read more >
Practical Regression and Anova using R - R Project
1.2 When to use Regression Analysis. Regression analysis is used for explaining or modeling the relationship between a single variable Y, called.
Read more >
How to save and restore state in Cypress tests - Reflect.run
Note: Amongst other things, enabling the session API means Cypress will clear the page at the beginning of each test, so cy.visit() must...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found