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

feat(vitest): support clearScreen cli flag #5241

Merged
merged 9 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim
| `--exclude <glob>` | Additional file globs to be excluded from test |
| `--expand-snapshot-diff` | Show full diff when snapshot fails |
| `--disable-console-intercept` | Disable automatic interception of console logging (default: `false`) |
| `--clearScreen` | Clear terminal screen when re-running tests during watch mode (default: `true`) |
| `--typecheck [options]` | Custom options for typecheck pool. If passed without options, enables typechecking |
| `--typecheck.enabled` | Enable typechecking alongside tests (default: `false`) |
| `--typecheck.only` | Run only typecheck tests. This automatically enables typecheck (default: `false`) |
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ export const cliOptionsConfig: VitestCLIOptions = {
description: 'Removes colors from the console output',
alias: 'no-color',
},
clearScreen: {
description: 'Clear terminal screen when re-running tests during watch mode (default: true)',
},

// disable CLI options
cliExclude: null,
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export function resolveConfig(
if (viteConfig.base !== '/')
resolved.base = viteConfig.base

resolved.clearScreen = resolved.clearScreen ?? viteConfig.clearScreen ?? true

if (options.shard) {
if (resolved.watch)
throw new Error('You cannot use --shard option with enabled watch')
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class Logger {
}

clearFullScreen(message: string) {
if (this.ctx.server.config.clearScreen === false) {
if (!this.ctx.config.clearScreen) {
this.console.log(message)
return
}
Expand All @@ -63,7 +63,7 @@ export class Logger {
}

clearScreen(message: string, force = false) {
if (this.ctx.server.config.clearScreen === false) {
if (!this.ctx.config.clearScreen) {
this.console.log(message)
return
}
Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,11 @@ export interface UserConfig extends InlineConfig {
* Additional exclude patterns
*/
cliExclude?: string[]

/**
* Override vite config's clearScreen from cli
*/
clearScreen?: boolean
}

export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'filters' | 'browser' | 'coverage' | 'testNamePattern' | 'related' | 'api' | 'reporters' | 'resolveSnapshotPath' | 'benchmark' | 'shard' | 'cache' | 'sequence' | 'typecheck' | 'runner' | 'poolOptions' | 'pool' | 'cliExclude'> {
Expand Down
38 changes: 38 additions & 0 deletions test/core/test/cli-test.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect, test } from 'vitest'
import { resolveConfig as viteResolveConfig } from 'vite'
import { createCLI } from '../../../packages/vitest/src/node/cli/cac.js'
import { resolveConfig } from '../../../packages/vitest/src/node/config.js'

const vitestCli = createCLI()

Expand Down Expand Up @@ -253,3 +255,39 @@ test('browser by name', () => {
expect(args).toEqual([])
expect(options).toEqual({ browser: { enabled: true, name: 'firefox' } })
})

test('clearScreen', async () => {
const examples = [
// vitest cli | vite clearScreen
['--clearScreen', undefined],
['--clearScreen', true],
['--clearScreen', false],
['--no-clearScreen', undefined],
['--no-clearScreen', true],
['--no-clearScreen', false],
['', undefined],
['', true],
['', false],
] as const
const baseViteConfig = await viteResolveConfig({ configFile: false }, 'serve')
const results = examples.map(([vitestClearScreen, viteClearScreen]) => {
const config = resolveConfig('test', getCLIOptions(vitestClearScreen), {
...baseViteConfig,
clearScreen: viteClearScreen,
})
return config.clearScreen
})
expect(results).toMatchInlineSnapshot(`
[
true,
true,
true,
false,
false,
false,
true,
true,
false,
]
`)
})
Loading