Skip to content

Commit

Permalink
fix: fix node crash under Windows (fix #582) (#588)
Browse files Browse the repository at this point in the history
* fix: fix node crash under Windows

* chore: use exit code 1 if cli did not exit in time

* Revert "chore: use exit code 1 if cli did not exit in time"

This reverts commit c063bac.
  • Loading branch information
Demivan committed Jan 19, 2022
1 parent 456a296 commit 86b7e03
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 33 deletions.
5 changes: 1 addition & 4 deletions packages/vitest/src/node/cli.ts
Expand Up @@ -112,9 +112,6 @@ async function run(cliFilters: string[], options: UserConfig) {
}
finally {
if (!ctx.config.watch)
await ctx.close()
await ctx.exit()
}

if (!ctx.config.watch)
await ctx.exit()
}
35 changes: 6 additions & 29 deletions packages/vitest/src/node/core.ts
Expand Up @@ -351,35 +351,12 @@ export class Vitest {
}

async exit() {
let timeout: NodeJS.Timeout
let resolveClose: () => void
let resolveTimeout: () => void
const closePromise = new Promise((resolve, reject) => {
resolveClose = () => resolve(true)
this.close().then(resolve, reject)
})
// make sure all promises are resolved before exiting process
// https://github.com/nodejs/node/blob/master/src/node_file-inl.h#L160
const cleanup = () => {
clearTimeout(timeout)
resolveTimeout?.()
resolveClose?.()
}
const timeoutPromise = new Promise((resolve, reject) => {
resolveTimeout = () => resolve(true)
timeout = setTimeout(() => reject(new Error(`close timed out after ${CLOSE_TIMEOUT}ms`)), CLOSE_TIMEOUT).unref()
})
Promise.race([closePromise, timeoutPromise]).then(
() => {
cleanup()
process.exit()
},
(err) => {
cleanup()
console.error('error during close', err)
process.exit(1)
},
)
setTimeout(() => {
console.warn(`close timed out after ${CLOSE_TIMEOUT}ms`)
process.exit()
}, CLOSE_TIMEOUT).unref()

await this.close()
}

async report<T extends keyof Reporter>(name: T, ...args: ArgumentsType<Reporter[T]>) {
Expand Down

0 comments on commit 86b7e03

Please sign in to comment.