Skip to content

[Tests] Make getPackageManager environment fallback test deterministic - #8601

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
tests-maintenance-35547738595
Draft

github-actions[bot] wants to merge 1 commit into
mainfrom
tests-maintenance-35547738595

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

WHY are these changes introduced?

getPackageManager walks up from a directory looking for a lockfile, and when it finds none it falls back to packageManagerFromUserAgent(), then to 'npm' if that returns 'unknown'.

The test named "tries to guess the package manager from the environment if it can't find a package.json" created an empty temp directory and asserted the result was 'pnpm', with the comment "pnpm is used locally and in CI". Nothing in the fixture produces 'pnpm' — the value came from the ambient npm_config_user_agent of whichever package manager happened to launch vitest. The test passes under pnpm test and fails under any other runner:

AssertionError: expected 'npm' to deeply equal 'pnpm'

Two problems follow from that. The suite is not runnable outside pnpm, and the assertion pinned an environment value rather than behavior — the unknown -> npm fallback the test appeared to cover was never executed, since the user agent always resolved to a known manager.

WHAT is this pull request doing?

Replaces that test with two that stub npm_config_user_agent explicitly instead of reading whatever the runner set:

  • an unrecognized user agent (some-other-tool/1.0.0), and
  • an empty user agent,

both asserting the 'npm' fallback. Each restores the environment in a finally block, matching the surrounding tests in the file, and neither depends on the ambient runner. Verified they pass under pnpm, npm, npm_config_user_agent=yarn/1.22.0, and an empty user agent, and that both fail if the unknown -> npm fallback is removed from getPackageManager.

No production code is changed.

How to manually test your changes?

CI

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes
  • I've considered analytics changes to measure impact
  • The change is user-facing — I've identified the correct bump type (patch for bug fixes · minor for new features · major for breaking changes) and added a changeset with pnpm changeset add

The test asserted `getPackageManager` returns 'pnpm' for a directory with no
package.json. Nothing in the fixture produces that result — it came from the
ambient `npm_config_user_agent` of whichever package manager launched vitest.
The test passes under `pnpm test` and fails under any other runner, and it
never exercised the `unknown -> npm` fallback it appeared to be covering.

Replace it with two tests that stub the user agent explicitly: one for an
unrecognized user agent, one for an empty one. Both assert the 'npm' fallback,
and both fail if that branch is removed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

0 participants