测试组织
结构测试 files, naming, and folders 所以 suite stays navigable as 它 grows.
你将学到
- Co-locating 测试使用源 对比中央测试 folders
- Naming 约定 for 测试 files and 描述块
- 分组使用描述 and tagging for selective 运行
- Scaling 测试 suite 不使用 losing navigability
概念讲解
Where Tests Live
Two conventions dominate. Co-location places foo.测试.ts next to foo.ts, making it easy to find a module's tests and keep them in sync. Centralized puts all tests under a 测试/ or __tests__/ folder, keeping source folders clean. Co-location wins for unit tests (tight coupling to the file), while a central 测试/ folder works well for integration and E2E tests that span modules. Many projects use both.
Naming Conventions
Name test files after the unit they test: cart.测试.ts for cart.ts. Name describe blocks after the unit and its public surface: 描述("Cart", () => { 描述("添加", ...) }). Name 它 blocks for the behavior and expected outcome: 它("添加项 当 cart 是空", ...). When a test fails, the full path Cart > 添加 > 添加 项 当 cart 是 空 should read like a sentence describing the broken behavior.
Grouping and Tagging
Use nested 描述 blocks to group related tests and share setup via beforeEach. For selective runs, tag tests with a comment or a custom matcher and filter by name: vitest -t "认证" runs only tests matching "认证". Some frameworks support custom annotations for @慢 or @integration so CI can run a fast subset on every push and the full suite nightly.
Scaling
As the suite grows, enforce conventions with a linter (eslint-plugin-testing-library) and a naming check in CI. Keep test files small — if a file exceeds a few hundred lines, split by describe block. Periodically prune redundant tests and consolidate overlapping ones. A well-organized suite is a map of your application's behavior; a disorganized suite is a pile no one trusts.
In Practice
Pick a convention and enforce it from day one — retrofitting organization onto a thousand tests is painful. Co-locate unit tests with their source (foo.ts → foo.测试.ts) so tests are easy to find and delete together, and keep integration and E2E tests in a central 测试/ folder with names that describe the feature (认证-flow.e2e.测试.ts). Use nested 描述 blocks to mirror the public API, and write 它 names as behavior sentences. Add a CI check that fails if a source file has no corresponding test file, so coverage stays organic. Run a periodic "测试 hygiene" session: delete tests that assert nothing, merge duplicates, and fix or remove flaky ones. A tidy suite is a trustworthy suite.
示例代码
// src/cart/cart.test.ts — co-located unit tests
import { describe, it, expect, beforeEach } from "vitest";
import { Cart } from "./cart";
describe("Cart", () => {
let cart: Cart;
beforeEach(() => (cart = new Cart()));
describe("add", () => {
it("adds an item to an empty cart", () => {
cart.add({ id: 1, price: 10 });
expect(cart.size()).toBe(1);
});
it("increases the total by the item price", () => {
cart.add({ id: 1, price: 10 });
expect(cart.total()).toBe(10);
});
});
describe("remove", () => {
it("removes an existing item", () => {
cart.add({ id: 1, price: 10 });
cart.remove(1);
expect(cart.size()).toBe(0);
});
});
});
测试 co-located 使用源. Nested 描述 mirror 公共方法, and 每个它 描述 behavior. 失败 path 读取 as sentence.
动手试一试
- Code Diff
Review 测试结构更改期间 refactors.
- JSON Formatter
读取 structured 测试 report 到导航 failures.
常见错误
错误写法
One giant 测试 file for 整个模块
正确做法
拆分通过描述块 or 通过 feature. Files over 少数 hundred 行是 困难到 导航 and 慢到 运行 in watch 模式.
错误写法
Vague 测试 names 如 '工作' or 'test1'
正确做法
Name 测试 for behavior and outcome. 完整描述 > 它 path 应该读取 as sentence 那个解释 broken behavior 当它 失败.
练习 测试组织
5 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。