升级 Jest 29→30 时,如何一键迁移旧版配置项?

解读

国内前端团队普遍使用 Jest 做单元/集成测试,升级时最怕“配置爆炸”:旧项目里往往有 jest.config.js、package.json 的 jest 字段、babel-jest、ts-jest、eslint-plugin-jest 等一堆关联配置。Jest 30 把部分默认行为、快照格式、环境隔离策略都做了 Breaking Change,如果手动比对 Release Note 逐项改,不仅耗时,还容易漏掉隐藏差异,导致 CI 直接挂掉。面试官问“一键迁移”,表面看是问工具链,实质在考察三点:

  1. 你对 Jest 30 Breaking Change 的敏感程度;
  2. 你是否具备“自动化迁移 > 手工比对”的工程化思维;
  3. 你是否能把迁移脚本落地到国内常见的 Monorepo + pnpm + TypeScript 场景,并给出回滚预案。

知识点

  1. jest-codemods:官方维护的 jscodeshift 插件集,内置 29→30 的 transform,可批量改写 jest.config.js、package.json、测试文件里的过时 API。
  2. jest-upgrade-config:社区提供的单行命令封装,内部调用 jest-codemods,并自动拉取 Jest 30 的默认 preset 做 diff-merge,支持 —dry-run 预览。
  3. Jest 30 主要 Breaking Change:
    • 默认 testEnvironment 由 jsdom 变为 node,导致依赖 DOM 的用例挂掉;
    • 快照格式去掉“@jest-environment”注释,序列化器版本号升级;
    • globals 开关默认 false,必须显式 inject 才能用 describe/it 全局;
    • fakeTimers 默认 modern,旧用法 jest.useFakeTimers(‘legacy’) 被移除;
    • 覆盖率 provider 默认 v8,istanbul 需手动指定。
  4. 国内常见配套:
    • ts-jest 29→30 需同步升级,否则出现“jest-30 找不到 ts-jest/utils”报错;
    • pnpm workspace 下需加 —workspace-root 参数,让 codemods 识别根配置;
    • GitLab CI 里需同步升级 jest-junit reporter 版本,否则解析失败导致流水线红色。
  5. 回滚策略:在 package.json 里先写死 “jest”: “^29” 的 resolutions,迁移脚本跑通后再整体升到 30;同时把 jest.config.js 备份为 jest.config.js.bak,CI 加“迁移后回归用例”job,一旦失败自动 git checkout 回退。

答案

步骤如下,真正做到“一键”且可落地国内工程:

  1. 前置锁定
    pnpm add -D jest@29 jest-codemods@latest ts-jest@29
    git add . && git commit -m “chore: lock v29 before migration”

  2. 一键迁移
    npx jest-upgrade-config —from=29 —to=30 —dry-run
    确认 diff 无误后去掉 —dry-run,正式执行:
    npx jest-upgrade-config —from=29 —to=30
    该命令内部完成:
    • 调用 jest-codemods 把 jest.config.js 里的 testEnvironment、globals、timers 等字段自动改写;
    • 把 package.json 中的 jest 字段合并进 jest.config.js,避免双配置;
    • 扫描所有 .test. 文件,替换掉废弃的 jest.genMockFromModule() 写法;
    • 生成 jest.config.mjs 并保留旧文件备份。

  3. 同步升级依赖
    pnpm add -D jest@30 ts-jest@30 @types/jest@30 jest-junit@16
    pnpm dedupe

  4. 差异化校验
    新增脚本 “test:migrate”: “jest —listTests | head -5 && jest —clearCache && jest —coverage —changedSince=origin/main”
    在本地与 CI 双跑,确保快照通过、覆盖率不掉、无 console.error。

  5. 回滚预案
    在 GitLab CI 中加 rollback job:
    if [ $CI_JOB_STATUS == "failed" ]; then
    git checkout HEAD~1 — jest.config.js package.json pnpm-lock.yaml
    pnpm i —frozen-lockfile
    exit 1
    fi
    实现“红色即回滚”,保障主干永远可发布。

拓展思考

  1. 如果公司 Monorepo 有 20+ 子包,每个子包配置风格不一,可把 jest-upgrade-config 封装成内部 CLI:
    #!/usr/bin/env node
    const { execSync } = require('child_process');
    const packages = JSON.parse(execSync('pnpm ls —depth -1 —json', {encoding:'utf8'}));
    packages.forEach(p => {
    process.chdir(p.path);
    execSync('npx jest-upgrade-config —from=29 —to=30 —silent', {stdio:'inherit'});
    });
    并在 Rush/Changesets 的 version 钩子中自动触发,实现“批量零感知”升级。
  2. 对于老项目里大量手写 jest.mock(‘xxx’) 但依赖提升路径变化导致找不到模块的情况,可在迁移脚本后追加自定义 codemod:
    jscodeshift -t scripts/codemods/fix-pnpm-mock-paths.js —parser=tsx src
    把 mock 路径自动补全 .pnpm 虚拟 store 的完整地址,解决“升级后 mock 失效”的隐形坑。
  3. 未来 Jest 31 若继续 Breaking,可把本次迁移脚本沉淀为 GitHub Action,结合 Renovate 的 PR 自动触发,实现“依赖升级 → 配置迁移 → 回归验证”全链路无人值守,真正把“一键迁移”做成持续交付的常态能力。