-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(typescript/vfs): don't read localStorage during feature detection in Node #3651
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
Open
C. Spencer Beggs (spencerbeggs)
wants to merge
2
commits into
microsoft:v2
Choose a base branch
from
spencerbeggs:fix/vfs-node26-localstorage-warning
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+76
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@typescript/vfs": patch | ||
| --- | ||
|
|
||
| Skip the localStorage feature detect in Node so importing the package no longer emits an ExperimentalWarning on Node 26 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * @jest-environment node | ||
| */ | ||
|
|
||
| // Regression coverage for the import-time `localStorage` feature detect. | ||
| // | ||
| // Node >= 26 exposes `localStorage` as a global accessor which emits an | ||
| // ExperimentalWarning when it is read - `typeof` included - unless the process was | ||
| // started with `--localstorage-file`. The probe therefore must not touch the global | ||
| // in a bare Node process. Installing a counting accessor lets us assert that | ||
| // directly, on whichever Node version CI happens to run. | ||
|
|
||
| const globals = globalThis as any | ||
|
|
||
| /** Defines `localStorage` as an accessor and reports how many times it was read. */ | ||
| const installLocalStorageAccessor = () => { | ||
| let reads = 0 | ||
| Object.defineProperty(globals, "localStorage", { | ||
| configurable: true, | ||
| get() { | ||
| reads++ | ||
| return { getItem: () => null, setItem: () => { }, removeItem: () => { } } | ||
| }, | ||
| }) | ||
| return () => reads | ||
| } | ||
|
|
||
| /** Evaluates the module fresh, so the import-time probe runs again. */ | ||
| const importModule = () => { | ||
| jest.resetModules() | ||
| require("../src") | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| delete globals.localStorage | ||
| delete globals.window | ||
| }) | ||
|
|
||
| it("does not read the localStorage global when evaluated in a bare Node process", () => { | ||
| const reads = installLocalStorageAccessor() | ||
|
|
||
| importModule() | ||
|
|
||
| expect(reads()).toBe(0) | ||
| }) | ||
|
|
||
| it("still reads the localStorage global when a DOM is present", () => { | ||
| globals.window = {} | ||
| const reads = installLocalStorageAccessor() | ||
|
|
||
| importModule() | ||
|
|
||
| expect(reads()).toBeGreaterThan(0) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Eh, that's overkill
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.
Copilot is nothing if not rigorous!
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.
Jake Bailey (@jakebailey) Want me to remove the test? Anything else I need to do?