Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 52 additions & 3 deletions tests/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ async function globalSetup(): Promise<void> {
// 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',
Expand Down Expand Up @@ -48,15 +50,62 @@ async function globalSetup(): Promise<void> {
],
},
});
} 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;

// 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<void>((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) {
Comment on lines +67 to +82

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 It's a good practice to avoid hardcoding URLs and ports in test files. Defining the base URL as a constant at the top of the polling logic will improve maintainability and make it easier to update if the port or host changes.

Suggested change
const delayMs = 2000;
let attempts = 0;
while (attempts < maxAttempts) {
try {
const response = await fetch('http://127.0.0.1:9400/wp-admin/');
if (response.ok || response.status === 302) {
const BASE_URL = 'http://127.0.0.1:9400';
const maxAttempts = 30;
const delayMs = 2000;
let attempts = 0;
while (attempts < maxAttempts) {
try {
const response = await fetch(`${BASE_URL}/wp-admin/`);

// 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<void>((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<void>((resolve) => setTimeout(resolve, 1000));
}

export default globalSetup;
Loading