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

Mocking matchMedia #103

Closed
wtchnm opened this issue Dec 12, 2021 · 3 comments
Closed

Mocking matchMedia #103

wtchnm opened this issue Dec 12, 2021 · 3 comments

Comments

@wtchnm
Copy link
Contributor

wtchnm commented Dec 12, 2021

Currently I think that there is no way to mock some window methods like matchMedia because of the way that the jsdom env works: https://github.com/antfu-sponsors/vitest/blob/67fecfbf81f5e4be75e973e3655a3ae351a2afb7/src/env/jsdom.ts#L23
It would be great if we could do something like this: https://github.com/wtchnm/vitest-example/blob/194c4f71f8f07f33bb464d5e78be056aecc7c07b/src/__tests__/App.tsx#L5

@wtchnm
Copy link
Contributor Author

wtchnm commented Dec 12, 2021

Got it working using Object.defineProperty(global, 'matchMedia', { but it's strange that global and window are different objects.

@antfu antfu closed this as completed in d4f4c93 Dec 13, 2021
@wtchnm
Copy link
Contributor Author

wtchnm commented Dec 13, 2021

Thanks @antfu!

@wtchnm
Copy link
Contributor Author

wtchnm commented Dec 13, 2021

Unfortunately, it's not working yet, but I'll open a PR to address the issue.

@wtchnm wtchnm reopened this Dec 13, 2021
@antfu antfu closed this as completed Dec 13, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jun 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants