Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion packages/vitest/src/typecheck/typechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Vitest } from '../node/core'
import type { TestProject } from '../node/project'
import type { File, Task, TaskEventPack, TaskResultPack, TaskState } from '../runtime/runner/types'
import type { TscErrorInfo } from './types'
import { execFile } from 'node:child_process'
import os from 'node:os'
import { performance } from 'node:perf_hooks'
import { eachMapping, generatedPositionFor, TraceMap } from '@jridgewell/trace-mapping'
Expand Down Expand Up @@ -268,8 +269,11 @@ export class Typechecker {
}

public async stop(): Promise<void> {
this.process?.kill()
const child = this.process
this.process = undefined
if (child) {
await killProcessTree(child)
}
}

protected async ensurePackageInstalled(ctx: Vitest, checker: string): Promise<void> {
Expand Down Expand Up @@ -526,3 +530,24 @@ function findGeneratedPosition(
}
return { line: null, column: null }
}

function killProcessTree(child: ChildProcess): Promise<void> {
// on Windows the checker runs behind a `cmd.exe` shim, and `kill()` stops only the shim
if (process.platform !== 'win32' || child.pid == null || child.exitCode != null) {
child.kill()
return Promise.resolve()
}
return new Promise((resolve) => {
execFile(
'taskkill',
['/pid', String(child.pid), '/T', '/F'],
{ windowsHide: true },
(error) => {
if (error) {
child.kill()
}
resolve()
},
)
})
}
60 changes: 58 additions & 2 deletions test/typescript/test/typechecker.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { chmodSync, existsSync, readFileSync } from 'node:fs'
import { resolve } from 'pathe'
import { describe, expect, it } from 'vitest'
import { runVitest } from '../../test-utils'
import { describe, expect, it, onTestFinished } from 'vitest'
import { runVitest, ts, useTmpFS } from '../../test-utils'

describe('Typechecker', () => {
it('handles non-existing typechecker command gracefully', async () => {
Expand Down Expand Up @@ -40,4 +41,59 @@ describe('Typechecker', () => {
expect(stderr).toContain('before type checking finished')
expect(stderr).toContain('ran out of memory')
})

it('stops the watching checker when Vitest closes', async () => {
const fs = useTmpFS({
'fake-tsc.cmd': '@node "%~dp0fake-tsc.mjs" %*',
'fake-tsc.mjs': ts`#!/usr/bin/env node
import { writeFileSync } from 'node:fs'
writeFileSync('checker.pid', String(process.pid))
console.log('Found 0 errors. Watching for file changes.')
setInterval(() => {}, 1000)
`,
'basic.test-d.ts': ts`
import { expectTypeOf, test } from 'vitest'
test('number', () => {
expectTypeOf(1).toEqualTypeOf<number>()
})
`,
})
chmodSync(fs.resolveFile('fake-tsc.mjs'), 0o755)

const { ctx, stderr } = await runVitest({
root: fs.root,
watch: true,
typecheck: {
enabled: true,
only: true,
checker: fs.resolveFile(process.platform === 'win32' ? 'fake-tsc.cmd' : 'fake-tsc.mjs'),
},
})

const pidFile = fs.resolveFile('checker.pid')
await expect.poll(() => existsSync(pidFile)).toBe(true)
const pid = Number(readFileSync(pidFile, 'utf-8'))
onTestFinished(() => {
if (isRunning(pid)) {
process.kill(pid)
}
})
expect(isRunning(pid)).toBe(true)

await ctx!.close()

await expect.poll(() => isRunning(pid)).toBe(false)
expect(stderr).toBe('')
})
})

function isRunning(pid: number) {
try {
process.kill(pid, 0)
return true
} catch {
return false
}
}
Loading