Skip to content

Commit

Permalink
refactor: use execa to handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Aug 3, 2020
1 parent 5670297 commit 0f0d6bd
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions tests/utils/devServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ const startServer = async ({ cwd, env = {} }) => {
const ps = execa(cliPath, ['dev', '-p', port], {
cwd,
env,
reject: false,
})
return new Promise((resolve, reject) => {
let selfKilled = false
ps.stdout.on('data', data => {
if (data.toString().includes('Server now ready on')) {
resolve({
url,
host,
port,
close: async () => {
selfKilled = true
const pids = await pidtree(ps.pid).catch(() => [])
pids.forEach(pid => () => {
try {
Expand All @@ -42,22 +43,12 @@ const startServer = async ({ cwd, env = {} }) => {
}
})
ps.kill()
await Promise.race([ps, new Promise(resolve => setTimeout(resolve, 1000))])
await Promise.race([ps.catch(() => undefined), new Promise(resolve => setTimeout(resolve, 1000))])
},
})
}
})

let error = ''
ps.stderr.on('data', data => {
error = error + data.toString()
})
ps.on('close', code => {
if (code !== 0) {
console.error(error)
reject(error)
}
})
ps.catch(error => !selfKilled && reject(error))
})
}

Expand All @@ -82,7 +73,9 @@ const withDevServer = async (options, testHandler) => {
server = await startDevServer(options)
return await testHandler(server)
} finally {
await server.close()
if (server) {
await server.close()
}
}
}

Expand Down

0 comments on commit 0f0d6bd

Please sign in to comment.