aboutsummaryrefslogtreecommitdiff
path: root/bench/hot-module-reloading/css-stress-test/browser.js
blob: d0835ac57ba5864e9b17ef39015ad79fa1a44d76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const puppeteer = require("puppeteer");
const http = require("http");
const path = require("path");
const url = require("url");
const fs = require("fs");
const child_process = require("child_process");
const serverURL = process.env.TEST_SERVER_URL || "http://localhost:8080";

if (process.env.PROJECT === "bun") {
  const bunFlags = [`--origin=${serverURL}`].filter(Boolean);
  const bunExec = process.env.BUN_BIN || "bun";
  const bunProcess = child_process.spawn(bunExec, bunFlags, {
    cwd: process.cwd(),
    stdio: "ignore",
    env: {
      ...process.env,
      DISABLE_BUN_ANALYTICS: "1",
    },

    shell: false,
  });
  console.log("$", bunExec, bunFlags.join(" "));
  const isDebug = bunExec.endsWith("-debug");

  // bunProcess.stderr.pipe(process.stderr);
  // bunProcess.stdout.pipe(process.stdout);
  bunProcess.once("error", (err) => {
    console.error("❌ bun error", err);
    process.exit(1);
  });
  process.on("beforeExit", () => {
    bunProcess?.kill(0);
  });
} else if (process.env.PROJECT === "next") {
  const bunProcess = child_process.spawn(
    "./node_modules/.bin/next",
    ["--port", "8080"],
    {
      cwd: process.cwd(),
      stdio: "ignore",
      env: {
        ...process.env,
      },

      shell: false,
    }
  );
}

const delay = new Promise((resolve, reject) => {
  const watcher = fs.watch(path.resolve(process.cwd(), "src/colors.css.blob"));
  watcher.once("change", () => {
    setTimeout(() => {
      resolve();
    }, 1000);
  });
});

async function main() {
  const browser = await puppeteer.launch({
    headless: false,
    waitForInitialPage: true,
    args: [
      `--window-size=${parseInt(process.env.SCREEN_WIDTH || "1024", 10) / 2},${
        parseInt(process.env.SCREEN_HEIGHT || "1024", 10) / 2
      }`,
    ],
    defaultViewport: {
      width: parseInt(process.env.SCREEN_WIDTH || "1024", 10) / 2,
      height: parseInt(process.env.SCREEN_HEIGHT || "1024", 10) / 2,
    },
  });
  const promises = [];
  let allTestsPassed = true;

  async function runPage(key) {
    var page;

    try {
      console.log("Opening page");
      page = await browser.newPage();

      console.log(`Navigating to "http://localhost:8080/"`);

      while (true) {
        try {
          await page.goto("http://localhost:8080/", { waitUntil: "load" });
          break;
        } catch (exception) {
          if (!exception.toString().includes("ERR_CONNECTION_REFUSED")) break;
        }
      }

      await page.bringToFront();

      await delay;

      //   runner.stdout.pipe(process.stdout);
      //   runner.stderr.pipe(process.stderr);
      var didResolve = false;

      console.log(`Completed. Done.`);
    } catch (error) {
      console.error(error);
    } finally {
      await page.close();
      await browser.close();
    }
  }

  return runPage();
}

main().catch((error) =>
  setTimeout(() => {
    throw error;
  })
);