From e1735fb6797866188d2e9855966e33e0987cb008 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 14 Mar 2024 23:54:06 +0900 Subject: [PATCH] feat(vitest): support `clearScreen` cli flag (#5241) --- docs/guide/cli.md | 1 + packages/vitest/src/node/cli/cli-config.ts | 3 ++ packages/vitest/src/node/config.ts | 2 ++ packages/vitest/src/node/logger.ts | 4 +-- packages/vitest/src/types/config.ts | 5 +++ test/core/test/cli-test.test.ts | 40 ++++++++++++++++++++++ 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/docs/guide/cli.md b/docs/guide/cli.md index 825fd6e6e74a..424775c82dfc 100644 --- a/docs/guide/cli.md +++ b/docs/guide/cli.md @@ -103,6 +103,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim | `--exclude ` | 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`) | diff --git a/packages/vitest/src/node/cli/cli-config.ts b/packages/vitest/src/node/cli/cli-config.ts index c727d3a66276..afb1ec4dc17e 100644 --- a/packages/vitest/src/node/cli/cli-config.ts +++ b/packages/vitest/src/node/cli/cli-config.ts @@ -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, diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index 6d054f0a417e..662bb12b542f 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -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') diff --git a/packages/vitest/src/node/logger.ts b/packages/vitest/src/node/logger.ts index fa17be5ebad0..a494c9b619ee 100644 --- a/packages/vitest/src/node/logger.ts +++ b/packages/vitest/src/node/logger.ts @@ -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 } @@ -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 } diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index 5888f434be24..594ed36eae35 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -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, 'config' | 'filters' | 'browser' | 'coverage' | 'testNamePattern' | 'related' | 'api' | 'reporters' | 'resolveSnapshotPath' | 'benchmark' | 'shard' | 'cache' | 'sequence' | 'typecheck' | 'runner' | 'poolOptions' | 'pool' | 'cliExclude'> { diff --git a/test/core/test/cli-test.test.ts b/test/core/test/cli-test.test.ts index b40ff72f9efc..90f5123799d4 100644 --- a/test/core/test/cli-test.test.ts +++ b/test/core/test/cli-test.test.ts @@ -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() @@ -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: [],