Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(playwright): reuse existing server #881

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion examples/app-playwright/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ export default defineConfig<ConfigOptions>({
trace: 'on-first-retry',
/* Nuxt configuration options */
nuxt: {
/* Reuse a server if it's already running. Useful when developing tests. */
reuseExistingServer: process.env.CI ? false : true,
rootDir: fileURLToPath(new URL('.', import.meta.url)),
},
},
projects: devicesToTest.map(p => typeof p === 'string' ? ({ name: p, use: devices[p] }) : p),
projects: devicesToTest.map(p => typeof p === 'string' ? { name: p, use: devices[p] } : p),
Barbapapazes marked this conversation as resolved.
Show resolved Hide resolved
})
42 changes: 28 additions & 14 deletions src/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,23 @@ export interface StartServerOptions {
env?: Record<string, unknown>
}

export async function reuseExistingServer() {
const ctx = useTestContext()
const host = ctx.options.host || 'localhost' // Default to localhost since it's the host used by nuxt dev server
const port = ctx.options.port || 3000 // Default to 3000 since it's the port used by nuxt dev server

if (port === undefined) {
throw new Error('Port is required when reusing server')
}

ctx.url = `http://${host}:${port}`
}

export async function startServer(options: StartServerOptions = {}) {
const ctx = useTestContext()
await stopServer()
const host = '127.0.0.1'
const port = ctx.options.port || await getRandomPort(host)
const port = ctx.options.port || (await getRandomPort(host))
Barbapapazes marked this conversation as resolved.
Show resolved Hide resolved
ctx.url = `http://${host}:${port}`
if (ctx.options.dev) {
const nuxiCLI = await kit.resolvePath('nuxi/cli')
Expand Down Expand Up @@ -52,18 +64,20 @@ export async function startServer(options: StartServerOptions = {}) {
throw lastError || 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),
HOST: host,
NODE_ENV: 'test',
...options.env,
ctx.serverProcess = execa(
'node',
[resolve(ctx.nuxt!.options.nitro.output!.dir!, 'server/index.mjs')],
{
stdio: 'inherit',
env: {
...process.env,
PORT: String(port),
HOST: host,
NODE_ENV: 'test',
...options.env,
Barbapapazes marked this conversation as resolved.
Show resolved Hide resolved
},
Barbapapazes marked this conversation as resolved.
Show resolved Hide resolved
Barbapapazes marked this conversation as resolved.
Show resolved Hide resolved
},
})
)
Barbapapazes marked this conversation as resolved.
Show resolved Hide resolved
await waitForPort(port, { retries: 20, host })
}
}
Expand All @@ -79,9 +93,9 @@ export function fetch(path: string, options?: RequestInit) {
return _fetch(url(path), options)
}

export const $fetch = (function (path: string, options?: FetchOptions) {
export const $fetch = function (path: string, options?: FetchOptions) {
Barbapapazes marked this conversation as resolved.
Show resolved Hide resolved
return _$fetch(url(path), options)
}) as typeof globalThis['$fetch']
} as typeof globalThis['$fetch']
Barbapapazes marked this conversation as resolved.
Show resolved Hide resolved

export function url(path: string) {
const ctx = useTestContext()
Expand Down
10 changes: 7 additions & 3 deletions src/core/setup/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createTestContext, setTestContext } from '../context'
import { buildFixture, loadFixture } from '../nuxt'
import { startServer, stopServer } from '../server'
import { reuseExistingServer, startServer, stopServer } from '../server'
import { createBrowser } from '../browser'
import type { TestHooks, TestOptions } from '../types'
import setupCucumber from './cucumber'
Expand Down Expand Up @@ -41,15 +41,19 @@ export function createTest(options: Partial<TestOptions>): TestHooks {
}

const setup = async () => {
if (ctx.options.reuseExistingServer) {
await reuseExistingServer()
}

if (ctx.options.fixture) {
await loadFixture()
}

if (ctx.options.build) {
if (ctx.options.build && !ctx.options.reuseExistingServer) {
await buildFixture()
}

if (ctx.options.server) {
if (ctx.options.server && !ctx.options.reuseExistingServer) {
await startServer()
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface TestOptions {
rootDir: string
buildDir: string
nuxtConfig: NuxtConfig
reuseExistingServer?: boolean
build: boolean
dev: boolean
setupTimeout: number
Expand All @@ -23,6 +24,7 @@ export interface TestOptions {
launch?: LaunchOptions
}
server: boolean
host?: string
port?: number
}

Expand Down