Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.keys(localStorage) returns ['_store'] not the keys stored #1181

Closed
knownasilya opened this issue Dec 15, 2023 · 3 comments · Fixed by #1254
Closed

Object.keys(localStorage) returns ['_store'] not the keys stored #1181

knownasilya opened this issue Dec 15, 2023 · 3 comments · Fixed by #1254
Labels
bug Something isn't working

Comments

@knownasilya
Copy link

knownasilya commented Dec 15, 2023

Describe the bug
Object.keys(localStorage) returns ['_store'] not the keys stored
You have to do Object.keys(localStorage._store) to get the keys.
This also happens with for-in loop.

Using "happy-dom": "^12.10.3"

To Reproduce
Steps to reproduce the behavior:

  1. Set values on localStorage
  2. Run Object.keys(localStorage)
  3. See incorrect result

Expected behavior
Object.keys(localStorage) should return keys that were set.

Device:

  • OS: macos m1 latest version
@knownasilya knownasilya added the bug Something isn't working label Dec 15, 2023
@knownasilya
Copy link
Author

Workaround:

for (let i = 0; i < localStorage.length; i++) {
    const key = localStorage.key(i);
    // access each key
}

frankdiw pushed a commit to frankdiw/happy-dom that referenced this issue Feb 5, 2024
@XiaoDaiGua-Ray
Copy link

I encountered the same situation as you, after emulating the storage of a value, I used Object.keys(window.localStorage) to get an empty array.

image

capricorn86 added a commit to frankdiw/happy-dom that referenced this issue Mar 21, 2024
…nStorage as Storage is the correct class to use
capricorn86 added a commit to frankdiw/happy-dom that referenced this issue Mar 21, 2024
…nStorage as Storage is the correct class to use
capricorn86 added a commit that referenced this issue Mar 21, 2024
… by the properties Window.localStorage and Window.sessionStorage

* fix: [#1181] Refactor storage mock

* chore: [#1181] Removes the classes LocalStorage and SessionStorage as Storage is the correct class to use

* chore: [#1181] Removes the classes LocalStorage and SessionStorage as Storage is the correct class to use

---------

Co-authored-by: frankdiwang <frankdiwang@tencent.com>
Co-authored-by: David Ortner <david@ortner.se>
@capricorn86
Copy link
Owner

Than you for reporting @knownasilya! and @XiaoDaiGua-Ray! 🙂

A big thanks to @frankdiw for contributing with a fix 🌟

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v14.3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants