1
0
mirror of synced 2026-01-02 12:04:38 -05:00

Clean up jest (#50177)

This commit is contained in:
Peter Bengtsson
2024-04-16 15:38:46 -04:00
committed by GitHub
parent a863eab83f
commit 227e4e2bbf
24 changed files with 55 additions and 3071 deletions

View File

@@ -1,13 +0,0 @@
#!/usr/bin/env node
import { START_JEST_SERVER, isServerHealthy, killServer } from './server-for-jest.js'
export default async () => {
if (START_JEST_SERVER) {
global.__SERVER__.close()
if (await isServerHealthy()) {
killServer()
}
}
}

View File

@@ -1,35 +0,0 @@
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)
}

View File

@@ -1,31 +0,0 @@
#!/usr/bin/env node
import { main } from '#src/frame/start-server.js'
import { PORT, START_JEST_SERVER, isServerHealthy, isPortRunning } from './server-for-jest.js'
export default async () => {
if (START_JEST_SERVER) {
console.log(`Starting a server for jest on port :${PORT}.`)
process.env.NODE_ENV = 'test'
// Has to be this because that's what the end-to-end tests expect
process.env.PORT = `${PORT}`
if (await isPortRunning()) {
console.error(`Something's already running on :${PORT}`)
console.log(
'If you intend to run jest tests with an existing server, set env var START_JEST_SERVER=false',
)
process.exit(1)
}
// So it can be accessed from the script that
// is set up by the jest config: `globalTeardown`
global.__SERVER__ = await main()
console.assert(await isServerHealthy())
} else {
console.warn(`jest is NOT automatically starting a server on port :${PORT}`)
}
}