Skip to content

fix!: allow mutating happy-dom/jsdom window object - #10373

Merged
sheremet-va merged 10 commits into
vitest-dev:mainfrom
hi-ogawa:fix-allow-mutating-happy-dom-jsdom-window-object
Jun 4, 2026
Merged

sheremet-va merged 10 commits into
vitest-dev:mainfrom
hi-ogawa:fix-allow-mutating-happy-dom-jsdom-window-object

Conversation

@hi-ogawa

@hi-ogawa hi-ogawa commented May 18, 2026

Copy link
Copy Markdown
Collaborator

Description

Like in the issue above, dom implementation supports mutating some global window property, but they weren't propagated through jsdom/happy-dom global wrapper of Vitest. This changes so it writes out to the dom window object.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify

netlify Bot commented May 18, 2026

Copy link
Copy Markdown

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit ef25cfc
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a1f74146954ce000882236c
😎 Deploy Preview https://deploy-preview-10373--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@sheremet-va

Copy link
Copy Markdown
Member

Cannot find many issues with this approach, to be honest

@hi-ogawa hi-ogawa changed the title fix: allow mutating happy-dom/jsdom window object fix!: allow mutating happy-dom/jsdom window object May 29, 2026
Comment on lines +81 to +89
// propagate changes to underlying window implementation,
// which can affect other window API behavior internally, e.g.
// updating `innerWidth` affects `matchMedia("(max-width: *)")` on happy-dom.
try {
win[key] = v
}
catch {
// haven't found a case it throws but just in case
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Discussed that we can just remove try/catch to surface if there's any setter error.

@hi-ogawa hi-ogawa self-assigned this May 29, 2026
Comment on lines +50 to +66
test('readonly window assignment throws', ({ task }) => {
// happy-dom's vmThreads setup returns Window as a Node VM context directly.
// Node contextification reports this getter-only assignment as successful,
// unlike the populateGlobal facade used by threads/forks.
if (task.file.pool === 'vmThreads') {
expect(() => {
Object.assign(window, { navigator: {} })
}).not.toThrow()
return
}

expect(() => {
Object.assign(window, { navigator: {} })
}).toThrowErrorMatchingInlineSnapshot(
`[TypeError: Cannot set property navigator of #<GlobalWindow> which has only a getter]`,
)
})

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

There's vm annoyance. Jsdom seems to mitigate this as they provide dedicated API for vm use JSDOM.getInternalVMContext, but happy-dom doesn't have the same.

This minor difference sounds fine as just an edge case. Note that, window.innerWidth = 50 behavior still works fine.

@sheremet-va sheremet-va left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this might also need a mention in the migration guide

@hi-ogawa
hi-ogawa marked this pull request as ready for review June 3, 2026 00:23
@hi-ogawa
hi-ogawa requested a review from sheremet-va June 3, 2026 00:38
@hi-ogawa hi-ogawa removed their assignment Jun 4, 2026
@sheremet-va
sheremet-va merged commit 206e8cf into vitest-dev:main Jun 4, 2026
16 of 18 checks passed
@hi-ogawa
hi-ogawa deleted the fix-allow-mutating-happy-dom-jsdom-window-object branch June 4, 2026 07:11
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting window.innerWidth doesn't actually set innerWidth on happy-dom's window object

2 participants