测试 in CI
运行测试 automatically on 每个推送使用 GitHub 操作, 缓存, and gates.
你将学到
- 为什么测试必须运行 in continuous integration, 不只是 locally
- 配置 GitHub 操作到 运行测试 on 每个推送 and PR
- 缓存 dependencies and sharding for 速度
- Using 测试 results as 合并 gates and 状态检查
概念讲解
CI Is Where Tests Earn Their Keep
Locally, it is easy to skip a failing test or forget to run the suite. Continuous integration (CI) runs the full suite on every push and pull request on a clean machine, so a broken test blocks the merge. This is the safety net that makes tests valuable at the team level: a passing CI run means the change is safe to merge, not just safe on your laptop.
A Basic Workflow
A GitHub Actions workflow installs dependencies with pnpm 安装 --frozen-lockfile, runs pnpm 测试 (and lint and build), and uploads the coverage report. The job runs on pull_request and 推送 to main. Set the job as a required status check in branch protection so merges are blocked while tests fail or are still running.
Speed with Caching and Sharding
CI minutes are precious. Cache the pnpm store and any build output with 操作/缓存 so installs are fast. Run tests in parallel by sharding: split the suite into N shards and run N jobs concurrently, then merge the results. Cache the Vitest transform output with dir: 'node_modules/.vite' to speed cold starts. Keep the total CI time under ten minutes so developers do not context-switch while waiting.
Gates and Quality
Beyond pass/fail, CI can enforce coverage thresholds, forbid new console.logs, and run a linter. Publish coverage to a service like Coveralls or Codecov so trends are visible. Treat the CI config as code: review changes to it, keep it in version control, and document what each job does so the team understands the gates protecting the main branch.
In Practice
Keep CI fast by caching the pnpm store and Vitest's transform output, and by sharding the suite across parallel jobs when it grows past a few hundred tests. Fail fast: run the unit suite first (seconds), and only run integration and E2E if unit passes — there is no point debugging a slow E2E failure when a unit test already caught the bug. Make the test job a required status check in branch protection so a red build blocks the merge. Publish coverage as an artifact and comment the diff on pull requests so reviewers see what changed. Periodically review the CI time: if it creeps past ten minutes, investigate sharding, caching, or pushing tests down the pyramid. A fast, trustworthy CI gate is the foundation that lets the team move quickly without breaking main.
示例代码
// scripts/ci-test.ts — orchestrate the test pipeline the way CI does.
import { execSync } from "node:child_process";
function run(label: string, cmd: string): void {
console.log(`\n=== ${label} ===`);
execSync(cmd, { stdio: "inherit", cwd: process.cwd() });
}
// Install with a frozen lockfile for reproducible builds
run("install", "pnpm install --frozen-lockfile");
// Single-pass test run (never watch mode in CI)
run("test", "pnpm test:run");
// Collect coverage and fail the build below thresholds
run("coverage", "pnpm test:run --coverage");
console.log("\nCI test pipeline complete — safe to merge.");
这个 script mirrors 什么 CI workflow 做: 安装使用 frozen lockfile, 运行测试一次 (不 in watch 模式), and collect coverage. equivalent GitHub 操作 YAML calls 这些相同 commands in steps.
动手试一试
- CI YAML Generator
Scaffold GitHub 操作 workflow 那个运行你的测试.
- YAML Validator
验证你的 workflow YAML 之前 committing 它.
常见错误
错误写法
运行测试 in watch 模式 in CI
正确做法
Use `vitest 运行` (or `jest --CI`) for 单个通过那个 exits. Watch 模式从不 terminates and hangs CI 作业直到 timeout.
错误写法
不缓存 dependencies
正确做法
缓存 pnpm 存储 and node_modules 使用操作/缓存. cold 安装 on 每个运行 wastes minutes and money.
练习 测试 in CI
3 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。