-
Notifications
You must be signed in to change notification settings - Fork 4.9k
test(taro-runtime): vitest #18056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
test(taro-runtime): vitest #18056
Conversation
Walkthrough本次变更将 taro-runtime 的测试框架从 Jest 迁移到 Vitest;重构并新增/移动多份测试为 ESM/Vitest 风格;修复大量拼写错误(CONTEXT_ACTIONS.DESTORY → CONTEXT_ACTIONS.DESTROY)并同步引用;导出 TaroWindow;重命名 inner-html 扫描器 Scaner → Scanner;调整相关配置与文档链接。 Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Page as 页面/组件
participant PageCfg as Page Config [ONUNLOAD]
participant Win as TaroWindow
participant Loc as TaroLocation
participant His as TaroHistory
Note over PageCfg,Win: 卸载流程(已修正为 CONTEXT_ACTIONS.DESTROY)
PageCfg->>Win: trigger(CONTEXT_ACTIONS.DESTROY, pageId)
Win->>Loc: trigger(CONTEXT_ACTIONS.DESTROY, pageId)
Win->>His: trigger(CONTEXT_ACTIONS.DESTROY, pageId)
Loc-->>Loc: 删除 pageId 对应的 Location 缓存
His-->>His: 删除 pageId 对应的 History 缓存
Win-->>PageCfg: 卸载完成通知
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (2)
packages/taro-runtime/package.json (1)
25-26
: 脚本切换到 Vitest ✅
脚本已对齐新框架,参数简单明了。可考虑再加一个test:watch
方便本地调试:"test:watch": "vitest"packages/taro-runtime/tests/style.spec.ts (1)
11-13
: 环境变量清理 OK,但建议使用 Vitest API
可用afterAll
的别名vi.afterAll
,避免同时引入多个同名函数后混淆。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
pnpm-lock.yaml
is excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (19)
packages/taro-runtime/jest.config.js
(0 hunks)packages/taro-runtime/package.json
(1 hunks)packages/taro-runtime/src/__tests__/event.spec.js
(0 hunks)packages/taro-runtime/src/__tests__/mutation.spec.js
(0 hunks)packages/taro-runtime/src/__tests__/react.spec.js
(0 hunks)packages/taro-runtime/tests/class.spec.ts
(6 hunks)packages/taro-runtime/tests/dom.spec.ts
(13 hunks)packages/taro-runtime/tests/event.spec.ts
(1 hunks)packages/taro-runtime/tests/eventSource.spec.ts
(4 hunks)packages/taro-runtime/tests/exports.spec.ts
(2 hunks)packages/taro-runtime/tests/html.spec.ts
(16 hunks)packages/taro-runtime/tests/location.spec.ts
(14 hunks)packages/taro-runtime/tests/mutation.spec.ts
(1 hunks)packages/taro-runtime/tests/react.spec.ts
(1 hunks)packages/taro-runtime/tests/style.spec.ts
(1 hunks)packages/taro-runtime/tests/utils.ts
(1 hunks)packages/taro-runtime/tsconfig.json
(1 hunks)packages/taro-runtime/tsconfig.test.json
(0 hunks)packages/taro-runtime/vitest.config.ts
(1 hunks)
🧠 Learnings (14)
📓 Common learnings
Learnt from: ianzone
PR: NervJS/taro#17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 __tests__ 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-runtime/tsconfig.json (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/package.json (2)
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-runtime/tests/style.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/class.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/exports.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/vitest.config.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/react.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/html.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/eventSource.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/dom.spec.ts (3)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
Learnt from: Single-Dancer
PR: #17653
File: packages/taro-components-advanced/src/components/water-flow/node.ts:83-95
Timestamp: 2025-05-06T06:55:44.077Z
Learning: 在 Taro 的 getRectSizeSync 函数中,如果找不到指定的节点,函数会一直处于 pending 状态而不是抛出错误。在这种情况下,使用 try/catch 处理不够充分,需要添加超时处理如 Promise.race 来避免无限等待。
packages/taro-runtime/tests/location.spec.ts (2)
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-runtime/tests/event.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/mutation.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
💤 Files with no reviewable changes (5)
- packages/taro-runtime/tsconfig.test.json
- packages/taro-runtime/src/tests/event.spec.js
- packages/taro-runtime/jest.config.js
- packages/taro-runtime/src/tests/react.spec.js
- packages/taro-runtime/src/tests/mutation.spec.js
🧰 Additional context used
🧠 Learnings (14)
📓 Common learnings
Learnt from: ianzone
PR: NervJS/taro#17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 __tests__ 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-runtime/tsconfig.json (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/package.json (2)
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-runtime/tests/style.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/class.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/exports.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/vitest.config.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/react.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/html.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/eventSource.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/dom.spec.ts (3)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
Learnt from: Single-Dancer
PR: #17653
File: packages/taro-components-advanced/src/components/water-flow/node.ts:83-95
Timestamp: 2025-05-06T06:55:44.077Z
Learning: 在 Taro 的 getRectSizeSync 函数中,如果找不到指定的节点,函数会一直处于 pending 状态而不是抛出错误。在这种情况下,使用 try/catch 处理不够充分,需要添加超时处理如 Promise.race 来避免无限等待。
packages/taro-runtime/tests/location.spec.ts (2)
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
packages/taro-runtime/tests/event.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
packages/taro-runtime/tests/mutation.spec.ts (2)
Learnt from: ianzone
PR: #17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。
Learnt from: ianzone
PR: #17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 tests 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
⏰ 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). (6)
- GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
- GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-musl
- GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
- GitHub Check: Build Rust WASM / stable - wasm32-wasi
- GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
- GitHub Check: Build Rust Binding / stable - aarch64-apple-darwin
🔇 Additional comments (38)
packages/taro-runtime/tests/utils.ts (1)
2-2
: 👍 显式声明Promise<void>
很好
补充返回类型让 TS 推导更准确,符合 “self-documenting code” 的理念。无其他问题。packages/taro-runtime/tests/class.spec.ts (1)
10-10
: 导航器注释掉是否仍需?
若 runtime 内部有对navigator
的访问,Vitest 将抛出ReferenceError
。请确认后决定保留或删除该注释。packages/taro-runtime/tests/style.spec.ts (1)
1-4
: 测试文件已 ESM 化 👍
引入方式统一为 ESM,提高一致性,无明显问题。packages/taro-runtime/tests/exports.spec.ts (3)
1-1
: LGTM! 正确的 Vitest 导入语法使用显式导入的方式从
vitest
导入测试函数,这是推荐的做法。
3-3
: LGTM! 改用源码导入路径从
../src/index
直接导入源码而不是构建后的分发版本,这在测试环境中是合适的做法。
10-10
: LGTM! 统一使用test
函数将
it
更改为test
以保持与 Vitest API 的一致性,这是正确的迁移做法。Also applies to: 19-19, 25-25, 30-30
packages/taro-runtime/tests/eventSource.spec.ts (4)
1-3
: LGTM! 正确的 Vitest 迁移显式导入 Vitest 测试工具并改用源码导入路径,符合迁移最佳实践。
8-8
: LGTM! 提高代码清晰度显式赋值
document
变量提高了代码的可读性和明确性。
20-20
: LGTM! 函数增强为
createDiv
函数添加可选的类型化id
参数,这是一个很好的改进。
29-29
: LGTM! 统一测试函数命名将测试函数从
it
更改为test
以保持与 Vitest API 的一致性。Also applies to: 53-53
packages/taro-runtime/tests/html.spec.ts (5)
1-9
: LGTM! 完整的 Vitest 迁移导入所有导入语句都已正确更新为使用源码路径和显式 Vitest 函数导入,迁移工作完整。
17-17
: LGTM! 显式传递 document 参数明确传递
document
对象到 parser 函数,提高了代码的清晰度。
22-22
: LGTM! 统一测试函数命名所有测试用例都已从
it
更改为test
,与 Vitest API 保持一致。Also applies to: 42-42, 62-62, 93-93, 115-115, 137-137, 151-151, 171-171, 216-216, 253-253, 285-285, 317-317, 341-341, 347-347, 356-356, 379-379
327-327
: LGTM! 测试内容微调将换行符替换为空格的内容调整是合理的测试数据修改。
Also applies to: 336-336
380-380
: LGTM! 参数类型改进为
transformText
函数的参数添加类型注解,提高了代码质量。packages/taro-runtime/tests/react.spec.ts (3)
1-3
: LGTM! 正确的 Vitest 导入设置使用了完整的 Vitest 测试工具导入,包括
vi
用于模拟函数,这是正确的设置。
5-11
: LGTM! 合适的测试环境设置正确设置了 React 框架环境变量,并在测试结束后进行了清理,这是良好的测试实践。
13-25
: LGTM! 完整的事件处理测试测试覆盖了事件创建、监听器注册和事件分发的完整流程,验证逻辑正确且全面。
packages/taro-runtime/vitest.config.ts (3)
1-3
: LGTM! 正确的配置文件导入导入了必要的 path 工具和 Vitest 配置定义函数,设置正确。
6-14
: LGTM! 完整的功能特性标志配置定义了所有必要的 DOM 相关功能特性标志,确保运行时功能的完整性,这些配置对于 taro-runtime 的正常工作至关重要。
15-22
: LGTM! 完整的测试配置测试文件匹配模式、代码覆盖率配置和设置文件路径都配置正确,为 Vitest 测试提供了完整的运行环境。
packages/taro-runtime/tests/dom.spec.ts (5)
1-1
: 迁移到 Vitest 框架的导入设置正确正确地导入了 Vitest 的测试函数,替换了 Jest 的隐式全局变量。
3-3
: 运行时导入路径更新合理从构建后的分发版本改为直接从源代码导入,这在测试环境中更加合适。
17-226
: 测试方法名称统一更新所有测试用例都正确地从
it
更新为test
,与 Vitest 约定保持一致。
132-132
: 添加可选链操作符提高代码健壮性在
insertAdjacentHTML
测试中添加了可选链操作符,这是一个很好的改进,可以防止在节点可能为 undefined 时出现运行时错误。Also applies to: 135-138, 147-148
8-10
: 确认:在 dom.spec.ts 中移除 global.navigator 不会影响现有测试在 dom.spec.ts 的用例中,并未直接访问或依赖全局的
navigator
;所有 DOM 操作测试均基于从src/index
导出的window
和document
实例。
而对navigator
的验证在exports.spec.ts
中通过:
runtime.window.navigator
和runtime.navigator
均已从bom/navigator
正常导出并在测试中校验,不依赖global.navigator
。结论:可安全保留注释,所有当前用例均能通过。
packages/taro-runtime/tests/location.spec.ts (6)
1-3
: Vitest 迁移设置正确正确地从 Jest 迁移到 Vitest,包括导入语句和运行时模块路径的更新。
6-516
: 测试方法名称和模拟函数更新完整所有测试用例都正确地从
it
更新为test
,模拟函数从jest.fn()
更新为vi.fn()
,保持了 Vitest 框架的一致性。
121-121
: 错误处理类型转换改进在 URL 构造函数测试中添加了错误类型转换
(error as Error)
,这提高了 TypeScript 类型安全性。Also applies to: 129-129, 137-137, 145-145
171-178
: URL 构造函数测试参数调整将 URL 对象改为字符串作为 base 参数,这是一个合理的简化。
239-285
: Router 模拟对象属性增强在
Current.router
模拟对象中添加了$taroPath
、onReady
、onHide
、onShow
属性,使模拟对象更完整。
397-397
: 缓存访问添加可选链在访问缓存条目时添加了可选链操作符,提高了代码的健壮性。
Also applies to: 501-502
packages/taro-runtime/tests/event.spec.ts (2)
1-11
: 新事件测试套件设置完善正确使用 Vitest 框架,导入了必要的测试函数和运行时模块。测试环境清理设置合理。
12-245
: 事件系统测试覆盖全面测试套件全面覆盖了事件系统的关键功能:
- 事件监听器的添加、移除和触发
- 事件冒泡和阻止传播
- 多个处理程序和一次性选项
- 默认行为阻止
- 立即停止传播
测试用例结构清晰,使用了适当的中文描述,mock 函数使用一致。
packages/taro-runtime/tests/mutation.spec.ts (4)
1-23
: MutationObserver 测试套件设置规范测试套件正确使用了 Vitest 框架,生命周期钩子设置合理,包括观察器的初始化和清理。
24-91
: appendChild 变异测试覆盖完整测试用例全面覆盖了 appendChild 操作的变异观察,包括首个节点、兄弟节点和深层树结构的情况,断言结构完整。
93-169
: replaceChild 变异测试场景全面测试用例涵盖了 replaceChild 操作的各种场景,包括单节点和多节点的替换,正确验证了变异记录的结构。
171-284
: removeChild 变异测试逻辑严谨测试用例详细验证了 removeChild 操作的变异观察,包括单个节点、多个兄弟节点和深层结构的移除,异步测试模式使用正确。
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #18056 +/- ##
==========================================
+ Coverage 55.06% 55.50% +0.44%
==========================================
Files 416 447 +31
Lines 21563 23333 +1770
Branches 5267 5790 +523
==========================================
+ Hits 11873 12952 +1079
- Misses 8032 8518 +486
- Partials 1658 1863 +205
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
@yoyo837 辛苦合一下哈 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 9
🧹 Nitpick comments (3)
packages/taro-runtime/tests/event.spec.ts (3)
230-246
: 移除未使用的 spy本用例仅验证“无监听不报错/不触发”,这两个 spy 未被注册且仅用于断言 0 次,属于冗余。
- const containerSpy = vi.fn() - const divSpy = vi.fn() @@ - expect(divSpy).toBeCalledTimes(0) - expect(containerSpy).toBeCalledTimes(0) + // 无监听场景,不应抛错且不会触发任何回调
549-557
: 无效的 batchedEvents 逻辑可移除以简化用例
batchedEvents
从未被填充,if 块恒不生效。- const batchedEvents: CallableFunction[] = [] @@ - if (name === 'batchedEventUpdates') { - const fn = args[0] - if (batchedEvents.length > 0) { - batchedEvents.forEach(f => f()) - batchedEvents.length = 0 - } - fn() - return - } + if (name === 'batchedEventUpdates') { + const fn = args[0] + fn() + return + }
17-17
: 第三参使用 null 可读性/类型不佳多数 addEventListener 第三参传入 null。为贴合 TS 类型(boolean | AddEventListenerOptions),建议省略或传 undefined。
Also applies to: 46-46, 63-63, 80-80, 100-101, 130-131, 155-156, 174-179, 194-199, 214-219
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/taro-runtime/tests/event.spec.ts
(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: ianzone
PR: NervJS/taro#18146
File: packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/package.json:12-14
Timestamp: 2025-08-08T02:32:58.265Z
Learning: 在 Taro 项目的 pnpm 工作区中,Vitest 相关依赖(vitest 和 vitest/coverage-istanbul)被管理在根目录的 package.json 中,而不是各个子包的 devDependencies 中。这是 monorepo 中依赖提升的标准做法。
⏰ 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). (5)
- GitHub Check: Build Rust Binding / stable - aarch64-apple-darwin
- GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
- GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
- GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
- GitHub Check: Build Rust WASM / stable - wasm32-wasi
@@ -0,0 +1,613 @@ | |||
import { afterAll, describe, expect, test, vi } from 'vitest' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
统一增加 afterEach 自动恢复 spy/mock
引入 afterEach 并调用 vi.restoreAllMocks,避免遗漏恢复。
-import { afterAll, describe, expect, test, vi } from 'vitest'
+import { afterAll, afterEach, describe, expect, test, vi } from 'vitest'
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In packages/taro-runtime/tests/event.spec.ts around line 1, the test file
imports afterAll but lacks afterEach to auto-restore spies/mocks; add afterEach
to the import list and add a top-level afterEach(() => vi.restoreAllMocks()) so
all vitest spies/mocks are restored after each test to avoid leaks and
flakiness.
const document = runtime.document | ||
|
||
afterAll(() => { | ||
process.env.FRAMEWORK = '' | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
环境变量还原不安全,可能污染其它用例
afterAll 里直接将 FRAMEWORK 置空,未恢复原值。请在 describe 内保存原值并恢复,避免跨测试污染。
const document = runtime.document
+ const FRAMEWORK_ORIG = process.env.FRAMEWORK
afterAll(() => {
- process.env.FRAMEWORK = ''
+ process.env.FRAMEWORK = FRAMEWORK_ORIG
})
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const document = runtime.document | |
afterAll(() => { | |
process.env.FRAMEWORK = '' | |
}) | |
const document = runtime.document | |
const FRAMEWORK_ORIG = process.env.FRAMEWORK | |
afterAll(() => { | |
process.env.FRAMEWORK = FRAMEWORK_ORIG | |
}) |
🤖 Prompt for AI Agents
In packages/taro-runtime/tests/event.spec.ts around lines 8 to 12, the afterAll
handler unconditionally sets process.env.FRAMEWORK = '' which can leak into
other tests; capture the original value at the start of the describe (e.g.,
const originalFramework = process.env.FRAMEWORK) and then in afterAll restore it
(process.env.FRAMEWORK = originalFramework), ensuring the environment is
returned to its prior state rather than always clearing it.
afterAll(() => { | ||
process.env.FRAMEWORK = '' | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
配合添加 afterEach 恢复所有 mock
在同一 describe 中增加 afterEach,确保每个用例后清理 spy。
afterAll(() => {
process.env.FRAMEWORK = FRAMEWORK_ORIG
})
+
+ afterEach(() => {
+ vi.restoreAllMocks()
+ })
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In packages/taro-runtime/tests/event.spec.ts around lines 10 to 12, add an
afterEach within the same describe to restore/clear all spies and mocks after
each test; specifically call jest.restoreAllMocks() and jest.clearAllMocks()
(and optionally jest.resetModules() if needed) inside that afterEach so each
test runs with a clean mock state, while keeping the existing afterAll that
resets process.env.FRAMEWORK.
test('can addEventListener', () => { | ||
const div = document.createElement('div') | ||
const spy = vi.fn() | ||
div.addEventListener('tap', spy, null) | ||
const event = runtime.createEvent({ | ||
type: 'tap', | ||
detail: {}, | ||
target: { dataset: {}, id: '' }, | ||
currentTarget: { dataset: {}, id: '' } | ||
}, div) | ||
// mini program event system will do this for us | ||
div.dispatchEvent(event) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用例未断言结果,存在“假阳性”风险
应验证监听器是否被触发一次。
test('can addEventListener', () => {
const div = document.createElement('div')
const spy = vi.fn()
- div.addEventListener('tap', spy, null)
+ div.addEventListener('tap', spy)
const event = runtime.createEvent({
type: 'tap',
detail: {},
target: { dataset: {}, id: '' },
currentTarget: { dataset: {}, id: '' }
}, div)
// mini program event system will do this for us
div.dispatchEvent(event)
+ expect(spy).toHaveBeenCalledTimes(1)
})
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
test('can addEventListener', () => { | |
const div = document.createElement('div') | |
const spy = vi.fn() | |
div.addEventListener('tap', spy, null) | |
const event = runtime.createEvent({ | |
type: 'tap', | |
detail: {}, | |
target: { dataset: {}, id: '' }, | |
currentTarget: { dataset: {}, id: '' } | |
}, div) | |
// mini program event system will do this for us | |
div.dispatchEvent(event) | |
}) | |
test('can addEventListener', () => { | |
const div = document.createElement('div') | |
const spy = vi.fn() | |
div.addEventListener('tap', spy) | |
const event = runtime.createEvent({ | |
type: 'tap', | |
detail: {}, | |
target: { dataset: {}, id: '' }, | |
currentTarget: { dataset: {}, id: '' } | |
}, div) | |
// mini program event system will do this for us | |
div.dispatchEvent(event) | |
expect(spy).toHaveBeenCalledTimes(1) | |
}) |
🤖 Prompt for AI Agents
In packages/taro-runtime/tests/event.spec.ts around lines 14 to 26, the test
registers a listener and dispatches an event but has no assertions, causing a
false-positive; update the test to assert that the spy was called exactly once
(e.g., using expect(spy).toHaveBeenCalledTimes(1) or equivalent) after
dispatchEvent to verify the listener was invoked, and optionally reset/cleanup
the spy if the test framework requires it.
test('添加事件名会被小写化', () => { | ||
const div = document.createElement('div') | ||
const spy = vi.fn() | ||
const spy2 = vi.fn() | ||
div.addEventListener('tap', spy, null) | ||
div.addEventListener('tap', spy2, null) | ||
const event = runtime.createEvent({ | ||
type: 'tap', | ||
detail: {}, | ||
target: { dataset: {}, id: '' }, | ||
currentTarget: { dataset: {}, id: '' } | ||
}, div) | ||
div.dispatchEvent(event) | ||
expect(spy).toBeCalledTimes(1) | ||
expect(spy2).toBeCalledTimes(1) | ||
div.removeEventListener('tap', spy) | ||
div.dispatchEvent(event) | ||
expect(spy).toBeCalledTimes(1) | ||
expect(spy2).toBeCalledTimes(2) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
测试名与实现不符:未覆盖事件名大小写归一化
当前用例完全使用 'tap',未验证大小写归一。建议一处用 'TAP' 注册并用 'TAP' 触发,同时验证 remove 的归一。
test('添加事件名会被小写化', () => {
const div = document.createElement('div')
const spy = vi.fn()
const spy2 = vi.fn()
- div.addEventListener('tap', spy, null)
- div.addEventListener('tap', spy2, null)
+ div.addEventListener('TAP', spy)
+ div.addEventListener('tap', spy2)
const event = runtime.createEvent({
- type: 'tap',
+ type: 'TAP',
detail: {},
target: { dataset: {}, id: '' },
currentTarget: { dataset: {}, id: '' }
}, div)
div.dispatchEvent(event)
expect(spy).toBeCalledTimes(1)
expect(spy2).toBeCalledTimes(1)
- div.removeEventListener('tap', spy)
+ div.removeEventListener('tap', spy) // 移除时也应大小写无关
div.dispatchEvent(event)
expect(spy).toBeCalledTimes(1)
expect(spy2).toBeCalledTimes(2)
})
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
test('添加事件名会被小写化', () => { | |
const div = document.createElement('div') | |
const spy = vi.fn() | |
const spy2 = vi.fn() | |
div.addEventListener('tap', spy, null) | |
div.addEventListener('tap', spy2, null) | |
const event = runtime.createEvent({ | |
type: 'tap', | |
detail: {}, | |
target: { dataset: {}, id: '' }, | |
currentTarget: { dataset: {}, id: '' } | |
}, div) | |
div.dispatchEvent(event) | |
expect(spy).toBeCalledTimes(1) | |
expect(spy2).toBeCalledTimes(1) | |
div.removeEventListener('tap', spy) | |
div.dispatchEvent(event) | |
expect(spy).toBeCalledTimes(1) | |
expect(spy2).toBeCalledTimes(2) | |
}) | |
test('添加事件名会被小写化', () => { | |
const div = document.createElement('div') | |
const spy = vi.fn() | |
const spy2 = vi.fn() | |
div.addEventListener('TAP', spy) | |
div.addEventListener('tap', spy2) | |
const event = runtime.createEvent({ | |
type: 'TAP', | |
detail: {}, | |
target: { dataset: {}, id: '' }, | |
currentTarget: { dataset: {}, id: '' } | |
}, div) | |
div.dispatchEvent(event) | |
expect(spy).toBeCalledTimes(1) | |
expect(spy2).toBeCalledTimes(1) | |
div.removeEventListener('tap', spy) // 移除时也应大小写无关 | |
div.dispatchEvent(event) | |
expect(spy).toBeCalledTimes(1) | |
expect(spy2).toBeCalledTimes(2) | |
}) |
🤖 Prompt for AI Agents
In packages/taro-runtime/tests/event.spec.ts around lines 96 to 115, the test
only uses lowercase 'tap' and doesn't verify case-normalization; update the test
to register one listener with an uppercase event name (e.g., 'TAP') while
keeping the other as 'tap', create and dispatch the event using uppercase
('TAP') to ensure handlers registered with different cases are both invoked,
then call removeEventListener using the uppercase name to verify removal is
normalized and adjust the expect assertions to match the call counts after each
dispatch accordingly.
test('dispatchEvent 也会被小写', () => { | ||
const div = document.createElement('div') | ||
const spy = vi.fn() | ||
const spy2 = vi.fn() | ||
div.addEventListener('tap', spy2, null) | ||
div.addEventListener('tap', (e: any) => { | ||
spy() | ||
e.stopImmediatePropagation() | ||
}, null) | ||
const event = runtime.createEvent({ | ||
type: 'tap', | ||
detail: {}, | ||
target: { dataset: {}, id: '' }, | ||
currentTarget: { dataset: {}, id: '' } | ||
}, div) | ||
div.dispatchEvent(event) | ||
expect(spy).toBeCalledTimes(1) | ||
expect(spy2).toBeCalledTimes(0) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
未真正验证 dispatchEvent 的大小写归一化
事件创建仍为 'tap'。建议改为 'TAP' 以覆盖归一逻辑。
const event = runtime.createEvent({
- type: 'tap',
+ type: 'TAP',
detail: {},
target: { dataset: {}, id: '' },
currentTarget: { dataset: {}, id: '' }
}, div)
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
test('dispatchEvent 也会被小写', () => { | |
const div = document.createElement('div') | |
const spy = vi.fn() | |
const spy2 = vi.fn() | |
div.addEventListener('tap', spy2, null) | |
div.addEventListener('tap', (e: any) => { | |
spy() | |
e.stopImmediatePropagation() | |
}, null) | |
const event = runtime.createEvent({ | |
type: 'tap', | |
detail: {}, | |
target: { dataset: {}, id: '' }, | |
currentTarget: { dataset: {}, id: '' } | |
}, div) | |
div.dispatchEvent(event) | |
expect(spy).toBeCalledTimes(1) | |
expect(spy2).toBeCalledTimes(0) | |
}) | |
test('dispatchEvent 也会被小写', () => { | |
const div = document.createElement('div') | |
const spy = vi.fn() | |
const spy2 = vi.fn() | |
div.addEventListener('tap', spy2, null) | |
div.addEventListener('tap', (e: any) => { | |
spy() | |
e.stopImmediatePropagation() | |
}, null) | |
const event = runtime.createEvent({ | |
type: 'TAP', | |
detail: {}, | |
target: { dataset: {}, id: '' }, | |
currentTarget: { dataset: {}, id: '' } | |
}, div) | |
div.dispatchEvent(event) | |
expect(spy).toBeCalledTimes(1) | |
expect(spy2).toBeCalledTimes(0) | |
}) |
🤖 Prompt for AI Agents
In packages/taro-runtime/tests/event.spec.ts around lines 210 to 228, the test
intended to verify dispatchEvent lowercasing uses an event created with type
'tap' so it doesn't actually exercise the case-normalization; change the created
event's type to an uppercase variant (e.g. 'TAP') so the runtime's normalization
logic is exercised — keep the rest of the test the same and assert the listeners
behave as expected (spy called once, spy2 not called).
test('TaroEvent constructor without options', () => { | ||
const event = new runtime.TaroEvent('TAP', { bubbles: false, cancelable: false }) | ||
expect(event.type).toBe('tap') // should be lowercase | ||
expect(event.bubbles).toBe(false) | ||
expect(event.cancelable).toBe(false) | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
“without options” 名不副实
既然要验证无 options,建议不传第二参。
- const event = new runtime.TaroEvent('TAP', { bubbles: false, cancelable: false })
+ const event = new runtime.TaroEvent('TAP')
expect(event.type).toBe('tap') // should be lowercase
expect(event.bubbles).toBe(false)
expect(event.cancelable).toBe(false)
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
test('TaroEvent constructor without options', () => { | |
const event = new runtime.TaroEvent('TAP', { bubbles: false, cancelable: false }) | |
expect(event.type).toBe('tap') // should be lowercase | |
expect(event.bubbles).toBe(false) | |
expect(event.cancelable).toBe(false) | |
}) | |
test('TaroEvent constructor without options', () => { | |
const event = new runtime.TaroEvent('TAP') | |
expect(event.type).toBe('tap') // should be lowercase | |
expect(event.bubbles).toBe(false) | |
expect(event.cancelable).toBe(false) | |
}) |
🤖 Prompt for AI Agents
In packages/taro-runtime/tests/event.spec.ts around lines 260 to 266, the test
title says "TaroEvent constructor without options" but the constructor is called
with a second argument ({ bubbles: false, cancelable: false}); update the test
to actually call new runtime.TaroEvent('TAP') with no second parameter and keep
the existing assertions (expect lowercase type and default bubbles/cancelable
expectations) or adjust expectations to reflect defaults if they differ.
// Create elements | ||
const targetElement = runtime.document.createElement('div') | ||
targetElement.id = 'target-id' | ||
targetElement.dataset.role = 'target' | ||
runtime.document.body.appendChild(targetElement) | ||
|
||
const currentElement = runtime.document.createElement('div') | ||
currentElement.id = 'current-id' | ||
currentElement.dataset.role = 'current' | ||
runtime.document.body.appendChild(currentElement) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currentTarget 断言缺少数据准备,当前会失败
期望包含 customData,但未对 current 元素设置该字段。
const currentElement = runtime.document.createElement('div')
currentElement.id = 'current-id'
currentElement.dataset.role = 'current'
+ currentElement.dataset.customData = 'value'
runtime.document.body.appendChild(currentElement)
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// Create elements | |
const targetElement = runtime.document.createElement('div') | |
targetElement.id = 'target-id' | |
targetElement.dataset.role = 'target' | |
runtime.document.body.appendChild(targetElement) | |
const currentElement = runtime.document.createElement('div') | |
currentElement.id = 'current-id' | |
currentElement.dataset.role = 'current' | |
runtime.document.body.appendChild(currentElement) | |
const currentElement = runtime.document.createElement('div') | |
currentElement.id = 'current-id' | |
currentElement.dataset.role = 'current' | |
currentElement.dataset.customData = 'value' | |
runtime.document.body.appendChild(currentElement) |
🤖 Prompt for AI Agents
In packages/taro-runtime/tests/event.spec.ts around lines 303 to 313, the test
asserts currentTarget contains customData but the currentElement never has that
dataset prepared; set currentElement.dataset.customData to the same value the
test expects (e.g., the serialized/custom-data string or object used in
assertions) before appending it to the document so the currentTarget assertion
passes.
test('eventHandler with basic mpEvent', () => { | ||
const mockHooks = { | ||
call: vi.fn(), | ||
isExist: vi.fn(() => false) | ||
} | ||
|
||
// Mock hooks globally | ||
const originalHooks = runtime.hooks | ||
Object.assign(runtime.hooks, mockHooks) | ||
|
||
const element = runtime.document.createElement('div') | ||
element.id = 'test-element' | ||
runtime.document.body.appendChild(element) | ||
|
||
const mpEvent = { | ||
type: 'tap', | ||
target: { dataset: {}, id: 'test-element' }, | ||
currentTarget: { dataset: {}, id: 'test-element' }, | ||
detail: {} | ||
} | ||
|
||
eventHandler(mpEvent) | ||
|
||
expect(mockHooks.call).toHaveBeenCalledWith('modifyMpEventImpl', mpEvent) | ||
expect(mockHooks.isExist).toHaveBeenCalledWith('batchedEventUpdates') | ||
|
||
// Restore original hooks | ||
Object.assign(runtime.hooks, originalHooks) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mock hooks 的覆盖不可恢复,污染全局(高风险)
const originalHooks = runtime.hooks
保存的是引用,Object.assign
覆盖后无法恢复原实现,后续用例会被污染。请使用 vi.spyOn 精确替换并在用例末尾恢复(或依赖 afterEach 统一恢复)。
示例(以 369-397 为例,其他段落同理替换):
- const mockHooks = {
- call: vi.fn(),
- isExist: vi.fn(() => false)
- }
-
- // Mock hooks globally
- const originalHooks = runtime.hooks
- Object.assign(runtime.hooks, mockHooks)
+ const callSpy = vi.spyOn(runtime.hooks, 'call').mockImplementation(vi.fn())
+ const isExistSpy = vi.spyOn(runtime.hooks, 'isExist').mockImplementation(vi.fn(() => false))
@@
- expect(mockHooks.call).toHaveBeenCalledWith('modifyMpEventImpl', mpEvent)
- expect(mockHooks.isExist).toHaveBeenCalledWith('batchedEventUpdates')
+ expect(callSpy).toHaveBeenCalledWith('modifyMpEventImpl', mpEvent)
+ expect(isExistSpy).toHaveBeenCalledWith('batchedEventUpdates')
@@
- // Restore original hooks
- Object.assign(runtime.hooks, originalHooks)
+ callSpy.mockRestore()
+ isExistSpy.mockRestore()
并配合前述 afterEach 全局兜底恢复。
Also applies to: 399-427, 428-448, 450-486, 488-517, 519-546, 548-586, 588-612
🤖 Prompt for AI Agents
packages/taro-runtime/tests/event.spec.ts lines 369-397: the test saves a
reference to runtime.hooks and uses Object.assign to overwrite it which mutates
the global hooks and cannot be reliably restored; instead use vi.spyOn(runtime,
'hooks', 'get') or vi.spyOn(runtime.hooks, 'call') and vi.spyOn(runtime.hooks,
'isExist') (or spies on the specific methods) to replace behavior, then call
mockRestore() (or rely on afterEach to restore) at the end of the test so the
original implementations are not permanently overwritten; apply the same
spy/restore pattern to the other listed ranges (399-427, 428-448, 450-486,
488-517, 519-546, 548-586, 588-612).
这个 PR 做了什么? (简要描述所做更改)
将 taro-runtime 的 jest 测试改为 vitest 测试
这个 PR 是什么类型? (至少选择一个)
这个 PR 涉及以下平台:
Summary by CodeRabbit
Bug Fixes
Refactor
Chores
Documentation