2022-02-11 13:22:58 +00:00
|
|
|
import { execa } from 'execa'
|
2022-02-28 21:41:44 +00:00
|
|
|
import { getRandomPort, waitForPort } from 'get-port-please'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { FetchOptions } from 'ofetch'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { $fetch as _$fetch, fetch as _fetch } from 'ofetch'
|
2022-04-20 10:19:30 +00:00
|
|
|
import * as _kit from '@nuxt/kit'
|
2023-04-06 12:07:22 +00:00
|
|
|
import { resolve } from 'pathe'
|
2022-02-11 13:22:58 +00:00
|
|
|
import { useTestContext } from './context'
|
|
|
|
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error type cast
|
2023-03-11 21:16:01 +00:00
|
|
|
// eslint-disable-next-line
|
2022-04-20 10:19:30 +00:00
|
|
|
const kit: typeof _kit = _kit.default || _kit
|
|
|
|
|
2022-03-17 21:17:54 +00:00
|
|
|
export async function startServer () {
|
2022-02-11 13:22:58 +00:00
|
|
|
const ctx = useTestContext()
|
2022-03-17 21:17:54 +00:00
|
|
|
await stopServer()
|
2022-02-28 21:41:44 +00:00
|
|
|
const port = await getRandomPort()
|
2022-05-17 01:40:35 +00:00
|
|
|
ctx.url = 'http://127.0.0.1:' + port
|
2022-03-17 21:31:06 +00:00
|
|
|
if (ctx.options.dev) {
|
2022-04-20 10:19:30 +00:00
|
|
|
const nuxiCLI = await kit.resolvePath('nuxi/cli')
|
|
|
|
ctx.serverProcess = execa(nuxiCLI, ['dev'], {
|
2022-08-26 15:47:29 +00:00
|
|
|
cwd: ctx.nuxt!.options.rootDir,
|
2022-04-07 19:15:30 +00:00
|
|
|
stdio: 'inherit',
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
PORT: String(port),
|
2022-10-25 09:17:24 +00:00
|
|
|
NITRO_PORT: String(port),
|
2022-04-07 19:15:30 +00:00
|
|
|
NODE_ENV: 'development'
|
|
|
|
}
|
|
|
|
})
|
2022-04-07 21:49:43 +00:00
|
|
|
await waitForPort(port, { retries: 32 })
|
2022-03-17 21:31:06 +00:00
|
|
|
for (let i = 0; i < 50; i++) {
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 100))
|
2022-04-07 19:15:30 +00:00
|
|
|
try {
|
2023-01-30 20:21:02 +00:00
|
|
|
const res = await $fetch(ctx.nuxt!.options.app.baseURL)
|
2022-04-07 19:15:30 +00:00
|
|
|
if (!res.includes('__NUXT_LOADING__')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} catch {}
|
2022-02-11 13:22:58 +00:00
|
|
|
}
|
2023-01-30 20:21:02 +00:00
|
|
|
ctx.serverProcess.kill()
|
2022-03-17 21:31:06 +00:00
|
|
|
throw new Error('Timeout waiting for dev server!')
|
|
|
|
} else {
|
|
|
|
ctx.serverProcess = execa('node', [
|
2022-08-26 15:47:29 +00:00
|
|
|
resolve(ctx.nuxt!.options.nitro.output!.dir!, 'server/index.mjs')
|
2022-03-17 21:31:06 +00:00
|
|
|
], {
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
PORT: String(port),
|
2022-10-25 09:17:24 +00:00
|
|
|
NITRO_PORT: String(port),
|
2022-03-17 21:31:06 +00:00
|
|
|
NODE_ENV: 'test'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await waitForPort(port, { retries: 8 })
|
|
|
|
}
|
2022-02-11 13:22:58 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 21:17:54 +00:00
|
|
|
export async function stopServer () {
|
|
|
|
const ctx = useTestContext()
|
|
|
|
if (ctx.serverProcess) {
|
|
|
|
await ctx.serverProcess.kill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 13:22:58 +00:00
|
|
|
export function fetch (path: string, options?: any) {
|
|
|
|
return _fetch(url(path), options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function $fetch (path: string, options?: FetchOptions) {
|
|
|
|
return _$fetch(url(path), options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function url (path: string) {
|
|
|
|
const ctx = useTestContext()
|
|
|
|
if (!ctx.url) {
|
2022-07-28 13:40:03 +00:00
|
|
|
throw new Error('url is not available (is server option enabled?)')
|
2022-02-11 13:22:58 +00:00
|
|
|
}
|
2023-04-06 12:07:22 +00:00
|
|
|
if (path.startsWith(ctx.url)) {
|
|
|
|
return path
|
|
|
|
}
|
2022-02-11 13:22:58 +00:00
|
|
|
return ctx.url + path
|
|
|
|
}
|