Skip to content

test.env config not working in workspace project #5016

@hi-ogawa

Description

@hi-ogawa

Describe the bug

The issue is originally reported in the discussion #5003 (I initially suspected mis-usage of loadEnv with process.cwd, but it turned out to be irrelevant).

The reproduction is here https://stackblitz.com/edit/github-2srz39?file=packages%2Flib1%2Fvite.config.ts

Given following configurations:

//// vitest.workspace.ts
import { defineWorkspace } from 'vitest/config'

export default defineWorkspace([
  'packages/*',
])


//// packages/lib1/vite.config.ts
import { defineProject } from 'vitest/config';

export default defineProject({
  test: {
    env: {
      SOME_ENV: 'hello',   // <-- "env" is not applied per project
    },
  },
  define: {
    SOME_DEFINE: '"one"',  // <-- "define" is applied per project
  },
});

Running tests by npm -C packages/lib1 test succeeds but npm test fails.

One simple workaround I thought of is to use setupFiles and do this Object.assign(process.env, { ... your env ... }) manually.


It looks like currently only the root test.env config is eventually passed to new Tinypool constructor (and shared between all thread/fork instances) but per-project test.env config seems to be not used.

const options: PoolProcessOptions = {
execArgv: [
...execArgv,
...conditions,
],
env: {
TEST: 'true',
VITEST: 'true',
NODE_ENV: process.env.NODE_ENV || 'test',
VITEST_MODE: ctx.config.watch ? 'WATCH' : 'RUN',
...process.env,
...ctx.config.env,
},
}


Also btw, I noticed env config is not currently documented in https://vitest.dev/config/ but I also somehow knew this config (and actually recommended in remix vite doc https://remix.run/docs/en/main/future/vite#plugin-usage-with-other-vite-based-tools-eg-vitest-storybook). At least this is available from defineProject typing, so I suppose this feature is meant to work for project level as well.

Reproduction

https://stackblitz.com/edit/github-2srz39?file=packages%2Flib1%2Fvite.config.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.14.0 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 1.2.1 
    vite: latest => 5.0.11 
    vitest: latest => 1.2.1

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions