2022-04-15 15:19:05 +00:00
|
|
|
import { resolve } from 'node:path'
|
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-02-11 13:22:58 +00:00
|
|
|
import { fetch as _fetch, $fetch as _$fetch, FetchOptions } from 'ohmyfetch'
|
2022-04-20 10:19:30 +00:00
|
|
|
import * as _kit from '@nuxt/kit'
|
2022-02-11 13:22:58 +00:00
|
|
|
import { useTestContext } from './context'
|
|
|
|
|
2022-04-20 10:19:30 +00:00
|
|
|
// @ts-ignore type cast
|
|
|
|
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-04-07 19:15:30 +00:00
|
|
|
cwd: ctx.nuxt.options.rootDir,
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
PORT: String(port),
|
|
|
|
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 {
|
|
|
|
const res = await $fetch('/')
|
|
|
|
if (!res.includes('__NUXT_LOADING__')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} catch {}
|
2022-02-11 13:22:58 +00:00
|
|
|
}
|
2022-03-17 21:31:06 +00:00
|
|
|
throw new Error('Timeout waiting for dev server!')
|
|
|
|
} else {
|
|
|
|
ctx.serverProcess = execa('node', [
|
|
|
|
resolve(ctx.nuxt.options.nitro.output.dir, 'server/index.mjs')
|
|
|
|
], {
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
PORT: String(port),
|
|
|
|
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-03-17 21:31:06 +00:00
|
|
|
if (ctx.listener) {
|
|
|
|
await ctx.listener.close()
|
|
|
|
}
|
2022-03-17 21:17:54 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
return ctx.url + path
|
|
|
|
}
|