Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set mode before running Vite #775

Merged
merged 5 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/config/index.md
Expand Up @@ -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.

Expand Down Expand Up @@ -366,3 +366,10 @@ export default defineConfig({
- **Type:** `PrettyFormatOptions`

Format options for snapshot testing.

### mode

- **Type:** `string`
- **Default:** `test`

Overrides Vite mode.
3 changes: 2 additions & 1 deletion docs/guide/index.md
Expand Up @@ -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.

Expand Down Expand Up @@ -112,6 +112,7 @@ You can specify additional CLI options like `--port` or `--https`. For a full li
| `--outputFile <filename>` | 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 <env>` | Runner environment (default: node) |
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/cli.ts
Expand Up @@ -25,6 +25,7 @@ cli
.option('--outputFile <filename>', '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')
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/node/create.ts
Expand Up @@ -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),
}

Expand Down
3 changes: 0 additions & 3 deletions packages/vitest/src/node/plugins/index.ts
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/types/config.ts
Expand Up @@ -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<Required<UserConfig>, 'config' | 'filters' | 'coverage' | 'testNamePattern' | 'related' | 'api' | 'reporters'> {
Expand Down
5 changes: 5 additions & 0 deletions test/core/test/basic.test.ts
Expand Up @@ -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')
Expand Down