mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 21:55:11 +00:00
fix(test-utils): improve random port allocation (#3398)
This commit is contained in:
parent
90e23c600c
commit
39f7eb2cd2
@ -17,7 +17,7 @@
|
|||||||
"@nuxt/schema": "3.0.0",
|
"@nuxt/schema": "3.0.0",
|
||||||
"defu": "^5.0.1",
|
"defu": "^5.0.1",
|
||||||
"execa": "^6.1.0",
|
"execa": "^6.1.0",
|
||||||
"get-port-please": "^2.4.2",
|
"get-port-please": "^2.4.3",
|
||||||
"jiti": "^1.13.0",
|
"jiti": "^1.13.0",
|
||||||
"ohmyfetch": "^0.4.15"
|
"ohmyfetch": "^0.4.15"
|
||||||
},
|
},
|
||||||
|
@ -1,28 +1,12 @@
|
|||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
import { createServer, AddressInfo } from 'net'
|
|
||||||
import { execa } from 'execa'
|
import { execa } from 'execa'
|
||||||
import { getPort } from 'get-port-please'
|
import { getRandomPort, waitForPort } from 'get-port-please'
|
||||||
import { fetch as _fetch, $fetch as _$fetch, FetchOptions } from 'ohmyfetch'
|
import { fetch as _fetch, $fetch as _$fetch, FetchOptions } from 'ohmyfetch'
|
||||||
import { useTestContext } from './context'
|
import { useTestContext } from './context'
|
||||||
|
|
||||||
// TODO: use the export from `get-port-please`
|
|
||||||
function checkPort (port: number, host: string): Promise<number|false> {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
const server = createServer()
|
|
||||||
server.unref()
|
|
||||||
server.on('error', () => { resolve(false) })
|
|
||||||
server.listen(port, host, () => {
|
|
||||||
const { port } = server.address() as AddressInfo
|
|
||||||
server.close(() => { resolve(port) })
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function listen () {
|
export async function listen () {
|
||||||
const ctx = useTestContext()
|
const ctx = useTestContext()
|
||||||
const host = process.env.HOST || '0.0.0.0'
|
const port = await getRandomPort()
|
||||||
const port = await getPort({ host, random: true })
|
|
||||||
|
|
||||||
ctx.url = 'http://localhost:' + port
|
ctx.url = 'http://localhost:' + port
|
||||||
ctx.serverProcess = execa('node', [
|
ctx.serverProcess = execa('node', [
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@ -33,17 +17,7 @@ export async function listen () {
|
|||||||
NODE_ENV: 'test'
|
NODE_ENV: 'test'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
await waitForPort(port, { retries: 8 })
|
||||||
const TRIES = 50
|
|
||||||
const DELAY = 100
|
|
||||||
|
|
||||||
for (let i = TRIES; i; i--) {
|
|
||||||
await new Promise(resolve => setTimeout(resolve, DELAY))
|
|
||||||
// wait until port is in used
|
|
||||||
if (await checkPort(port, host) === false) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetch (path: string, options?: any) {
|
export function fetch (path: string, options?: any) {
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
"escape-string-regexp": "^5.0.0",
|
"escape-string-regexp": "^5.0.0",
|
||||||
"externality": "^0.1.6",
|
"externality": "^0.1.6",
|
||||||
"fs-extra": "^10.0.1",
|
"fs-extra": "^10.0.1",
|
||||||
"get-port-please": "^2.4.2",
|
"get-port-please": "^2.4.3",
|
||||||
"knitwork": "^0.1.0",
|
"knitwork": "^0.1.0",
|
||||||
"magic-string": "^0.25.7",
|
"magic-string": "^0.25.7",
|
||||||
"mlly": "^0.4.3",
|
"mlly": "^0.4.3",
|
||||||
|
15
yarn.lock
15
yarn.lock
@ -3253,7 +3253,7 @@ __metadata:
|
|||||||
"@nuxt/schema": 3.0.0
|
"@nuxt/schema": 3.0.0
|
||||||
defu: ^5.0.1
|
defu: ^5.0.1
|
||||||
execa: ^6.1.0
|
execa: ^6.1.0
|
||||||
get-port-please: ^2.4.2
|
get-port-please: ^2.4.3
|
||||||
jiti: ^1.13.0
|
jiti: ^1.13.0
|
||||||
ohmyfetch: ^0.4.15
|
ohmyfetch: ^0.4.15
|
||||||
playwright: ^1.19.2
|
playwright: ^1.19.2
|
||||||
@ -3369,7 +3369,7 @@ __metadata:
|
|||||||
escape-string-regexp: ^5.0.0
|
escape-string-regexp: ^5.0.0
|
||||||
externality: ^0.1.6
|
externality: ^0.1.6
|
||||||
fs-extra: ^10.0.1
|
fs-extra: ^10.0.1
|
||||||
get-port-please: ^2.4.2
|
get-port-please: ^2.4.3
|
||||||
knitwork: ^0.1.0
|
knitwork: ^0.1.0
|
||||||
magic-string: ^0.25.7
|
magic-string: ^0.25.7
|
||||||
magic-string-extra: ^0.1.2
|
magic-string-extra: ^0.1.2
|
||||||
@ -11370,7 +11370,7 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"get-port-please@npm:^2.1.0, get-port-please@npm:^2.2.0, get-port-please@npm:^2.4.2":
|
"get-port-please@npm:^2.1.0, get-port-please@npm:^2.2.0":
|
||||||
version: 2.4.2
|
version: 2.4.2
|
||||||
resolution: "get-port-please@npm:2.4.2"
|
resolution: "get-port-please@npm:2.4.2"
|
||||||
dependencies:
|
dependencies:
|
||||||
@ -11379,6 +11379,15 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"get-port-please@npm:^2.4.3":
|
||||||
|
version: 2.4.3
|
||||||
|
resolution: "get-port-please@npm:2.4.3"
|
||||||
|
dependencies:
|
||||||
|
fs-memo: ^1.2.0
|
||||||
|
checksum: ad29938ddafab5dc9d100e9c7dc9348bc81dd5c8f0dc9e1c45c41abd452654c9fb69cc6ff77f9f31e448567216479770cedfa96e17f54796222bc17145d2f228
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"get-port@npm:^5.1.1":
|
"get-port@npm:^5.1.1":
|
||||||
version: 5.1.1
|
version: 5.1.1
|
||||||
resolution: "get-port@npm:5.1.1"
|
resolution: "get-port@npm:5.1.1"
|
||||||
|
Loading…
Reference in New Issue
Block a user