Skip to content

Commit

Permalink
feat: display running processes, if vitest closes with timeout (#2633)
Browse files Browse the repository at this point in the history
* feat: display running processes, if vitest closes with timeout

* refactor: move hanging-process to reporter

* chore: cleanup
  • Loading branch information
sheremet-va committed Jan 11, 2023
1 parent 1ffb0ef commit 94968a6
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/config/index.md
Expand Up @@ -364,6 +364,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 @@ -553,8 +553,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.

0 comments on commit 94968a6

Please sign in to comment.