-
Notifications
You must be signed in to change notification settings - Fork 660
Fix: Vitest, the "window.getComputedStyle" error is not defined when running tests #31987
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
base: 25_2
Are you sure you want to change the base?
Fix: Vitest, the "window.getComputedStyle" error is not defined when running tests #31987
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a Vitest compatibility issue where window.getComputedStyle errors are not handled gracefully in the readThemeMarker function. The fix adds a catch block to return null when getComputedStyle throws an error, preventing test failures in environments like Vitest where this method may not be fully implemented.
Key Changes:
- Added error handling to
readThemeMarker()function in themes.ts to catch and handlegetComputedStyleerrors - Added test coverage for the new error handling behavior
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/devextreme/js/__internal/ui/themes.ts | Added catch block to readThemeMarker() to gracefully handle getComputedStyle errors by returning null |
| packages/devextreme/testing/tests/DevExpress.ui/themes.tests.js | Added two new tests to verify error handling when getComputedStyle throws errors |
| } | ||
| return result.substr(THEME_MARKER_PREFIX.length); | ||
| } catch (e) { | ||
| return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I like this fix.
First, are we saying that it's normal that under vitest the window.getComputedStyles method is not mocked? Assuming we are in jsdom here, I can see it is in fact mocked:
https://github.com/jsdom/jsdom/blob/main/lib/jsdom/browser/Window.js#L908
and probably that's why this bug is not reproduced on macOS (and even on Parallels' Windows for ARM).
What's different on Windows x64?
And even if we think that it's too expensive to investigate this (do we?), okay, in this case we may want to support the case when there is no getComputedStyles in our environment. But is it a good way to do this with a blanket catch? Not checking for specific condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Let me investigate a little more, and come back
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Let me investigate a little more, and come back
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| if (!window?.getComputedStyle) { | ||
| return null; | ||
| } | ||
| result = window.getComputedStyle(element.get(0)).fontFamily; |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix only checks if window.getComputedStyle exists, but the tests expect the function to handle cases where getComputedStyle throws an error when called. The current implementation will still throw an error if getComputedStyle exists but throws when invoked. To properly handle this scenario, wrap the window.getComputedStyle call in a try-catch block within the existing try-finally structure.
| result = window.getComputedStyle(element.get(0)).fontFamily; | |
| let computedStyle; | |
| try { | |
| computedStyle = window.getComputedStyle(element.get(0)); | |
| } catch (e) { | |
| return null; | |
| } | |
| // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | |
| result = computedStyle && computedStyle.fontFamily; |
…ivanyandev/DevExtreme into fix/vitest-get-computed-style
No description provided.