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
7 changes: 7 additions & 0 deletions examples/app-playwright/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true },
compatibilityDate: '2024-07-11',
// TODO: Add to default options?
// https://nuxt.com/docs/api/nuxt-config#ignore
ignore: [
'playwright-report',
'test-results',
],
runtimeConfig: {
public: {
myValue: 'Welcome to Playwright!',
Expand Down
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 for test first development. */
reuseExistingServer: process.env.CI ? false : true,
rootDir: fileURLToPath(new URL('.', import.meta.url)),
},
},
Expand Down
25 changes: 25 additions & 0 deletions src/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ export interface StartServerOptions {
env?: Record<string, unknown>
}

/**
* Reuse an existing server if it's already running.
*
* This is useful to do test first development and speed up the test execution.
*/
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 (127.0.0.1 is not working)
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')
}

// TODO: To run against deployed server, maybe we should allow to change the protocol?
ctx.url = `http://${host}:${port}`
}

/**
* Start a new server.
*
* This server can be a dev server using the Nuxt CLI dev command or a production server using the Nuxt build output.
*
* During testing, it's recommended to reuse an existing server using `reuseExistingServer` setting. This will speed up the test execution and allow test first development.
*/
export async function startServer(options: StartServerOptions = {}) {
const ctx = useTestContext()
await stopServer()
Expand Down
12 changes: 8 additions & 4 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.fixture) {
if (ctx.options.reuseExistingServer) {
await reuseExistingServer()
}

if (ctx.options.fixture && !ctx.options.reuseExistingServer) {
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(ctx.options.env)
}

Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface TestOptions {
rootDir: string
buildDir: string
nuxtConfig: NuxtConfig
reuseExistingServer?: boolean
build: boolean
dev: boolean
setupTimeout: number
Expand Down
Loading