Skip to content

Commit 3c8050e

Browse files
authoredFeb 3, 2025··
fix: don't toggle cli cursor on non-TTY (#7336)
1 parent 926ca95 commit 3c8050e

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed
 

‎packages/vitest/src/node/core.ts

-6
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,6 @@ export class Vitest {
509509
}
510510

511511
async collect(filters?: string[]): Promise<TestRunResult> {
512-
this._onClose = []
513-
514512
const files = await this.specifications.getRelevantTestSpecifications(filters)
515513

516514
// if run with --changed, don't exit if no tests are found
@@ -543,8 +541,6 @@ export class Vitest {
543541
* @param filters String filters to match the test files
544542
*/
545543
async start(filters?: string[]): Promise<TestRunResult> {
546-
this._onClose = []
547-
548544
try {
549545
await this.initCoverageProvider()
550546
await this.coverageProvider?.clean(this.config.coverage.clean)
@@ -602,8 +598,6 @@ export class Vitest {
602598
* If the `--watch` flag is provided, Vitest will still run changed tests even if this method was not called.
603599
*/
604600
async init(): Promise<void> {
605-
this._onClose = []
606-
607601
try {
608602
await this.initCoverageProvider()
609603
await this.coverageProvider?.clean(this.config.coverage.clean)

‎packages/vitest/src/node/logger.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export class Logger {
5050
this.addCleanupListeners()
5151
this.registerUnhandledRejection()
5252

53-
;(this.outputStream as Writable).write(HIDE_CURSOR)
53+
if ((this.outputStream as typeof process.stdout).isTTY) {
54+
(this.outputStream as Writable).write(HIDE_CURSOR)
55+
}
5456
}
5557

5658
log(...args: any[]) {
@@ -298,7 +300,10 @@ export class Logger {
298300
private addCleanupListeners() {
299301
const cleanup = () => {
300302
this.cleanupListeners.forEach(fn => fn())
301-
;(this.outputStream as Writable).write(SHOW_CURSOR)
303+
304+
if ((this.outputStream as typeof process.stdout).isTTY) {
305+
(this.outputStream as Writable).write(SHOW_CURSOR)
306+
}
302307
}
303308

304309
const onExit = (signal?: string | number, exitCode?: number) => {

‎test/reporters/tests/logger.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expect, test } from 'vitest'
2+
import { runVitest } from '../../test-utils'
3+
4+
test('cursor is hidden during test run in TTY', async () => {
5+
const { stdout } = await runVitest({
6+
include: ['b1.test.ts'],
7+
root: 'fixtures/default',
8+
reporters: 'none',
9+
watch: false,
10+
}, undefined, undefined, undefined, { tty: true, preserveAnsi: true })
11+
12+
expect(stdout).toContain('\x1B[?25l')
13+
expect(stdout).toContain('\x1B[?25h')
14+
})
15+
16+
test('cursor is not hidden during test run in non-TTY', async () => {
17+
const { stdout } = await runVitest({
18+
include: ['b1.test.ts'],
19+
root: 'fixtures/default',
20+
reporters: 'none',
21+
watch: false,
22+
}, undefined, undefined, undefined, { preserveAnsi: true })
23+
24+
expect(stdout).not.toContain('\x1B[?25l')
25+
expect(stdout).not.toContain('\x1B[?25h')
26+
})

‎test/test-utils/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface VitestRunnerCLIOptions {
2222
std?: 'inherit'
2323
fails?: boolean
2424
preserveAnsi?: boolean
25+
tty?: boolean
2526
}
2627

2728
export async function runVitest(
@@ -47,6 +48,11 @@ export async function runVitest(
4748
callback()
4849
},
4950
})
51+
52+
if (runnerOptions?.tty) {
53+
(stdout as typeof process.stdout).isTTY = true
54+
}
55+
5056
const stderr = new Writable({
5157
write(chunk, __, callback) {
5258
if (runnerOptions.std === 'inherit') {

0 commit comments

Comments
 (0)
Please sign in to comment.