用途
该技能用于指导编写 Bun 打包器的测试。测试位于 test/bundler/ 目录,基于 test/bundler/expectBundled.ts 中的 itBundled() 辅助函数,用于验证打包器、转译器或代码转换行为。
主要能力
- 声明式定义测试输入文件、入口点与运行时文件。
- 配置打包选项:outfile、outdir、format(esm/cjs/iife)、target(bun/browser/node)、压缩选项(minifyWhitespace、minifyIdentifiers、minifySyntax)、banner、footer、define、external、sourceMap、splitting、treeShaking、drop 等。
- 运行时验证:stdout(精确匹配、正则、子串)、stderr、exitCode、env、runtime(bun/node)以及运行时错误。
- 验证打包错误与警告(bundleErrors、bundleWarnings)。
- 死代码消除(DCE)验证:通过源码中的 KEEP/REMOVE 标记与 dceKeepMarkerCount 计数。
- 使用 capture() 验证精确转译结果。
- 通过 onAfterBundle 回调做打包后断言,如 expectFile、assertFileExists、readFile、toMatchSnapshot、captureFile。
使用方式
基本用法示例:
import { describe } from "bun:test";
import { itBundled, dedent } from "./expectBundled";
describe("bundler", () => {
itBundled("category/TestName", {
files: {
"index.js": `console.log("hello");`,
},
run: {
stdout: "hello",
},
});
});
测试 ID 格式为 category/TestName,例如 banner/CommentBanner、minify/Empty。
文件设置支持 files、entryPoints(默认第一个文件)、runtimeFiles(打包后写入)。
运行测试命令:
bun bd test test/bundler/bundler_banner.test.ts
BUN_BUNDLER_TEST_FILTER="banner/Comment" bun bd test bundler_banner.test.ts
BUN_BUNDLER_TEST_DEBUG=1 bun bd test bundler_minify.test.ts
适用场景
- 创建或修改打包器、转译器、代码转换相关测试。
- 验证打包输出内容、运行时行为、错误与警告。
- 验证死代码消除、字符串折叠等转换结果。
- 多文件 CJS/ESM 互操作测试。
限制
资料未提供该技能的具体安装方式、收费模式、支持平台范围或性能效果等信息。测试文件路径为相对路径(如 /index.js),需使用 dedent 编写可读的多行代码,复杂输出建议使用 .toMatchSnapshot(),多场景可通过向 run 传数组实现。