Files
bun.sh/test/bake/dev/bundle.test.ts
2025-03-01 00:07:20 -08:00

268 lines
7.0 KiB
TypeScript

// Bundle tests are tests concerning bundling bugs that only occur in DevServer.
import { expect } from "bun:test";
import { devTest, emptyHtmlFile, minimalFramework, reactRefreshStub } from "../bake-harness";
devTest("import identifier doesnt get renamed", {
framework: minimalFramework,
files: {
"db.ts": `export const abc = "123";`,
"routes/index.ts": `
import { abc } from '../db';
export default function (req, meta) {
let v1 = "";
const v2 = v1
? abc.toFixed(2)
: abc.toString();
return new Response('Hello, ' + v2 + '!');
}
`,
},
async test(dev) {
await dev.fetch("/").equals("Hello, 123!");
await dev.write("db.ts", `export const abc = "456";`);
await dev.fetch("/").equals("Hello, 456!");
await dev.patch("routes/index.ts", {
find: "Hello",
replace: "Bun",
});
await dev.fetch("/").equals("Bun, 456!");
},
});
devTest("symbol collision with import identifier", {
framework: minimalFramework,
files: {
"db.ts": `export const abc = "123";`,
"routes/index.ts": `
let import_db = 987;
import { abc } from '../db';
export default function (req, meta) {
let v1 = "";
const v2 = v1
? abc.toFixed(2)
: abc.toString();
return new Response('Hello, ' + v2 + ', ' + import_db + '!');
}
`,
},
async test(dev) {
await dev.fetch("/").equals("Hello, 123, 987!");
await dev.write("db.ts", `export const abc = "456";`);
await dev.fetch("/").equals("Hello, 456, 987!");
},
});
devTest('uses "development" condition', {
framework: minimalFramework,
files: {
"node_modules/example/package.json": JSON.stringify({
name: "example",
version: "1.0.0",
exports: {
".": {
development: "./development.js",
default: "./production.js",
},
},
}),
"node_modules/example/development.js": `export default "development";`,
"node_modules/example/production.js": `export default "production";`,
"routes/index.ts": `
import environment from 'example';
export default function (req, meta) {
return new Response('Environment: ' + environment);
}
`,
},
async test(dev) {
await dev.fetch("/").equals("Environment: development");
},
});
devTest("importing a file before it is created", {
files: {
"index.html": emptyHtmlFile({
styles: [],
scripts: ["index.ts"],
}),
"index.ts": `
import { abc } from './second';
console.log('value: ' + abc);
`,
},
async test(dev) {
await using c = await dev.client("/", {
errors: [`index.ts:1:21: error: Could not resolve: "./second"`],
});
await c.expectReload(async () => {
await dev.write("second.ts", `export const abc = "456";`);
});
await c.expectMessage("value: 456");
},
});
devTest("default export same-scope handling", {
files: {
...reactRefreshStub,
"index.html": emptyHtmlFile({
styles: [],
scripts: ["index.ts", "react-refresh/runtime"],
}),
"index.ts": `
await import("./fixture1.ts");
console.log((new ((await import("./fixture2.ts")).default)).a);
await import("./fixture3.ts");
console.log((new ((await import("./fixture4.ts")).default)).result);
console.log((await import("./fixture5.ts")).default);
console.log((await import("./fixture6.ts")).default);
console.log((await import("./fixture7.ts")).default());
console.log((await import("./fixture8.ts")).default());
console.log((await import("./fixture9.ts")).default(false));
`,
"fixture1.ts": `
const sideEffect = () => "a";
export default class A {
[sideEffect()] = "ONE";
}
console.log(new A().a);
`,
"fixture2.ts": `
const sideEffect = () => "a";
export default class A {
[sideEffect()] = "TWO";
}
`,
"fixture3.ts": `
export default class A {
result = "THREE"
}
console.log(new A().result);
`,
"fixture4.ts": `
export default class MOVE {
result = "FOUR"
}
`,
"fixture5.ts": `
const default_export = "FIVE";
export default default_export;
`,
"fixture6.ts": `
const default_export = "S";
function sideEffect() {
return default_export + "EVEN";
}
export default sideEffect();
console.log(default_export + "IX");
`,
"fixture7.ts": `
export default function() { return "EIGHT" };
`,
"fixture8.ts": `
export default function MOVE() { return "NINE" };
`,
"fixture9.ts": `
export default function named(flag = true) { return flag ? "TEN" : "ELEVEN" };
console.log(named());
`,
},
async test(dev) {
await using c = await dev.client("/", { storeHotChunks: true });
c.expectMessage(
//
"ONE",
"TWO",
"THREE",
"FOUR",
"FIVE",
"SIX",
"SEVEN",
"EIGHT",
"NINE",
"TEN",
"ELEVEN",
);
const filesExpectingMove = Object.entries(dev.options.files)
.filter(([, content]) => content.includes("MOVE"))
.map(([path]) => path);
for (const file of filesExpectingMove) {
await dev.writeNoChanges(file);
const chunk = await c.getMostRecentHmrChunk();
expect(chunk).toMatch(/default:\s*(function|class)\s*MOVE/);
}
await dev.writeNoChanges("fixture7.ts");
const chunk = await c.getMostRecentHmrChunk();
expect(chunk).toMatch(/default:\s*function/);
},
});
devTest("directory cache bust case #17576", {
files: {
...reactRefreshStub,
"web/index.html": emptyHtmlFile({
styles: [],
scripts: ["index.ts", "react-refresh/runtime"],
}),
"web/index.ts": `
console.log(123);
`,
},
mainDir: "server",
async test(dev) {
await using c = await dev.client("/");
await c.expectMessage(123);
await c.expectNoWebSocketActivity(async () => {
await dev.write(
"web/Test.ts",
`
export const abc = 456;
`,
);
});
await dev.write(
"web/index.ts",
`
import { abc } from "./Test.ts";
console.log(abc);
`,
);
await c.expectMessage(456);
},
});
devTest("deleting imported file shows error then recovers", {
files: {
"index.html": emptyHtmlFile({
styles: [],
scripts: ["index.ts"],
}),
"index.ts": `
import { value } from "./other";
console.log(value);
`,
"other.ts": `
export const value = 123;
`,
"unrelated.ts": `
export const value = 123;
`,
},
async test(dev) {
await using c = await dev.client("/");
await c.expectMessage(123);
await dev.delete("other.ts", {
errors: ['index.ts:1:23: error: Could not resolve: "./other"'],
});
await c.expectReload(async () => {
await dev.write(
"other.ts",
`
export const value = 456;
`,
);
});
await c.expectMessage(456);
await c.expectNoWebSocketActivity(async () => {
await dev.delete("unrelated.ts");
});
},
});