Skip to content

Commit

Permalink
fix: run mode stuck in TTY terminals (#3690)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Jun 28, 2023
1 parent 0d5ec0f commit 141a86a
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/vitest/src/node/cli-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ export async function startVitest(
return ctx
}

let stdinCleanup
if (process.stdin.isTTY)
registerConsoleShortcuts(ctx)
stdinCleanup = registerConsoleShortcuts(ctx)

ctx.onServerRestart((reason) => {
ctx.report('onServerRestart', reason)
Expand All @@ -115,6 +116,7 @@ export async function startVitest(
if (ctx.shouldKeepServer())
return ctx

stdinCleanup?.()
await ctx.close()
return ctx
}
4 changes: 4 additions & 0 deletions packages/vitest/src/node/stdin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,8 @@ export function registerConsoleShortcuts(ctx: Vitest) {
}

on()

return function cleanup() {
off()
}
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/coverage-test/testing.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ for (const threads of threadsConfig) {
exit()
}
else if (process.exitCode) {
process.exitCode = null
console.warn(`Browser tests failed, retrying ${1 + retry}/${retries.length - 1}...`)
}
else {
Expand Down
7 changes: 7 additions & 0 deletions test/run/fixtures/example.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'

import { getHelloWorld } from './example'

test('getHello', async () => {
expect(getHelloWorld()).toBe('Hello world')
})
3 changes: 3 additions & 0 deletions test/run/fixtures/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getHelloWorld() {
return 'Hello world'
}
7 changes: 7 additions & 0 deletions test/run/fixtures/math.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'

import { sum } from './math'

test('sum', () => {
expect(sum(1, 2)).toBe(3)
})
3 changes: 3 additions & 0 deletions test/run/fixtures/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sum(a: number, b: number) {
return a + b
}
13 changes: 13 additions & 0 deletions test/run/fixtures/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'vitest/config'

// Patch stdin on the process so that we can fake it to seem like a real interactive terminal and pass the TTY checks
process.stdin.isTTY = true
process.stdin.setRawMode = () => process.stdin

export default defineConfig({
test: {
watch: false,
reporters: 'verbose',
teardownTimeout: 5_000,
},
})
11 changes: 11 additions & 0 deletions test/run/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@vitest/test-run",
"private": true,
"scripts": {
"test": "vitest"
},
"devDependencies": {
"vite": "latest",
"vitest": "workspace:*"
}
}
15 changes: 15 additions & 0 deletions test/run/test/tty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'

test('run mode does not get stuck when TTY', async () => {
const vitest = await runVitestCli('--root', 'fixtures')
await vitest.isDone

expect(vitest.stdout).toContain('✓ example.test.ts')
expect(vitest.stdout).toContain('✓ math.test.ts')
expect(vitest.stdout).toContain('2 passed')

// Regression #3642
expect(vitest.stderr).not.toContain('close timed out')
expect(vitest.stderr).toBe('')
})
12 changes: 12 additions & 0 deletions test/run/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
reporters: 'verbose',
include: ['test/**/*.test.*'],
chaiConfig: {
truncateThreshold: 0,
},
testTimeout: process.env.CI ? 30_000 : 10_000,
},
})

0 comments on commit 141a86a

Please sign in to comment.