Skip to content

env.mode is not set to benchmark when running vitest bench #4398

@adriencaccia

Description

@adriencaccia

Describe the bug

When running vitest bench, the env.mode variable is set to test, where we expect it is set to benchmark as explained in the docs:

Use process.env.VITEST or mode property on defineConfig (will be set to test/benchmark if not overridden) to conditionally apply different configuration in vite.config.ts

Reproduction

Running vitest bench with the following vitest.config.ts will log mode: test

import { defineConfig } from 'vitest/config'

export default defineConfig((env) => {
  console.log('mode:', env.mode)
  // ...
})

I found two possible culprits, the first one being

// this will make "mode" = "test" inside defineConfig
mode: options.mode || process.env.NODE_ENV || mode,

I think mode should be put ahead of process.env.NODE_ENV, since it is the computed mode. This is the change I made in the linked PR.

Another possible way of fixing it would be to change

process.env.NODE_ENV ??= options.mode || 'test'

And use process.env.NODE_ENV ??= options.mode || mode || 'test' instead. However I do not think that setting NODE_ENV to benchmark is such a good idea, as where are in a test environment, and more often that not users will expect their benchmark code to be executed with NODE_ENV=test.

System Info

System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M1 Pro
    Memory: 63.02 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/Library/Caches/fnm_multishells/74906_1698658724643/bin/node
    npm: 9.5.0 - ~/Library/Caches/fnm_multishells/74906_1698658724643/bin/npm
  Browsers:
    Chrome: 118.0.5993.117
    Firefox: 119.0
    Safari: 17.0
  npmPackages:
    vite: latest => 4.5.0 
    vitest: workspace:* => 1.0.0-beta.3

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions