diff options
author | 2021-09-17 03:14:23 -0700 | |
---|---|---|
committer | 2021-09-17 03:14:23 -0700 | |
commit | 1c7485e58c2fc02ceed4656752c5315178e9b9a9 (patch) | |
tree | 2633805273fb709ef1a20bc12d869033f74c810c /integration/scripts/browser.js | |
parent | 872428de89c63e0034573c1419fa616f8d0648b1 (diff) | |
download | bun-1c7485e58c2fc02ceed4656752c5315178e9b9a9.tar.gz bun-1c7485e58c2fc02ceed4656752c5315178e9b9a9.tar.zst bun-1c7485e58c2fc02ceed4656752c5315178e9b9a9.zip |
Begin to add integration tests
Diffstat (limited to 'integration/scripts/browser.js')
-rw-r--r-- | integration/scripts/browser.js | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/integration/scripts/browser.js b/integration/scripts/browser.js new file mode 100644 index 000000000..7aa027d77 --- /dev/null +++ b/integration/scripts/browser.js @@ -0,0 +1,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; + }) +); |