Skip to content

Vitest 4 Broke Astro tests (import.meta.env is undefined) #8859

@teacher987

Description

@teacher987

Describe the bug

I already reported this on Astro project, but was advised to do it here instead:
withastro/astro#14626

The tests work with Vitest 3, but after upgrade, I'm getting

TypeError: Cannot read properties of undefined (reading 'DEV')

The line that throws the error is:

// node_modules/astro/dist/assets/runtime.js:12
if (import.meta.env.DEV) {
  ...
}

import.meta.env should be standard Vite stuff, but it seems it doesn't always exist.

Reproduction

https://stackblitz.com/edit/github-dmphmzqz?file=vitest.config.ts,src%2Fcomponents%2FWelcome.test.ts&on=stackblitz

Just try to run tests (npm test), and you'll encounter the error.

System Info

System:
    OS: macOS 15.6.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 295.80 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - /Users/x/.nvm/versions/node/v22.14.0/bin/node
    npm: 10.9.2 - /Users/x/.nvm/versions/node/v22.14.0/bin/npm
    Watchman: 2025.09.08.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 142.0.7444.60
    Edge: 141.0.3537.99
    Firefox: 139.0.4
    Safari: 18.6
  npmPackages:
    @vitest/coverage-v8: ^4.0.4 => 4.0.4 
    @vitest/ui: ^4.0.4 => 4.0.4 
    vitest: ^4.0.4 => 4.0.4

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p4-importantViolate documented behavior or significantly improves performance (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions