当规则升级时,如何批量更新快照?

解读

“快照”在国内前端团队里通常指两类产物:

  1. Jest 快照测试生成的 __snapshots__/*.snap 文件,用来固化 Stylelint 报错信息;
  2. 自研的“规则快照”——把每条规则在固定代码片段上的实际输出(行号、列号、消息文本、severity、fix 结果)序列化成 JSON 或 Markdown,作为回归测试基线。
    面试官问“批量更新”,核心想看三点:
  • 你能否精准识别“规则升级”带来的差异是预期还是缺陷;
  • 你能否用自动化脚本一次性刷新成百上千条快照,而不是让同事手动改;
  • 你能否把刷新流程沉淀到 CI,防止“谁改谁手工”的协作灾难。

知识点

  1. Stylelint 内置的 --formatter json 与 --output-file,可一次性拿到结构化结果。
  2. Jest 快照更新命令 jest -u 或 jest --updateSnapshot,但只对 expect().toMatchSnapshot() 生效;若用 eslint-formatter-stylelint 自定义测试,则需调用 jest -t stylelint-rules --updateSnapshot。
  3. PostCSS AST 的 node.source.start.line|column 在版本升级后会因插值节点变化而漂移,需用“相对偏移”而非“绝对行列”做 diff key。
  4. 国内私有 NPM 源(淘宝、华为、字节 Verdaccio)缓存可能导致“本地升级成功、CI 拉到老版本”,需在 npm i -D stylelint@latest 后 npm ls stylelint --depth=0 二次确认。
  5. 若团队用 husky + lint-staged 做提交拦截,可在 lint-staged.config.js 里加一条 { '*.{css,scss,less}': 'npm run test:stylelint:update' },实现“提交即刷新”。
  6. 快照差异大于 20% 时触发人工评审,可用 git diff --numstat | awk '{add+=$1; del+=$2} END {print (add+del)/NR}' 计算平均变动行数,超阈值则 exit 1 阻断合并。

答案

步骤一:版本确认

npm i -D stylelint@latest
npx stylelint --version > /tmp/new_ver
diff /tmp/old_ver /tmp/new_ver || echo "版本已升级"

步骤二:本地全量扫描并序列化结果

npx stylelint "src/**/*.{css,scss,less}" \
  --formatter json \
  --output-file test/stylelint-result.json

步骤三:若项目基于 Jest 快照,直接刷新

npm run test:stylelint -- -u

步骤四:若项目使用自定义 JSON 快照,写 Node 脚本批量覆盖

// scripts/update-stylelint-snapshots.js
const fs = require('fs');
const path = require('path');
const result = require('../test/stylelint-result.json');
const snapDir = path.resolve(__dirname, '../test/__stylelint_snapshots__');
if (!fs.existsSync(snapDir)) fs.mkdirSync(snapDir);
result.forEach(({ source, warnings }) => {
  const file = path.relative(process.cwd(), source);
  const snapFile = path.join(snapDir, file.replace(/\//g, '#') + '.json');
  fs.writeFileSync(snapFile, JSON.stringify(warnings, null, 2));
});

package.json 增加脚本:

"scripts": {
  "test:stylelint:update": "node scripts/update-stylelint-snapshots.js"
}

步骤五:CI 中强制同步

# .github/workflows/snapshot.yml
- name: Update snapshots
  if: contains(github.event.head_commit.message, '[update-snapshots]')
  run: |
    npm run test:stylelint:update
    git config user.name "github-actions"
    git config user.email "github-actions@xxx.com"
    git add test/__stylelint_snapshots__
    git diff --cached --quiet || (git commit -m "chore: update stylelint snapshots" && git push)

步骤六:差异评审
MR 模板里加 checklist:

  • 快照差异行数 < 20%
  • 所有 breaking change 已对照 Stylelint Release Notes 确认
    评审通过后方可合并。

拓展思考

  1. 如何防止“规则升级 + 快照更新”掩盖真正的业务样式缺陷?
    答:引入“分层快照”——把“核心设计令牌(token)文件”单独拎出来做正向快照(必须 0 错误),其余业务样式允许警告级别快照。升级后先保证核心层 0 回归,再批量刷新业务层。
  2. 大型 monorepo 子包依赖的 Stylelint 版本不一致,如何一次性刷新?
    答:在根目录用 pnpm --filter ./packages/* exec npx stylelint --version 收集实际版本,若差异大于 patch,则先 pnpm up -r stylelint,再统一执行 pnpm --filter ./packages/* run test:stylelint:update,保证快照与版本原子化对齐。
  3. 快照文件体积过大(>10 MB)导致 Git 拉取缓慢,如何优化?
    答:把快照改存到 Git LFS,或在 CI 里把快照上传到内部对象存储(如阿里云 OSS),本地只保留 snapshot.hash 文件,CI 运行时下载比对,实现“重快照、轻仓库”。