From 5e36c1fdde5996f6508a115fefc404331813aaf4 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 17 Feb 2022 11:24:18 +0300 Subject: [PATCH 1/4] fix: set mode before running Vite --- docs/config/index.md | 7 +++++++ docs/guide/index.md | 1 + packages/vitest/src/node/cli.ts | 1 + packages/vitest/src/node/create.ts | 2 ++ packages/vitest/src/node/plugins/index.ts | 3 --- packages/vitest/src/types/config.ts | 6 ++++++ test/core/test/basic.test.ts | 5 +++++ 7 files changed, 22 insertions(+), 3 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 0b21ec128b13..64a3e5e3ff7f 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -364,3 +364,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..ee691f5ce44a 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -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 975c76b80d83..64420e2659de 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') 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 7f5e055b445c..f9fc7a77f143 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -28,9 +28,6 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest()) preOptions.api = resolveApiConfig(preOptions) 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 d245d7d69d45..7c42521f0282 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') From b0990d234c9a9ee78ac84881508e96a208dfd872 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 17 Feb 2022 11:41:09 +0300 Subject: [PATCH 2/4] chore: set NODE_ENV as mode from CLI --- packages/vitest/src/node/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 6586849374f0..60eff37df25f 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -77,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 From 2a05e577a9b21a255e05b57679cb357c9edeeaa4 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 17 Feb 2022 11:45:44 +0300 Subject: [PATCH 3/4] docs: you can use mode property on defineConfig to conditionally apply configuration --- docs/config/index.md | 2 +- docs/guide/index.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index de8c655784f8..02d14e8f1be0 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` 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. diff --git a/docs/guide/index.md b/docs/guide/index.md index ee691f5ce44a..7f911140c095 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` 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. From 193adb43b4cfe4bca2dca8549a6cc99ea5f20d96 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 17 Feb 2022 11:48:18 +0300 Subject: [PATCH 4/4] docs: more additional info --- docs/config/index.md | 2 +- docs/guide/index.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 02d14e8f1be0..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` or `mode` property on `defineConfig` 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. diff --git a/docs/guide/index.md b/docs/guide/index.md index 7f911140c095..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` or `mode` property on `defineConfig` 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.