test: replace env operation with vi.stubEnv() - #45490
Draft
secustor wants to merge 4 commits into
Draft
Conversation
Enable Vitest's `unstubEnvs` so env stubs are reverted automatically
before every test, and convert the manual `process.env.X = ...` /
`delete process.env.X` pairs in specs to `vi.stubEnv()`.
This removes the hand-written `afterEach` cleanup blocks and the
`const OLD_ENV = process.env` / `process.env = { ...OLD_ENV }`
save-restore dance, which silently leaked state between tests when a
test set a variable without a matching delete.
Left as-is (incompatible with `unstubEnvs`, which unstubs before each
test, not after):
- module top-level env writes (cocoapods, gomod, post-update, pip-compile
and bundler artifacts specs)
- `beforeAll` env writes -- the two in `util/http` moved to `beforeEach`
Three specs asserted on values that the surrounding environment can override, so they only passed on a machine that happened not to set them: - `util/exec/env`: `getChildProcessEnv()` forwards every entry of `basicEnvVars`, but the spec only seeded a subset. Any other forwarded variable that was set ambiently (e.g. a lowercase `http_proxy`) leaked into the `toEqual` assertion. Clear all of them first. - `util/git/index`: `process.env.GIT_SSH_COMMAND` deliberately overrides the default that the two env-inheritance tests assert on. Clear it. - `logger/pretty-stdout`: the colorized level strings are built at import time, so the `beforeEach` setting `FORCE_COLOR` could never affect them -- it was dead code, and the test silently assumed a terminal without colour support. Derive the expectation the same way the module does.
The `determine source URL from nupkg` tests do not mock the filesystem,
so they really wrote downloaded packages into a fixed `/tmp/cache` path.
That fails wherever the system tmpdir is not writable, and two checkouts
running the suite at once would share the same directory.
Use `tmp.dir({ unsafeCleanup: true })` and clean it up afterwards, which
is the pattern the other specs that touch disk already use.
Enforces in specs what the migration to `vi.stubEnv` established: - assigning to or deleting `process.env.X` inside `beforeEach`, `afterEach`, `it` or `test` is reported, with a fixer that rewrites it to `vi.stubEnv()` - module scope, `describe` bodies and `beforeAll` / `afterAll` are left alone: `unstubEnvs` reverts stubs *before* each test, so a stub created there is gone by the time the first test runs, and those must keep assigning directly - replacing the `process.env` object is reported everywhere and never fixed. `vi.stubEnv()` captures the original object when the worker starts, so after a reassignment its deletes silently target the old object The rule found 28 further violations in specs the earlier sweep missed, since that one was anchored on `delete process.env`. Twelve were auto-fixed; the rest replaced `process.env` wholesale and are now `clearEnv()` from test/util. `util/exec/index.spec.ts` opts out with a comment: it asserts on the whole child env, and `vi.stubEnv()` cannot express that because it maps `PROD`, `DEV` and `SSR` onto '' instead of deleting them.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Changes
Replaces
process.envoperations in tests withvi.stubEnv()which allows us to set globally the resetting of these variablesContext
For now I have not added an additional oxlint rule though probably makes sense to add one.
Please select one of the following:
AI assistance disclosure
Did you use AI tools to create any part of this pull request?
Please select one option and, if yes, briefly describe how AI was used (e.g., code, tests, docs) and which tool(s) you used.
Use of AI in replying to PR comments
Who answers review comments:
Documentation (please check one with an [x])
How I've tested my work (please select one)
I have verified these changes via:
The public repository: