Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: display running processes, if vitest closes with timeout #2633

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/config/index.md
Expand Up @@ -358,6 +358,7 @@ Custom reporters for output. Reporters can be [a Reporter instance](https://gith
- `'junit'` - JUnit XML reporter (you can configure `testsuites` tag name with `VITEST_JUNIT_SUITE_NAME` environmental variable)
- `'json'` - give a simple JSON summary
- `'html'` - outputs HTML report based on [`@vitest/ui`](/guide/ui)
- `'hanging-process'` - displays a list of hanging processes, if Vitest cannot exit process safely. This might be a heavy operation, enable it only if Vitest consistently cannot exit process
- path of a custom reporter (e.g. `'./path/to/reporter.ts'`, `'@scope/reporter'`)

### outputTruncateLength
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/package.json
Expand Up @@ -122,7 +122,8 @@
"tinypool": "^0.3.0",
"tinyspy": "^1.0.2",
"vite": "^3.0.0 || ^4.0.0",
"vite-node": "workspace:*"
"vite-node": "workspace:*",
"why-is-node-running": "^2.2.2"
},
"devDependencies": {
"@antfu/install-pkg": "^0.1.1",
Expand Down
6 changes: 4 additions & 2 deletions packages/vitest/src/node/core.ts
Expand Up @@ -548,8 +548,10 @@ export class Vitest {
*/
async exit(force = false) {
setTimeout(() => {
console.warn(`close timed out after ${this.config.teardownTimeout}ms`)
process.exit()
this.report('onProcessTimeout').then(() => {
console.warn(`close timed out after ${this.config.teardownTimeout}ms`)
process.exit()
})
}, this.config.teardownTimeout).unref()

await this.close()
Expand Down
15 changes: 15 additions & 0 deletions packages/vitest/src/node/reporters/hanging-process.ts
@@ -0,0 +1,15 @@
import { createRequire } from 'module'
import type { Reporter } from '../../types'

export class HangingProcessReporter implements Reporter {
whyRunning: (() => void) | undefined

onInit(): void {
const _require = createRequire(import.meta.url)
this.whyRunning = _require('why-is-node-running')
}

onProcessTimeout() {
this.whyRunning?.()
}
}
2 changes: 2 additions & 0 deletions packages/vitest/src/node/reporters/index.ts
Expand Up @@ -5,6 +5,7 @@ import { VerboseReporter } from './verbose'
import { TapReporter } from './tap'
import { JUnitReporter } from './junit'
import { TapFlatReporter } from './tap-flat'
import { HangingProcessReporter } from './hanging-process'

export { DefaultReporter }

Expand All @@ -16,6 +17,7 @@ export const ReportersMap = {
'tap': TapReporter,
'tap-flat': TapFlatReporter,
'junit': JUnitReporter,
'hanging-process': HangingProcessReporter,
}

export type BuiltinReporters = keyof typeof ReportersMap
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/types/reporter.ts
Expand Up @@ -16,6 +16,8 @@ export interface Reporter {
onServerRestart?: (reason?: string) => Awaitable<void>

onUserConsoleLog?: (log: UserConsoleLog) => Awaitable<void>

onProcessTimeout?: () => Awaitable<void>
}

export type { Vitest }
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

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