cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.01k stars 3.18k forks source link

Cypress Component Testing with Local Storage #29178

Open iepoch opened 7 months ago

iepoch commented 7 months ago

Current behavior

Problem

In my current cypress component test if I run the component test locally the local storage sets the data and test will pass when you run cypress open command ie: npx cypress open --component In the image below you can see this is the Components Test and that I have Mocked the localStorage to show these items listed

image

However when running the test through cypress run command the LocalStorage gets set but the component never renders the data ie:

npx cypress run --component

This is a screenshot of the headed cypress run is when the component is loading. I have tried BeforeEach and even using

Cypress.on('test:before:run:async', async () => {
  await window.localStorage.setItem('cartItems', JSON.stringify(items))
});

The data does load before this mounts. image

This is a screenshot of the headed run you can see it here it populates the Data in local storage but after the component has rendered.

image

Yet the CartDrawer is not rendering the items because Local Storage and react does not force a re-render of the component. And yet the test when running through cypress open command the component test render fine with the items and pass all the time. What is causing this difference between cypress open and cypress run?

Desired behavior

Would like to be able to use LocalStorage with cypress components to mock the output of the component. Should work the same as cypress open. image

Test code to reproduce

You can fork my repo where I have created this test here My repo

Cypress Version

13.7.0

Node version

18.17.0

Operating System

Windows 11

Debug Logs

Log is too long

Other

None

iepoch commented 7 months ago

image I am also Attaching this error when checking local storage after setting it the before test as mentioned above. I grabbed this screenshot in headed mode. As you can see even after setting it in the Before it does not appear to be setting local storage until after the component gets mounted. As far as the debug log I have most of it and even that is to long to post here. All the test is checking for is that the Local storage is set in the before

Cypress.on('test:before:run:async', async () => {
  await window.localStorage.setItem('cartItems', JSON.stringify(items))
});
iepoch commented 7 months ago

Still awaiting an answer

jennifer-shehane commented 7 months ago

This looks like a possible bug we'd have to investigate.

cypress-app-bot commented 1 month ago

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

iepoch commented 1 month ago

@jennifer-shehane This is still an issue in the latter versions. This was marked as needing investigation, but has it yet to be looked into?

jennifer-shehane commented 1 month ago

@iepoch This is not being prioritized at the moment.