1
0
mirror of synced 2025-12-20 02:19:14 -05:00
Files
docs/script/server-for-jest.mjs
Peter Bengtsson 4fcd3ae25f automatically start server for jest (#26206)
* reinstate

* start server manually

* routing tests too

* skip more

* sleep more and fail if not 200

* use e2etest for content/ too

* automatically start server for jest

* does this work?

* feedbacked

* rename things

* getting it to work

* add dev dependency

* install the right version

* don't need to start that

* fix package lock

* update readme about it

* feedbacked
2022-03-18 21:46:07 +00:00

36 lines
885 B
JavaScript

import kill from 'kill-port'
import portUsed from 'port-used'
import got, { RequestError } from 'got'
export const PORT = 4000
// By default it's on
export const START_JEST_SERVER = Boolean(JSON.parse(process.env.START_JEST_SERVER || 1))
export async function isServerHealthy() {
try {
const res = await got.head(`http://localhost:${PORT}/healthz`, { retry: { limit: 0 } })
return res.statusCode === 200
} catch (err) {
// This exception is thrown if you can't even connect.
if (err instanceof RequestError) {
return false
}
throw err
}
}
export function killServer() {
kill(PORT, 'tcp')
.then(() => {
console.log(`Killed what was on :${PORT}`)
})
.catch((error) => {
console.log(`Unable to kill whatever was on :${PORT}:`, error)
})
}
export async function isPortRunning() {
return await portUsed.check(PORT)
}