aboutsummaryrefslogtreecommitdiff
path: root/integration/scripts/browser.js
blob: 7aa027d770bf6879a55783e5604628970694f8b9 (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
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 snippetsDir = path.resolve(__dirname, "../snippets");
const serverURL = process.env.TEST_SERVER_URL || "http://localhost:8080";

const DISABLE_HMR = !!process.env.DISABLE_HMR;
const bunFlags = [
  `--origin=${serverURL}`,
  DISABLE_HMR && "--disable-hmr",
].filter(Boolean);
const bunExec = process.env.BUN_BIN || "bun";
const bunProcess = child_process.spawn(bunExec, bunFlags, {
  cwd: snippetsDir,
  stdio: "pipe",

  shell: false,
});
console.log("$", bunExec, bunFlags.join(" "));

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);
});

function writeSnapshot(name, code) {
  const file = path.join(
    __dirname,
    "../snapshots" + (DISABLE_HMR ? "-no-hmr" : ""),
    name
  );
  fs.writeFileSync(file, code);
}

async function main() {
  const browser = await puppeteer.launch();
  const promises = [];
  let allTestsPassed = true;

  async function runPage(key) {
    var page;
    try {
      page = await browser.newPage();
      page.on("console", (obj) =>
        console.log(`[console.${obj.type()}] ${obj.text()}`)
      );
      page.exposeFunction("testFail", (error) => {
        console.log(`❌ ${error}`);
        allTestsPassed = false;
      });
      let testDone = new Promise((resolve) => {
        page.exposeFunction("testDone", resolve);
      });
      await page.goto(`${serverURL}/`, {
        waitUntil: "domcontentloaded",
      });
      await page.evaluate(`
        globalThis.runTest("${key}");
      `);
      await testDone;

      console.log(`✅ ${key}`);
    } catch (e) {
      allTestsPassed = false;
      console.log(`❌ ${key}: ${(e && e.message) || e}`);
    } finally {
      try {
        const code = await page.evaluate(`
         globalThis.getModuleScriptSrc("${key}");
      `);
        writeSnapshot(key, code);
      } catch (exception) {
        console.warn(`Failed to update snapshot: ${key}`, exception);
      }
    }

    await page.close();
  }

  const tests = [
    "/cjs-transform-shouldnt-have-static-imports-in-cjs-function.js",
    "/bundled-entry-point.js",
    "/export.js",
    "/type-only-imports.ts",
  ];

  for (let test of tests) {
    await runPage(test);
  }

  await browser.close();
  bunProcess.kill(0);

  if (!allTestsPassed) {
    console.error(`❌ browser test failed`);
    process.exit(1);
  } else {
    console.log(`✅ browser test passed`);
    bunProcess.kill(0);
    process.exit(0);
  }
}

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