From f80a6464954523ea5695e6b15969dba1ce304346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Wed, 22 Jan 2025 16:44:55 +0200 Subject: [PATCH] fix: don't toggle cli cursor on non-TTY --- packages/vitest/src/node/core.ts | 6 ------ packages/vitest/src/node/logger.ts | 9 +++++++-- test/reporters/tests/logger.test.ts | 26 ++++++++++++++++++++++++++ test/test-utils/index.ts | 6 ++++++ 4 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 test/reporters/tests/logger.test.ts diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 5b77a4412cb7..7e97d59b7b37 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -501,8 +501,6 @@ export class Vitest { } async collect(filters?: string[]): Promise { - this._onClose = [] - const files = await this.specifications.getRelevantTestSpecifications(filters) // if run with --changed, don't exit if no tests are found @@ -535,8 +533,6 @@ export class Vitest { * @param filters String filters to match the test files */ async start(filters?: string[]): Promise { - this._onClose = [] - try { await this.initCoverageProvider() await this.coverageProvider?.clean(this.config.coverage.clean) @@ -594,8 +590,6 @@ export class Vitest { * If the `--watch` flag is provided, Vitest will still run changed tests even if this method was not called. */ async init(): Promise { - this._onClose = [] - try { await this.initCoverageProvider() await this.coverageProvider?.clean(this.config.coverage.clean) diff --git a/packages/vitest/src/node/logger.ts b/packages/vitest/src/node/logger.ts index a00a5bad42de..b553e308a81b 100644 --- a/packages/vitest/src/node/logger.ts +++ b/packages/vitest/src/node/logger.ts @@ -50,7 +50,9 @@ export class Logger { this.addCleanupListeners() this.registerUnhandledRejection() - ;(this.outputStream as Writable).write(HIDE_CURSOR) + if ((this.outputStream as typeof process.stdout).isTTY) { + (this.outputStream as Writable).write(HIDE_CURSOR) + } } log(...args: any[]) { @@ -298,7 +300,10 @@ export class Logger { private addCleanupListeners() { const cleanup = () => { this.cleanupListeners.forEach(fn => fn()) - ;(this.outputStream as Writable).write(SHOW_CURSOR) + + if ((this.outputStream as typeof process.stdout).isTTY) { + (this.outputStream as Writable).write(SHOW_CURSOR) + } } const onExit = (signal?: string | number, exitCode?: number) => { diff --git a/test/reporters/tests/logger.test.ts b/test/reporters/tests/logger.test.ts new file mode 100644 index 000000000000..d4cca4e86e42 --- /dev/null +++ b/test/reporters/tests/logger.test.ts @@ -0,0 +1,26 @@ +import { expect, test } from 'vitest' +import { runVitest } from '../../test-utils' + +test('cursor is hidden during test run in TTY', async () => { + const { stdout } = await runVitest({ + include: ['b1.test.ts'], + root: 'fixtures/default', + reporters: 'none', + watch: false, + }, undefined, undefined, undefined, { tty: true, preserveAnsi: true }) + + expect(stdout).toContain('\x1B[?25l') + expect(stdout).toContain('\x1B[?25h') +}) + +test('cursor is not hidden during test run in non-TTY', async () => { + const { stdout } = await runVitest({ + include: ['b1.test.ts'], + root: 'fixtures/default', + reporters: 'none', + watch: false, + }, undefined, undefined, undefined, { preserveAnsi: true }) + + expect(stdout).not.toContain('\x1B[?25l') + expect(stdout).not.toContain('\x1B[?25h') +}) diff --git a/test/test-utils/index.ts b/test/test-utils/index.ts index 92bca066d713..3483d5127d1a 100644 --- a/test/test-utils/index.ts +++ b/test/test-utils/index.ts @@ -21,6 +21,7 @@ interface VitestRunnerCLIOptions { std?: 'inherit' fails?: boolean preserveAnsi?: boolean + tty?: boolean } export async function runVitest( @@ -46,6 +47,11 @@ export async function runVitest( callback() }, }) + + if (runnerOptions?.tty) { + (stdout as typeof process.stdout).isTTY = true + } + const stderr = new Writable({ write(chunk, __, callback) { if (runnerOptions.std === 'inherit') {