Skip to content

Commit

Permalink
feat(vitest): support clearScreen cli flag (#5241)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Mar 14, 2024
1 parent 356db87 commit e1735fb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
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 @@ -580,6 +580,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 @@ -809,6 +809,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
40 changes: 40 additions & 0 deletions test/core/test/cli-test.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { expect, test } from 'vitest'
import { resolveConfig as viteResolveConfig } from 'vite'
import { resolveConfig } from '../../../packages/vitest/src/node/config.js'
import { createCLI, parseCLI } from '../../../packages/vitest/src/node/cli/cac.js'

const vitestCli = createCLI()
Expand Down Expand Up @@ -261,6 +263,44 @@ test('browser by name', () => {
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 viteConfig = {
...baseViteConfig,
clearScreen: viteClearScreen,
}
const vitestConfig = getCLIOptions(vitestClearScreen)
const config = resolveConfig('test', vitestConfig, viteConfig)
return config.clearScreen
})
expect(results).toMatchInlineSnapshot(`
[
true,
true,
true,
false,
false,
false,
true,
true,
false,
]
`)
})

test('public parseCLI works correctly', () => {
expect(parseCLI('vitest dev')).toEqual({
filter: [],
Expand Down

0 comments on commit e1735fb

Please sign in to comment.