javierbrea / cypress-localstorage-commands

Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests and spec files. Allows disabling localStorage.
MIT License
178 stars 9 forks source link

Feature: Disable local storage #191

Closed Uninen closed 3 years ago

Uninen commented 3 years ago

Is your feature request related to a problem? Please describe.

When working with localStorage it's almost always necessary to handle exceptions as well. IT would be useful if this plugin would offer a way to easily disable the local storage.

Describe the solution you'd like

Maybe cy.disableLocalStorage() and cy.enableLocalStorage() commands, for example.

Describe alternatives you've considered

Don't actually know any easy alternative at the moment (hence the ticket).

Additional context

I really like this plugin, Thank You for your work 👍

javierbrea commented 3 years ago

Hi @Uninen, first of all thank you, I'm glad you like the plugin 😃.

I think it is a great idea to offer that feature, but I have doubts about the possible implementation, maybe you can help me:

I have thought two possible approaches:

I think I will investigate the first one approach deeper, but let me know your opinion please.

Uninen commented 3 years ago

While using the browser settings to disable localStorage sounds optimal, I'm pretty sure that path leads to more headaches in the long term, at least until Cypress has a better API for doing that with all supported browsers.

The second option sounds better, because it's more practical, and allows for more freedom when writing tests. We could for example always throw by default from setItem() but allow easy override if you want to return something else instead. For most of the use cases I can think of where you test the availability of localStorage this should be enough.

My current tests work like this, which was easy and clean to implement:

    cy.on('window:before:load', win => {
      cy.stub(win.localStorage, 'setItem').throws()
    })

(This should also cover almost all real code as at for example MDN docs explicitly state that "developers should make sure to always catch possible exceptions from setItem()" and based on my experience that's how the availability usually gets tested as well.)

javierbrea commented 3 years ago

Hi again @Uninen. Finally I have used your recommended approach, but I have also stubbed the getItem, removeItem and clear methods after checking that Chrome, Safari and Firefox are throwing errors also in those cases.

I have no implemented the enableLocalStorage command because the disableLocalStorage one has only effect for the current test, so it is enabled automatically again in the next one. This detail, added to the fact that the name enableLocalStorage could be a little bit confusing, because it would only enable localStorage if it had been disabled using the disableLocalStorage command made me to not implement it for the moment.

I have added also the possibility to use a custom error through the withError option in the disableLocalStorage command, as in:

cy.disableLocalStorage({
  withError: new Error("Disabled by cypress-local-storage")
});

In hope it will be useful 🙂

Thank you for your contribution!, I will mention it in the release notes and changelog.

Uninen commented 3 years ago

This is superb - absolutely spot on.

Thank You for your work @javierbrea 🙏