aboutsummaryrefslogtreecommitdiff
path: root/integration/scripts/browser.js
blob: 2d758e638482cf11c490eb608ed44abe88c6f052 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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",
  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);
});

function writeSnapshot(name, code) {
  let file = path.join(__dirname, "../snapshots", name);

  if (!DISABLE_HMR) {
    file =
      file.substring(0, file.length - path.extname(file).length) +
      ".hmr" +
      path.extname(file);
  }

  if (!fs.existsSync(path.dirname(file))) {
    fs.mkdirSync(path.dirname(file), { recursive: true });
  }

  fs.writeFileSync(
    isDebug
      ? file.substring(0, file.length - path.extname(file).length) +
          ".debug" +
          path.extname(file)
      : 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",
    "/global-is-remapped-to-globalThis.js",
    "/multiple-imports.js",
    "/ts-fallback-rewrite-works.js",
    "/tsx-fallback-rewrite-works.js",
    "/lodash-regexp.js",
    "/unicode-identifiers.js",
    "/string-escapes.js",
    "/package-json-exports/index.js",
    "/array-args-with-default-values.js",
    "/forbid-in-is-correct.js",
    "/code-simplification-neql-define.js",
    "/spread_with_key.tsx",
    "/styledcomponents-output.js",
    "/void-shouldnt-delete-call-expressions.js",
  ];
  tests.reverse();

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