Skip to content

Commit

Permalink
fix: don't toggle cli cursor on non-TTY
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jan 24, 2025
1 parent c60ee27 commit f80a646
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
6 changes: 0 additions & 6 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,6 @@ export class Vitest {
}

async collect(filters?: string[]): Promise<TestRunResult> {
this._onClose = []

const files = await this.specifications.getRelevantTestSpecifications(filters)

// if run with --changed, don't exit if no tests are found
Expand Down Expand Up @@ -535,8 +533,6 @@ export class Vitest {
* @param filters String filters to match the test files
*/
async start(filters?: string[]): Promise<TestRunResult> {
this._onClose = []

try {
await this.initCoverageProvider()
await this.coverageProvider?.clean(this.config.coverage.clean)
Expand Down Expand Up @@ -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<void> {
this._onClose = []

try {
await this.initCoverageProvider()
await this.coverageProvider?.clean(this.config.coverage.clean)
Expand Down
9 changes: 7 additions & 2 deletions packages/vitest/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down Expand Up @@ -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) => {
Expand Down
26 changes: 26 additions & 0 deletions test/reporters/tests/logger.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
6 changes: 6 additions & 0 deletions test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface VitestRunnerCLIOptions {
std?: 'inherit'
fails?: boolean
preserveAnsi?: boolean
tty?: boolean
}

export async function runVitest(
Expand All @@ -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') {
Expand Down

0 comments on commit f80a646

Please sign in to comment.