Skip to content
Open
Changes from 1 commit
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
33 changes: 31 additions & 2 deletions tests/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,37 @@ async function globalSetup(): Promise<void> {
// 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;

while (attempts < maxAttempts) {
try {
const response = await fetch('http://127.0.0.1:9400/wp-admin/');
if (response.ok || response.status === 302) {
Comment on lines +67 to +82
Copy link
Copy Markdown

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)`);
break;
}
} catch {
// 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) {
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