diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml index af113a9..1baa948 100644 --- a/.github/workflows/playwright.yml +++ b/.github/workflows/playwright.yml @@ -18,7 +18,7 @@ jobs: - name: Setup Node.js uses: actions/setup-node@v4 with: - node-version: 'lts/*' + node-version: '20' cache: 'npm' - name: Install dependencies diff --git a/tests/global-setup.ts b/tests/global-setup.ts index 1368eea..7c036ab 100644 --- a/tests/global-setup.ts +++ b/tests/global-setup.ts @@ -8,7 +8,9 @@ async function globalSetup(): Promise { // Use process.cwd() and navigate to plugin directory const pluginPath = path.join(process.cwd()); - const cliServer = await runCLI({ + let cliServer; + try { + cliServer = await runCLI({ command: 'server', php: '8.3', wp: 'latest', @@ -48,6 +50,11 @@ async function globalSetup(): Promise { ], }, }); + } catch (error) { + // eslint-disable-next-line no-console + console.error('Failed to start WordPress Playground:', error); + throw error; + } // Store the server instance globally for teardown (global as any).cliServer = cliServer; @@ -55,8 +62,50 @@ async function globalSetup(): Promise { // eslint-disable-next-line no-console console.log('WordPress Playground server started on http://127.0.0.1:9400'); - // Wait a bit for the server to be fully ready - await new Promise((resolve) => setTimeout(resolve, 2000)); + // Wait for WordPress to be fully ready by polling the health endpoint + const maxAttempts = 30; + const delayMs = 2000; + let attempts = 0; + let lastError: Error | null = null; + + while (attempts < maxAttempts) { + try { + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), 5000); + + const response = await fetch('http://127.0.0.1:9400/wp-admin/', { + signal: controller.signal, + }); + + clearTimeout(timeoutId); + + if (response.ok || response.status === 302) { + // eslint-disable-next-line no-console + console.log(`WordPress is ready after ${attempts + 1} attempt(s)`); + lastError = null; + break; + } + } catch (error) { + lastError = error as Error; + // Server not ready yet, continue polling + } + + attempts++; + if (attempts < maxAttempts) { + // eslint-disable-next-line no-console + console.log(`Waiting for WordPress to be ready... (attempt ${attempts}/${maxAttempts})`); + await new Promise((resolve) => setTimeout(resolve, delayMs)); + } + } + + if (attempts >= maxAttempts) { + // eslint-disable-next-line no-console + console.error('Last error:', lastError); + throw new Error('WordPress Playground failed to start within the expected time'); + } + + // Additional wait for plugin initialization + await new Promise((resolve) => setTimeout(resolve, 1000)); } export default globalSetup;