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

Error when adding a spy on localStorage #1377

Closed
sybers opened this issue Apr 3, 2024 · 2 comments · Fixed by #1385
Closed

Error when adding a spy on localStorage #1377

sybers opened this issue Apr 3, 2024 · 2 comments · Fixed by #1385
Assignees
Labels
bug Something isn't working

Comments

@sybers
Copy link

sybers commented Apr 3, 2024

Describe the bug
Hey,

I'm trying to add a spy on localstorage.setItem and I'm getting an error.

I'm using @happy-dom/jest-environment@14.3.9.

To Reproduce

If you add the code below to any tests:

jest.spyOn(localStorage, 'setItem').mockImplementationOnce(() => {
    throw new Error('error');
});

Jest will fail with the following error:

TypeError: 'deleteProperty' on proxy: trap returned falsish for property 'setItem'.

Please let me know if you need more information, Thanks !

PS: This could be related to #1368

@sybers sybers added the bug Something isn't working label Apr 3, 2024
@capricorn86 capricorn86 self-assigned this Apr 6, 2024
capricorn86 added a commit that referenced this issue Apr 6, 2024
…y-on-localstorage

fix: [#1377] Makes it possible to spy on Storage.prototype methods
@capricorn86
Copy link
Owner

Thank you for reporting @sybers! 🙂

I have fixed the error when deleting a property, but it seems like it will not be possible to spy on the instance methods with Jest (it works with Vitest), as Jest relies on Object.getOwnPropertyDescriptor(), which according to spec should return undefined for the Storage methods.

In the latest version of Happy DOM, I have made it possible to spy on Storage.prototype methods, which should solve your problem.

Example:

jest.spyOn(Storage.prototype, 'getItem').mockImplementationOnce(() => () => {
   throw new Error('error');
});

Read more about the release:
https://github.com/capricorn86/happy-dom/releases/tag/v14.6.2

@sybers
Copy link
Author

sybers commented Apr 14, 2024

Thanks alot for fixing this @capricorn86 !

In the latest version of Happy DOM, I have made it possible to spy on Storage.prototype methods, which should solve your problem.

That's how I used to to with Jest so everuthing good :)

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.

2 participants