Skip to content

Commit 8389156

Browse files
committed
fix: don't toggle cli cursor on non-TTY
1 parent c60ee27 commit 8389156

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

packages/vitest/src/node/core.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,6 @@ export class Vitest {
501501
}
502502

503503
async collect(filters?: string[]): Promise<TestRunResult> {
504-
this._onClose = []
505-
506504
const files = await this.specifications.getRelevantTestSpecifications(filters)
507505

508506
// if run with --changed, don't exit if no tests are found
@@ -535,8 +533,6 @@ export class Vitest {
535533
* @param filters String filters to match the test files
536534
*/
537535
async start(filters?: string[]): Promise<TestRunResult> {
538-
this._onClose = []
539-
540536
try {
541537
await this.initCoverageProvider()
542538
await this.coverageProvider?.clean(this.config.coverage.clean)
@@ -594,8 +590,6 @@ export class Vitest {
594590
* If the `--watch` flag is provided, Vitest will still run changed tests even if this method was not called.
595591
*/
596592
async init(): Promise<void> {
597-
this._onClose = []
598-
599593
try {
600594
await this.initCoverageProvider()
601595
await this.coverageProvider?.clean(this.config.coverage.clean)

packages/vitest/src/node/logger.ts

Lines changed: 7 additions & 2 deletions
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) => {
Lines changed: 26 additions & 0 deletions
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, { std: 'tty', 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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Cli } from './cli'
1818
Object.assign(tinyrainbow.default, tinyrainbow.getDefaultColors())
1919

2020
interface VitestRunnerCLIOptions {
21-
std?: 'inherit'
21+
std?: 'inherit' | 'tty'
2222
fails?: boolean
2323
preserveAnsi?: boolean
2424
}
@@ -46,6 +46,12 @@ export async function runVitest(
4646
callback()
4747
},
4848
})
49+
50+
if (runnerOptions?.std === 'tty') {
51+
// @ts-expect-error -- trick
52+
stdout.isTTY = true
53+
}
54+
4955
const stderr = new Writable({
5056
write(chunk, __, callback) {
5157
if (runnerOptions.std === 'inherit') {

0 commit comments

Comments
 (0)