test(taro-h5): jest to vitest
这个 PR 做了什么? (简要描述所做更改)
这个 PR 是什么类型? (至少选择一个)
- [ ] 错误修复 (Bugfix) issue: fix #
- [ ] 新功能 (Feature)
- [ ] 代码重构 (Refactor)
- [ ] TypeScript 类型定义修改 (Types)
- [ ] 文档修改 (Docs)
- [ ] 代码风格更新 (Code style update)
- [x] 测试更新
- [ ] 构建优化 (Chore)
- [ ] 其他,请描述 (Other, please describe):
这个 PR 涉及以下平台:
- [ ] 所有平台
- [x] Web 端(H5)
- [ ] 移动端(React-Native)
- [ ] 鸿蒙(Harmony)
- [ ] 鸿蒙容器(Harmony Hybrid)
- [ ] ASCF 元服务
- [ ] 快应用(QuickApp)
- [ ] 所有小程序
- [ ] 微信小程序
- [ ] 企业微信小程序
- [ ] 京东小程序
- [ ] 百度小程序
- [ ] 支付宝小程序
- [ ] 支付宝 IOT 小程序
- [ ] 钉钉小程序
- [ ] QQ 小程序
- [ ] 飞书小程序
- [ ] 快手小程序
- [ ] 头条小程序
Summary by CodeRabbit
-
Chores(杂项)
- 将测试框架从 Jest 切换为 Vitest,更新测试脚本与相关开发依赖,移除旧的 Jest 配置并新增 Vitest 配置
- 若干包的 package.json 增加/调整 exports 字段以改善模块导出兼容性
- 更新测试环境初始化与构建相关工具链配置
-
Tests(测试)
- 大量测试用例迁移至 Vitest:替换 mocking API、改为 Promise/async 风格、统一控制台与环境 mock 处理,保持现有功能断言不变
✏️ Tip: You can customize this high-level summary in your review settings.
[!NOTE]
Other AI code review bot(s) detected
CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.
Walkthrough
将 packages/taro-h5 包的测试框架从 Jest 迁移至 Vitest:新增 Vitest 配置并移除 Jest 配置,批量更新测试文件将 Jest API 替换为 Vitest(vi),调整 package.json 测试脚本与 devDependencies,新增/修改若干 package.json exports 与 mocks,测试用例异步风格从 done 回调迁移为 Promise/async/await。
Changes
| 内聚合 / 文件(s) | 变更摘要 |
|---|---|
配置迁移 packages/taro-h5/jest.config.ts, packages/taro-h5/vitest.config.mts |
删除 Jest 配置文件;新增 vitest.config.mts(jsdom 环境、setupFiles、别名 @tarojs/taro→src/index.ts、coverage 设置)。 |
包配置 / 依赖 packages/taro-h5/package.json, packages/taro-api/package.json, packages/taro-runtime/package.json |
将测试脚本由 Jest 切换为 Vitest(scripts);移除/替换 Jest 相关依赖并添加 jsdom、rollup 插件、rollup-plugin-dts 等;在 taro-api 与 taro-runtime 中新增 exports 字段。 |
全局 mock / 环境 packages/taro-h5/__mocks__/setEnv.ts, packages/taro-h5/__mocks__/platform.ts |
在 setEnv.ts 中新增 @testing-library/jest-dom/vitest 导入以适配 Vitest;删除 __mocks__/platform.ts 的默认导出。 |
测试运行时/工具 packages/taro-h5/__tests__/utils.ts |
将测试辅助运行时从真实 runtime 切换为本地 mock (../__mocks__/taro-framework)。 |
基础测试迁移 packages/taro-h5/__tests__/base/* .../network.test.ts, .../system.test.ts, .../index.test.ts, .../pxTransform.test.ts |
将 Jest globals/断言迁移为 Vitest(import { describe, expect, test } from 'vitest');jest.fn()→vi.fn();异步测试由 done 改为 Promise/async;Taro 引用改为本地 ../../src/index。 |
网络相关测试 packages/taro-h5/__tests__/network/* request.test.ts, websocket.test.ts |
用 Vitest(vi)替换 Jest mocks;用本地 vi mock 替代 jest-fetch-mock(mockFetchOnce/mockFetchReject);增强 AbortSignal 模拟;WebSocket 使用 mock-socket,改为 Promise 驱动并增加显式清理。 |
存储与定位测试 packages/taro-h5/__tests__/storage/*, packages/taro-h5/__tests__/location/* |
jest.fn→vi.fn;用 vi.spyOn(console, ...) 取代 jest-mock-console;新增 afterEach 调用 vi.restoreAllMocks();更新 Taro 引用路径。 |
UI 交互与组件测试 packages/taro-h5/__tests__/ui/interaction/*, .../navigation.test.ts, .../tab-bar.test.tsx, .../animation.test.ts |
迁移为 Vitest,替换 mocks,异步风格改为 async/await/Promise;若干断言(如颜色)改为 rgb 表示;更新引用路径。 |
WXML / DOM 查询测试 packages/taro-h5/__tests__/wxml/index.test.ts |
jest.resetAllMocks()→vi.resetAllMocks();将 callback(done) 测试重写为 Promise,使用 selectorQuery/exec 回调在 Promise 中 resolve。 |
Estimated code review effort
🎯 3 (中等) | ⏱️ ~25 分钟
- 重点复核:
packages/taro-h5/__tests__/network/websocket.test.ts(并发、mock-socket 行为与资源清理)packages/taro-h5/__tests__/network/request.test.ts(fetch/AbortSignal 模拟与错误传播)packages/taro-h5/vitest.config.mts(alias、setupFiles 与包含模式是否覆盖所有测试)packages/taro-h5/__mocks__/setEnv.ts(导入的测试库与 Vitest 兼容性)
Possibly related PRs
- NervJS/taro#18607 — 同样在项目中进行 Jest → Vitest 的迁移,调整配置与测试文件。
- NervJS/taro#18147 — 与本次迁移类似,更新测试框架与相关测试适配。
- NervJS/taro#17810 — 涉及将测试和配置迁移到 Vitest 并修改测试依赖,代码级相关性强。
Suggested reviewers
- luckyadam
- yoyo837
- tutuxxx
Poem
"我是一只爱跑测试的兔,
把旧 Jest 背包轻放树下,
vi.fn 在晨露里闪亮,Promise 舞成弧线,
新配置里我蹦又跳,代码田间闻胡萝卜香 🐇🥕"
Pre-merge checks and finishing touches
❌ 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%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | 标题清晰准确地反映了PR的主要变更:从Jest迁移到Vitest进行测试。 |
✨ Finishing touches
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
📜 Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 9502d07ba00dc3b841efb5440141b5e7961f8f8e and 07d9622195d071afc1998be075a192417bd23a01.
📒 Files selected for processing (1)
packages/taro-runtime/package.json(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/taro-runtime/package.json
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
- GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
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.
Comment @coderabbitai help to get the list of available commands and usage tips.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 50.71%. Comparing base (ef4a0b2) to head (842d1d9).
:x: Your project check has failed because the head coverage (50.71%) is below the target coverage (75.00%). You can increase the head coverage or adjust the target coverage.
Additional details and impacted files
@@ Coverage Diff @@
## main #18668 +/- ##
==========================================
- Coverage 52.63% 50.71% -1.93%
==========================================
Files 463 244 -219
Lines 25108 11258 -13850
Branches 6629 2703 -3926
==========================================
- Hits 13215 5709 -7506
+ Misses 9726 4407 -5319
+ Partials 2167 1142 -1025
| Flag | Coverage Δ | |
|---|---|---|
| taro-cli | 72.69% <ø> (-0.17%) |
:arrow_down: |
| taro-runtime | 60.33% <ø> (ø) |
|
| taro-web | 46.74% <ø> (-6.38%) |
:arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more. see 362 files with indirect coverage changes
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.