Skip to content

feat(playwright): reuse existing server #881

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions examples/app-playwright/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ 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)),
},
},
Expand Down
37 changes: 25 additions & 12 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))
ctx.url = `http://${host}:${port}`
if (ctx.options.dev) {
const nuxiCLI = await kit.resolvePath('nuxi/cli')
Expand Down Expand Up @@ -53,17 +65,18 @@ export async function startServer(options: StartServerOptions = {}) {
}
else {
ctx.serverProcess = execa('node', [
resolve(ctx.nuxt!.options.nitro.output!.dir!, 'server/index.mjs'),
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,
stdio: 'inherit',
env: {
...process.env,
PORT: String(port),
HOST: host,
NODE_ENV: 'test',
...options.env,
},
},
})
)
await waitForPort(port, { retries: 20, host })
}
}
Expand All @@ -79,9 +92,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) {
return _$fetch(url(path), options)
}) as typeof globalThis['$fetch']
} as typeof globalThis['$fetch']

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