diff --git a/docs/config/index.md b/docs/config/index.md index 467717cb17e4..5a76f88d38e0 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -6,7 +6,7 @@ - Create `vitest.config.ts`, which will have the higher priority - Pass `--config` option to CLI, e.g. `vitest --config ./path/to/vitest.config.ts` -- Use `process.env.VITEST` to conditionally apply different configuration in `vite.config.ts` +- Use `process.env.VITEST` or `mode` property on `defineConfig` (will be set to `test` if not overridden) to conditionally apply different configuration in `vite.config.ts` To configure `vitest` itself, add `test` property in your Vite config. You'll also need to add a reference to Vitest types using a [triple slash command](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) at the top of your config file. @@ -366,3 +366,10 @@ export default defineConfig({ - **Type:** `PrettyFormatOptions` Format options for snapshot testing. + +### mode + +- **Type:** `string` +- **Default:** `test` + +Overrides Vite mode. \ No newline at end of file diff --git a/docs/guide/index.md b/docs/guide/index.md index 54d478d00511..a6c1a02dcbe7 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -35,7 +35,7 @@ One of the main advantages of Vitest is its unified configuration with Vite. If - Create `vitest.config.ts`, which will have the higher priority - Pass `--config` option to CLI, e.g. `vitest --config ./path/to/vitest.config.ts` -- Use `process.env.VITEST` to conditionally apply different configuration in `vite.config.ts` +- Use `process.env.VITEST` or `mode` property on `defineConfig` (will be set to `test` if not overridden) to conditionally apply different configuration in `vite.config.ts` To configure `vitest` itself, add `test` property in your Vite config. You'll also need to add a reference to Vitest types using a [triple slash command](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) at the top of your config file. @@ -112,6 +112,7 @@ You can specify additional CLI options like `--port` or `--https`. For a full li | `--outputFile ` | Write test results to a file when the `--reporter=json` option is also specified | | `--coverage` | Use c8 for coverage | | `--run` | Do not watch | +| `--mode` | Override Vite mode (default: `test`) | | `--global` | Inject APIs globally | | `--dom` | Mock browser api with happy-dom | | `--environment ` | Runner environment (default: node) | diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 1cce695ef32a..60eff37df25f 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -25,6 +25,7 @@ cli .option('--outputFile ', 'write test results to a file when the --reporter=json option is also specified') .option('--coverage', 'use c8 for coverage') .option('--run', 'do not watch') + .option('--mode', 'override Vite mode (default: test)') .option('--globals', 'inject apis globally') .option('--global', 'deprecated, use --globals') .option('--dom', 'mock browser api with happy-dom') @@ -76,7 +77,7 @@ async function run(cliFilters: string[], options: CliOptions) { async function start(cliFilters: string[], options: CliOptions) { process.env.TEST = 'true' process.env.VITEST = 'true' - process.env.NODE_ENV ??= 'test' + process.env.NODE_ENV ??= options.mode || 'test' if (options.run) options.watch = false diff --git a/packages/vitest/src/node/create.ts b/packages/vitest/src/node/create.ts index 1df9b7f62129..f8adeded7ed8 100644 --- a/packages/vitest/src/node/create.ts +++ b/packages/vitest/src/node/create.ts @@ -19,6 +19,8 @@ export async function createVitest(options: UserConfig, viteOverrides: ViteUserC root, logLevel: 'error', configFile: configPath, + // this will make "mode" = "test" inside defineConfig + mode: options.mode || process.env.NODE_ENV || 'test', plugins: await VitestPlugin(options, ctx), } diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index f9be67b3ca70..3d7dddf523b3 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -60,9 +60,6 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest()) (options as ResolvedConfig).defines = defines return { - // we are setting NODE_ENV when running CLI to 'test', - // but it can be overridden - mode: viteConfig.mode || process.env.NODE_ENV || 'test', clearScreen: false, resolve: { // by default Vite resolves `module` field, which not always a native ESM module diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index 61e0dcee63c2..acc142f4a885 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -309,6 +309,12 @@ export interface UserConfig extends InlineConfig { * Run tests that cover a list of source files */ related?: string[] | string + + /** + * Overrides Vite mode + * @default 'test' + */ + mode?: string } export interface ResolvedConfig extends Omit, 'config' | 'filters' | 'coverage' | 'testNamePattern' | 'related' | 'api' | 'reporters'> { diff --git a/test/core/test/basic.test.ts b/test/core/test/basic.test.ts index dad3e931880c..59662e7446a9 100644 --- a/test/core/test/basic.test.ts +++ b/test/core/test/basic.test.ts @@ -25,6 +25,11 @@ test('JSON', () => { assert.deepEqual(JSON.parse(output), input, 'matches original') }) +test('mode and NODE_ENV is test by default', () => { + expect(process.env.NODE_ENV).toBe('test') + expect(import.meta.env.MODE).toBe('test') +}) + test('assertion is callable', () => { const str = '13' expect(str).to.be.a('string')