Skip to content

Conversation

@hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Dec 8, 2025

Description

Resolves #9191

Problems

state.metaEnv = import.meta.env in browser tester.ts is transformed to { "MODE": "production", ...} due to vite build during @vitest/browser packaging. Later we mutate and override metaEnv with config.env, but this logic doesn't accommodate import.meta.env define and doesn't match with Vite dev behavior on browser mode. (On SSR, both import.meta.env define get merged to process.env, so defines works on runtime).

Solutions

I'm not sure this is the only way, but I replaced with a marker __vitest_browser_import_meta_env_init__ to ensure import.meta.env is processed by Vite (which happens during client import analysis).

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:.

@hi-ogawa hi-ogawa changed the title fix(browser): fix import.meta.env.* define fix(browser): fix import.meta.env define Dec 8, 2025
@hi-ogawa hi-ogawa marked this pull request as ready for review December 8, 2025 08:39
@sheremet-va sheremet-va merged commit 01a9a58 into vitest-dev:main Dec 8, 2025
14 of 16 checks passed
@hi-ogawa hi-ogawa deleted the 12-08-fix_browser_fix_import.meta.env._define branch December 8, 2025 08:55
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.

Prefixless env var with import.meta.env.* via define config not available

2 participants