Files
bun.sh/test/bundler/bundler_html_server.test.ts
Michael H d354714791 Plugins + cross-compilation + Bun.build API support for Bun.build({compile}) (#21915)
### What does this PR do?

in the name

### How did you verify your code works?

tests, but using ci to see if anything else broke

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
2025-08-20 01:25:49 -07:00

126 lines
3.4 KiB
TypeScript

import { describe } from "bun:test";
import { itBundled } from "./expectBundled";
describe("bundler", () => {
for (const backend of ["api", "cli"] as const) {
itBundled(`compile/${backend}/HTMLServerBasic`, {
compile: true,
backend: backend,
files: {
"/entry.ts": /* js */ `
import index from "./index.html";
using server = Bun.serve({
port: 0,
routes: {
"/": index,
},
});
const res = await fetch(server.url);
console.log("Status:", res.status);
console.log("Content-Type:", res.headers.get("content-type"));
const html = await res.text();
console.log("Has HTML tag:", html.includes("<html>"));
console.log("Has h1:", html.includes("Hello HTML"));
`,
"/index.html": /* html */ `
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<h1>Hello HTML</h1>
<script src="./app.js"></script>
</body>
</html>
`,
"/styles.css": /* css */ `
body {
background: blue;
}
`,
"/app.js": /* js */ `
console.log("Client app loaded");
`,
},
run: {
stdout: "Status: 200\nContent-Type: text/html;charset=utf-8\nHas HTML tag: true\nHas h1: true",
},
});
itBundled(`compile/${backend}/HTMLServerMultipleRoutes`, {
compile: true,
backend: backend,
files: {
"/entry.ts": /* js */ `
import home from "./home.html";
import about from "./about.html";
using server = Bun.serve({
port: 0,
routes: {
"/": home,
"/about": about,
},
});
// Test home route
const homeRes = await fetch(server.url);
console.log("Home status:", homeRes.status);
const homeHtml = await homeRes.text();
console.log("Home has content:", homeHtml.includes("Home Page"));
// Test about route
const aboutRes = await fetch(server.url + "about");
console.log("About status:", aboutRes.status);
const aboutHtml = await aboutRes.text();
console.log("About has content:", aboutHtml.includes("About Page"));
`,
"/home.html": /* html */ `
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<h1>Home Page</h1>
<script src="./app.js"></script>
</body>
</html>
`,
"/about.html": /* html */ `
<!DOCTYPE html>
<html>
<head>
<title>About</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<h1>About Page</h1>
<script src="./app.js"></script>
</body>
</html>
`,
"/styles.css": /* css */ `
body {
margin: 0;
font-family: sans-serif;
}
`,
"/app.js": /* js */ `
console.log("App loaded");
`,
},
run: {
stdout: "Home status: 200\nHome has content: true\nAbout status: 200\nAbout has content: true",
},
});
}
});