Skip to content

fix(container-runtime): Fix navigator assignment in Hardware Stats tests#26938

Open
ChumpChief wants to merge 2 commits intomicrosoft:mainfrom
ChumpChief:container-runtime-navigator-fix
Open

fix(container-runtime): Fix navigator assignment in Hardware Stats tests#26938
ChumpChief wants to merge 2 commits intomicrosoft:mainfrom
ChumpChief:container-runtime-navigator-fix

Conversation

@ChumpChief
Copy link
Copy Markdown
Contributor

Description

In Node 22+, globalThis.navigator is a read-only getter (Node added a built-in Navigator object). The Hardware Stats tests were assigning directly to global.navigator, which now throws TypeError: Cannot set property navigator of #<Object> which has only a getter.

Fix: use Object.defineProperty to override the property instead of direct assignment.

This is a prerequisite for upgrading the repo to Node 22 (#26934).

Reviewer Guidance

The review process is outlined on this wiki page.

In Node 22+, globalThis.navigator is a read-only getter. Use
Object.defineProperty to override it in tests instead of direct
assignment.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ChumpChief ChumpChief marked this pull request as ready for review April 3, 2026 18:23
@ChumpChief ChumpChief requested review from Copilot and markfields April 3, 2026 18:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates the Hardware Stats test helper to work on Node 22+ where globalThis.navigator is a read-only getter, enabling the repo’s Node 22 upgrade path.

Changes:

  • Replaces direct global.navigator = ... assignment with Object.defineProperty(globalThis, "navigator", ...) in tests.
  • Adds an explanatory comment about Node 22’s read-only navigator behavior.

Capture the original navigator property descriptor and restore it in
afterEach so the override doesn't leak into other test suites.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

function restoreNavigator() {
if (originalNavigatorDescriptor === undefined) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete -- this branch can be removed after upgrading to Node 22, which always provides a built-in navigator descriptor
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Got a work item?

Copy link
Copy Markdown

@shlevari shlevari left a comment

Choose a reason for hiding this comment

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

This code seems right for doing what you're doing, but, the fix feels a little weird to me. Like it just seems odd to add code to get around the fact that you can't set something because there is only a getter. Could you add more description to the PR on why this is necessary and the right fix and if there is any plan to remove it once the update happens?

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.

4 participants