1
0

global-setup.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const { execSync } = require('node:child_process');
  2. const http = require('node:http');
  3. const https = require('node:https');
  4. const COMPOSE_FILE = 'tests/docker-compose-playwright.yml';
  5. function isHttpOk(url) {
  6. return new Promise((resolve, reject) => {
  7. const client = url.startsWith('https://') ? https : http;
  8. const req = client.get(url, (res) => {
  9. const status = res.statusCode || 0;
  10. // Drain body so sockets can be reused/closed cleanly.
  11. res.resume();
  12. resolve(status >= 200 && status < 300);
  13. });
  14. req.on('error', reject);
  15. });
  16. }
  17. async function waitForReady(name, url, timeoutMs) {
  18. const start = Date.now();
  19. while (Date.now() - start < timeoutMs) {
  20. try {
  21. if (await isHttpOk(url)) {
  22. return;
  23. }
  24. } catch {
  25. // Retry until timeout.
  26. }
  27. await new Promise((resolve) => setTimeout(resolve, 1000));
  28. }
  29. throw new Error(`Timed out waiting for ${name} at ${url}`);
  30. }
  31. module.exports = async () => {
  32. try {
  33. execSync(`docker compose -f ${COMPOSE_FILE} up -d --build`, {
  34. stdio: 'inherit',
  35. });
  36. } catch (error) {
  37. throw new Error(
  38. `Failed to start Docker test stack from ${COMPOSE_FILE}. ` +
  39. `Ensure Docker is running. Original error: ${error.message}`
  40. );
  41. }
  42. const timeoutMs = 180_000;
  43. await waitForReady('standalone', 'http://127.0.0.1:18180/index.html', timeoutMs);
  44. await waitForReady('standalone-new', 'http://127.0.0.1:18185/index.html', timeoutMs);
  45. await waitForReady('backend', 'http://127.0.0.1:18181/empty.php', timeoutMs);
  46. await waitForReady('frontend', 'http://127.0.0.1:18182/index-modern.html', timeoutMs);
  47. await waitForReady('dual', 'http://127.0.0.1:18183/index-modern.html', timeoutMs);
  48. };