Skip to content

fix: invalidate Jest transform cache when inputs change - #297

Open
nrps9909 wants to merge 1 commit into
ant-design:masterfrom
nrps9909:codex/fix-jest-transform-cache
Open

nrps9909 wants to merge 1 commit into
ant-design:masterfrom
nrps9909:codex/fix-jest-transform-cache

Conversation

@nrps9909

@nrps9909 nrps9909 commented Sep 5, 2026 •

Copy link
Copy Markdown

The Jest code preprocessor currently returns the same cache key for every file and source revision at a given tools version and LIB_DIR. With caching enabled, a second Jest run can execute stale code after a file changes; different files with the same basename can also share transformed output.

This change incorporates Babel-Jest's cache key, using the same Babel configuration and filename normalization as process, while retaining the tools-version and LIB_DIR salts and the original source path.

The new integration regression runs Jest twice against the same cache, changes a module from exporting 1 to 2, and verifies that the second run reads 2. On the original code it reads 1. Additional regressions cover source text, same-basename paths, Jest configuration and instrumentation, plus stable keys for unchanged inputs.

Validation:

  • Original implementation: five new regressions fail, including the two-process stale-source reproduction.
  • Patched implementation: full suite passes (2 suites, 7 tests).
  • npm run compile, npm run lint, tsc --noEmit, targeted Prettier and git diff --check pass.
  • Consumer verification: Ant Design's complete Checkbox suite passes twice with caching enabled using this transformer (9 suites, 78 tests including 4 independent review probes, 37 snapshots on each run).

Summary by CodeRabbit

  • Bug Fixes

    • 修复 Jest 代码转换缓存未能准确反映源文件、文件路径、配置或插桩选项变化的问题。
    • 修改源文件后,测试运行可正确加载最新内容,避免使用过期缓存。
  • Tests

    • 新增缓存键一致性及缓存失效场景的测试覆盖。

@coderabbitai

coderabbitai Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: 1045980b-edf0-4564-8843-a17692a6058d

📥 Commits

Reviewing files that changed from the base of the PR and between 967174e and db0cb6d.

📒 Files selected for processing (2)
  • src/jest/codePreprocessor.ts
  • tests/codePreprocessor.test.js

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

Jest 预处理器提取统一的转换器构建逻辑。缓存键现在包含转换器缓存键和文件路径。新增测试覆盖缓存键失效及缓存目录复用场景。

Changes

Jest 预处理器缓存流程

Layer / File(s) Summary
转换器构建与缓存键生成
src/jest/codePreprocessor.ts
新增 CacheKey 类型和 getTransformer 辅助函数。process 和 getCacheKey 共用转换器构建逻辑。缓存键现在包含转换器缓存键、文件名和文件路径。
缓存失效与缓存复用验证
tests/codePreprocessor.test.js
测试验证相同输入生成相同缓存键。源代码、路径、Jest 配置或插桩选项变化时,缓存键会失效。端到端测试验证复用缓存目录后加载修改后的源文件。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to db0cb

This updates Jest transform cache invalidation so changed source, paths, and transform settings produce fresh output while unchanged inputs retain stable keys. The included regression coverage supports merge readiness.

Sequence Diagram(s)

sequenceDiagram
  participant Jest
  participant Preprocessor
  participant getTransformer
  participant BabelTransformer
  Jest->>Preprocessor: process(src, filePath, options)
  Preprocessor->>getTransformer: 构建转换器和规范化文件名
  getTransformer-->>Preprocessor: transformer, filename
  Preprocessor->>BabelTransformer: transform(src, filename, options)
  BabelTransformer-->>Jest: 转换后的代码
  Jest->>Preprocessor: getCacheKey(src, filePath, options)
  Preprocessor->>BabelTransformer: getCacheKey(src, filename, options)
  BabelTransformer-->>Preprocessor: transformer cache key
  Preprocessor-->>Jest: MD5(cache key + filePath)
Loading

Poem

小兔携来缓存钥匙亮,
源码变动,旧影不再藏。
路径配置同入签名,
Babel 轻跳换新装。
两次运行皆读新光。

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题“fix: invalidate Jest transform cache when inputs change”准确概括了本次变更的主要目的:当转换输入发生变化时,使 Jest 转换缓存失效。标题简洁、具体,并与代码和测试变更一致。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Some tools did not complete. Review the errors below.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

tests/codePreprocessor.test.js

Parsing error: "parserOptions.project" has been provided for @typescript-eslint/parser.
The file was not found in any of the provided project(s): tests/codePreprocessor.test.js


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant