writing-dev-server-tests
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWriting HMR/Dev Server Tests
编写HMR/Dev Server测试
Dev server tests validate hot-reloading robustness and reliability.
开发服务器测试用于验证热重载的健壮性与可靠性。
File Structure
文件结构
- - shared utilities:
test/bake/bake-harness.ts,devTest,prodTest,devAndProductionTestclass,DevclassClient - - subprocess for
test/bake/client-fixture.mjs(page loading, IPC queries)Client - - dev server and hot reload tests
test/bake/dev/*.test.ts - - tests running on both dev and production mode
test/bake/dev-and-prod.ts
- - 共享工具:
test/bake/bake-harness.ts、devTest、prodTest、devAndProductionTest类、Dev类Client - -
test/bake/client-fixture.mjs的子进程(页面加载、IPC查询)Client - - 开发服务器与热重载测试
test/bake/dev/*.test.ts - - 同时在开发和生产模式下运行的测试
test/bake/dev-and-prod.ts
Test Categories
测试分类
- - DevServer-specific bundling bugs
bundle.test.ts - - CSS bundling issues
css.test.ts - - development mode plugins
plugins.test.ts - - library compatibility (prefer concrete bugs over full package tests)
ecosystem.test.ts - - ESM features in development
esm.test.ts - - HTML file handling
html.test.ts - - React, react-refresh transform, server components
react-spa.test.ts - - source map correctness
sourcemap.test.ts
- - 针对DevServer的特定打包问题
bundle.test.ts - - CSS打包问题
css.test.ts - - 开发模式插件
plugins.test.ts - - 库兼容性测试(优先针对具体问题,而非完整包测试)
ecosystem.test.ts - - 开发模式下的ESM特性
esm.test.ts - - HTML文件处理
html.test.ts - - React、react-refresh转换、服务器组件
react-spa.test.ts - - 源映射正确性
sourcemap.test.ts
devTest Basics
devTest基础用法
ts
import { devTest, emptyHtmlFile } from "../bake-harness";
devTest("html file is watched", {
files: {
"index.html": emptyHtmlFile({
scripts: ["/script.ts"],
body: "<h1>Hello</h1>",
}),
"script.ts": `console.log("hello");`,
},
async test(dev) {
await dev.fetch("/").expect.toInclude("<h1>Hello</h1>");
await dev.patch("index.html", { find: "Hello", replace: "World" });
await dev.fetch("/").expect.toInclude("<h1>World</h1>");
await using c = await dev.client("/");
await c.expectMessage("hello");
await c.expectReload(async () => {
await dev.patch("index.html", { find: "World", replace: "Bar" });
});
await c.expectMessage("hello");
},
});ts
import { devTest, emptyHtmlFile } from "../bake-harness";
devTest("html file is watched", {
files: {
"index.html": emptyHtmlFile({
scripts: ["/script.ts"],
body: "<h1>Hello</h1>",
}),
"script.ts": `console.log("hello");`,
},
async test(dev) {
await dev.fetch("/").expect.toInclude("<h1>Hello</h1>");
await dev.patch("index.html", { find: "Hello", replace: "World" });
await dev.fetch("/").expect.toInclude("<h1>World</h1>");
await using c = await dev.client("/");
await c.expectMessage("hello");
await c.expectReload(async () => {
await dev.patch("index.html", { find: "World", replace: "Bar" });
});
await c.expectMessage("hello");
},
});Key APIs
核心API
- : Initial filesystem state
files - : HTTP requests
dev.fetch() - : Opens browser instance
dev.client() - : Filesystem mutations (wait for hot-reload automatically)
dev.write/patch/delete - : Assert console.log output
c.expectMessage() - : Wrap code that causes hard reload
c.expectReload()
Important: Use instead of - they wait for hot-reload.
dev.write/patch/deletenode:fs- : 初始文件系统状态
files - : HTTP请求
dev.fetch() - : 打开浏览器实例
dev.client() - : 文件系统变更(自动等待热重载完成)
dev.write/patch/delete - : 断言console.log输出
c.expectMessage() - : 包裹会触发硬重载的代码
c.expectReload()
重要提示:请使用而非 - 它们会自动等待热重载完成。
dev.write/patch/deletenode:fsTesting Errors
错误测试
ts
devTest("import then create", {
files: {
"index.html": `<!DOCTYPE html><html><head></head><body><script type="module" src="/script.ts"></script></body></html>`,
"script.ts": `import data from "./data"; console.log(data);`,
},
async test(dev) {
const c = await dev.client("/", {
errors: ['script.ts:1:18: error: Could not resolve: "./data"'],
});
await c.expectReload(async () => {
await dev.write("data.ts", "export default 'data';");
});
await c.expectMessage("data");
},
});Specify expected errors with the option:
errorsts
await dev.delete("other.ts", {
errors: ['index.ts:1:16: error: Could not resolve: "./other"'],
});ts
devTest("import then create", {
files: {
"index.html": `<!DOCTYPE html><html><head></head><body><script type="module" src="/script.ts"></script></body></html>`,
"script.ts": `import data from "./data"; console.log(data);`,
},
async test(dev) {
const c = await dev.client("/", {
errors: ['script.ts:1:18: error: Could not resolve: "./data"'],
});
await c.expectReload(async () => {
await dev.write("data.ts", "export default 'data';");
});
await c.expectMessage("data");
},
});通过选项指定预期错误:
errorsts
await dev.delete("other.ts", {
errors: ['index.ts:1:16: error: Could not resolve: "./other"'],
});