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

fix: issue with localStorage in jsdom #18

Closed
wants to merge 1 commit into from

Conversation

Demivan
Copy link
Contributor

@Demivan Demivan commented Jan 28, 2022

No description provided.

@Demivan Demivan marked this pull request as draft January 28, 2022 09:13
@@ -0,0 +1,15 @@
/**
* @vitest-environment jsdom
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to create Window ourselves. Otherwise we depend on test framework implementation

We do this with happy-dom

https://github.com/Aslemammad/tinyspy/blob/c8446464e18fb6a7298a575fe8e7252f757e8ecd/test/index.test.ts#L546

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still trying to figure out what is causing this. It is only happening with jsdom and not happy-dom.
I'll probably not use localStorage at all when I figure out the cause.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still trying to figure out what is causing this. It is only happening with jsdom and not happy-dom. I'll probably not use localStorage at all when I figure out the cause.

This aproach should help narrow it down, since it is still possible the error might be in Vitest pipeline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsdom/jsdom#2318 - you cannot actually call defineProperty to change implementation on a localStorage.

You need to replace entire localStorage implementation if you need to mock it: jestjs/jest#6798 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest.spyOn(window.localStorage.__proto__, 'setItem'); should work without any additional imports.

Hehe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe just need to document it somewhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants