happy-dom icon indicating copy to clipboard operation
happy-dom copied to clipboard

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

Open knownasilya opened this issue 1 year ago • 1 comments

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 avatar Dec 15 '23 21:12 knownasilya

Workaround:

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

knownasilya avatar Dec 20 '23 14:12 knownasilya

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

XiaoDaiGua-Ray avatar Mar 21 '24 12:03 XiaoDaiGua-Ray

Than you for reporting @knownasilya! and @XiaoDaiGua-Ray! :slightly_smiling_face:

A big thanks to @frankdiw for contributing with a fix :star2:

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

capricorn86 avatar Mar 21 '24 23:03 capricorn86