chore: find and use an available port in tests (#11043)
You can now run `pnpm dev [test-suite]` even if port 3000 is busy. I copied the error message as is from what nextjs shows.
This commit is contained in:
26
test/dev.ts
26
test/dev.ts
@@ -68,13 +68,31 @@ if (args.o) {
|
|||||||
await open(`http://localhost:3000${adminRoute}`)
|
await open(`http://localhost:3000${adminRoute}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const findOpenPort = (startPort: number): Promise<number> => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const server = createServer()
|
||||||
|
server.listen(startPort, () => {
|
||||||
|
console.log(`✓ Running on port ${startPort}`)
|
||||||
|
server.close(() => resolve(startPort))
|
||||||
|
})
|
||||||
|
server.on('error', () => {
|
||||||
|
console.log(`⚠ Port ${startPort} is in use, trying ${startPort + 1} instead.`)
|
||||||
|
findOpenPort(startPort + 1)
|
||||||
|
.then(resolve)
|
||||||
|
.catch(reject)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const port = process.env.PORT ? Number(process.env.PORT) : 3000
|
const port = process.env.PORT ? Number(process.env.PORT) : 3000
|
||||||
|
|
||||||
|
const availablePort = await findOpenPort(port)
|
||||||
|
|
||||||
// @ts-expect-error the same as in test/helpers/initPayloadE2E.ts
|
// @ts-expect-error the same as in test/helpers/initPayloadE2E.ts
|
||||||
const app = nextImport({
|
const app = nextImport({
|
||||||
dev: true,
|
dev: true,
|
||||||
hostname: 'localhost',
|
hostname: 'localhost',
|
||||||
port,
|
port: availablePort,
|
||||||
dir: rootDir,
|
dir: rootDir,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -88,7 +106,7 @@ void app.prepare().then(() => {
|
|||||||
createServer(async (req, res) => {
|
createServer(async (req, res) => {
|
||||||
const parsedUrl = parse(req.url || '', true)
|
const parsedUrl = parse(req.url || '', true)
|
||||||
await handle(req, res, parsedUrl)
|
await handle(req, res, parsedUrl)
|
||||||
}).listen(port, () => {
|
}).listen(availablePort, () => {
|
||||||
resolveServer()
|
resolveServer()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -97,8 +115,8 @@ await serverPromise
|
|||||||
process.env.PAYLOAD_DROP_DATABASE = process.env.PAYLOAD_DROP_DATABASE === 'false' ? 'false' : 'true'
|
process.env.PAYLOAD_DROP_DATABASE = process.env.PAYLOAD_DROP_DATABASE === 'false' ? 'false' : 'true'
|
||||||
|
|
||||||
// fetch the admin url to force a render
|
// fetch the admin url to force a render
|
||||||
void fetch(`http://localhost:${port}${adminRoute}`)
|
void fetch(`http://localhost:${availablePort}${adminRoute}`)
|
||||||
void fetch(`http://localhost:${port}/api/access`)
|
void fetch(`http://localhost:${availablePort}/api/access`)
|
||||||
// This ensures that the next-server process is killed when this process is killed and doesn't linger around.
|
// This ensures that the next-server process is killed when this process is killed and doesn't linger around.
|
||||||
process.on('SIGINT', () => {
|
process.on('SIGINT', () => {
|
||||||
if (child) {
|
if (child) {
|
||||||
|
|||||||
Reference in New Issue
Block a user