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.

setting localStorage in beforeEach() fails occasionally

See original GitHub issue

I have a system with JWT authentication and, as I understand is a best practice, set the localStorage in a beforeEach() to ensure the user is logged in before each test.

describe('A test', function() {
    beforeEach(function() {
        if (this.currentUser) {
            localStorage.setItem('currentUser', this.currentUser);
            localStorage.setItem('token', this.token);
        }
    });
   it('does a test', function() {});
   it('does another test', function() {});

Occasionally, one of the tests fails as it perceives the user is not logged in. This seems random, i.e. the first one can pass and the second then fail. (Like, once in 500 it() tests).

As it is not reproducible it is rather hard to debug, as it only happens about once in 30 minutes running headless on a CI server. I would say however that it seems to have gotten worse with 3.3.1. Earlier, it was so rare that I could live with it.

I am using Electron browser.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
danatemplecommented, Jul 2, 2019

Thanks - I look forward to #686 being implemented. This is exactly what is needed.

For info, the workaround https://github.com/cypress-io/cypress/issues/461#issuecomment-392070888 (or my similar one, on the comment right below that one!) is what is not working 100%.

As I don’t use localStorage for anything that I don’t want to persist, the following does the trick reliably:

Cypress.LocalStorage.clear = function (keys, ls, rs) {
   return;
}
1reaction
danatemplecommented, Jun 30, 2019

Having done more testing now, I can confirm that the tests are absolutely rock solid if I prevent the LocalStorage.clear from having an effect, whereas the unreliability exists if I use the official workaround of trying to restore localStorage in a beforeEach before each test.

It seems there really needs to be a reliable way to keep DOM-critical local storage from getting deleted at all, even temporarily? I know the philosophy of cleaning up each time, but for an SPA with JWT, the user login is integral to the app state. The DOM state is after all allowed to persist.

Otherwise, each it() will need to re-start the app 100% from scratch with a full page load and reset the entire DOM, which for an SPA especially removes the point of having the it() level of test hierarchy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress clearLocalStorage and beforeEach - Stack Overflow
I recently started to use Cypress, and I read here that clearLocalstorage() is automatically executed every time a new test starts.
Read more >
How to save and restore state in Cypress tests - Reflect.run
The new cy.session() command solves this problem by caching and restoring cookies, localStorage and sessionStorage after a successful login. The ...
Read more >
Changelog - Cypress Documentation
Testing multiple origins in a single test with the new cy.origin() command. Caching and restoring cookies, localStorage , and sessionStorage between tests ...
Read more >
Mocking | Guide - Vitest
setSystemTime(date) // access Date.now() will result in the date set above expect(purchase()).toEqual({ message: 'Error' }) }) }) ...
Read more >
Testing localStorage exceptions with Cypress - Javier Brea
Preserve localStorage between tests and spec files; Disable localStorage to check error handling; Get, set and remove values from ...
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