Skip to content

Conversation

ianzone
Copy link
Contributor

@ianzone ianzone commented Jul 22, 2025

这个 PR 做了什么? (简要描述所做更改)
将 taro-runtime 的 jest 测试改为 vitest 测试

这个 PR 是什么类型? (至少选择一个)

  • 错误修复 (Bugfix) issue: fix #
  • 新功能 (Feature)
  • 代码重构 (Refactor)
  • TypeScript 类型定义修改 (Types)
  • 文档修改 (Docs)
  • 代码风格更新 (Code style update)
  • 构建优化 (Chore)
  • 其他,请描述 (Other, please describe):

这个 PR 涉及以下平台:

  • 所有平台
  • Web 端(H5)
  • 移动端(React-Native)
  • 鸿蒙(Harmony)
  • 鸿蒙容器(Harmony Hybrid)
  • ASCF 元服务
  • 快应用(QuickApp)
  • 所有小程序
  • 微信小程序
  • 企业微信小程序
  • 京东小程序
  • 百度小程序
  • 支付宝小程序
  • 支付宝 IOT 小程序
  • 钉钉小程序
  • QQ 小程序
  • 飞书小程序
  • 快手小程序
  • 头条小程序

Summary by CodeRabbit

  • Bug Fixes

    • 修正卸载阶段拼写错误(DESTORY → DESTROY),确保页面上下文、历史与位置能正确清理。
  • Refactor

    • 导出窗口类以供外部使用;移除类型的显式导出。
    • 上下文动作枚举项重命名为 DESTROY。
    • 内部 HTML 扫描器类由 Scaner 重命名为 Scanner。
    • 公共工具函数名由 isParentBinded 改为 isParentBound。
  • Chores

    • 测试框架迁移至 Vitest:替换配置、脚本与新增 vitest 配置;删除 Jest/ts-jest 配置与 tsconfig.test.json;更新多处测试为 Vitest 风格并添加/重构测试文件。
  • Documentation

    • 更新 README 中事件文档链接。

Copy link

coderabbitai bot commented Jul 22, 2025

Walkthrough

本次变更将 taro-runtime 的测试框架从 Jest 迁移到 Vitest;重构并新增/移动多份测试为 ESM/Vitest 风格;修复大量拼写错误(CONTEXT_ACTIONS.DESTORY → CONTEXT_ACTIONS.DESTROY)并同步引用;导出 TaroWindow;重命名 inner-html 扫描器 Scaner → Scanner;调整相关配置与文档链接。

Changes

Cohort / File(s) Summary
测试框架迁移:Jest → Vitest
packages/taro-runtime/jest.config.js, packages/taro-runtime/tsconfig.test.json, packages/taro-runtime/vitest.config.ts, packages/taro-runtime/package.json, packages/taro-runtime/tsconfig.json
移除 Jest 配置与测试 tsconfig,新增 Vitest 配置(全局特性标志、coverage、setup);更新 npm scripts 为 vitest;调整 tsconfig 包含项/排除项。
测试目录与用例重构(ESM/Vitest 化)
packages/taro-runtime/src/__tests__/*, packages/taro-runtime/tests/*.spec.ts, packages/taro-runtime/tests/bom/window.spec.ts, packages/taro-runtime/tests/utils.ts
删除旧的 Jest 测试(src/tests),将/新增测试迁移至 tests 目录并改用 Vitest 的 ES 模块导入(describe/test/expect/vi/vi.fn),并做少量类型与可选链调整。
上下文销毁动作常量修正
packages/taro-runtime/src/constants/index.ts, packages/taro-runtime/src/bom/{history.ts,location.ts,window.ts}, packages/taro-runtime/src/dsl/common.ts, packages/taro-framework-react/..., packages/taro-platform-harmony-cpp/..., packages/taro-platform-harmony/...
将枚举与字符串常量 CONTEXT_ACTIONS.DESTORY 更名为 CONTEXT_ACTIONS.DESTROY,并在各处同步触发/监听。
TaroWindow 导出与测试
packages/taro-runtime/src/bom/window.ts, packages/taro-runtime/tests/bom/window.spec.ts
TaroWindow 改为导出类(export class TaroWindow),修正生命周期事件引用;新增覆盖初始化、属性复制与生命周期事件的单元测试。
inner-html 扫描器重命名与对齐
packages/taro-runtime/src/dom-external/inner-html/{scanner.ts,parser.ts}, packages/taro-runtime/tests/html.spec.ts
类名与导出由 ScanerScanner,parser 使用 new Scanner(...).scan();更新测试导入/路径与少量命名。
工具函数重命名与引用更新
packages/taro-platform-harmony/src/runtime-ets/utils/index.ts, packages/taro-runtime/src/utils/index.ts, packages/taro-runtime/src/dom/event.ts
将导出函数 isParentBinded 重命名为 isParentBound,并在使用处同步更新导入与调用。
文档与小修正
packages/taro-runtime/README.md
更新 Events 文档链接至 docs.taro.zone,修正注释拼写与少量格式调整。

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: 卸载完成通知
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • yoyo837
  • tutuxxx

Poem

小兔跳出码行间,
Jest 换衣成 Vitest 才欢。
DESTROY 一声风拂过,
窗口清缓存,测试新颜。
Scanner 更名不回头,快乐起舞 🐰✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

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

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai bot requested review from tutuxxx and yoyo837 July 22, 2025 09:07
Copy link

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between b1e8dc9 and 2c02ceb.

⛔ 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 导出的 windowdocument 实例。
而对 navigator 的验证在 exports.spec.ts 中通过:

  • runtime.window.navigatorruntime.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 模拟对象中添加了 $taroPathonReadyonHideonShow 属性,使模拟对象更完整。


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 操作的变异观察,包括单个节点、多个兄弟节点和深层结构的移除,异步测试模式使用正确。

Copy link

codecov bot commented Jul 22, 2025

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 55.50%. Comparing base (4a44051) to head (b0298bc).

Files with missing lines Patch % Lines
packages/taro-runtime/src/dsl/common.ts 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            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     
Flag Coverage Δ
taro-cli 72.85% <ø> (ø)
taro-runtime 60.51% <88.88%> (+0.64%) ⬆️
taro-web 53.13% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/taro-runtime/src/bom/history.ts 98.27% <100.00%> (+58.27%) ⬆️
packages/taro-runtime/src/bom/location.ts 90.72% <ø> (+74.16%) ⬆️
packages/taro-runtime/src/bom/window.ts 91.83% <100.00%> (+28.04%) ⬆️
packages/taro-runtime/src/constants/index.ts 100.00% <100.00%> (ø)
...taro-runtime/src/dom-external/inner-html/parser.ts 78.83% <100.00%> (-0.89%) ⬇️
...aro-runtime/src/dom-external/inner-html/scanner.ts 81.30% <ø> (ø)
packages/taro-runtime/src/dom/event.ts 93.87% <100.00%> (ø)
packages/taro-runtime/src/utils/index.ts 59.32% <100.00%> (+33.92%) ⬆️
packages/taro-runtime/src/dsl/common.ts 1.00% <0.00%> (ø)

... and 50 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ianzone
Copy link
Contributor Author

ianzone commented Jul 22, 2025

@yoyo837 辛苦合一下哈

This was referenced Jul 26, 2025
Copy link

@coderabbitai coderabbitai bot left a 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.

📥 Commits

Reviewing files that changed from the base of the PR and between c3fd9fb and b0298bc.

📒 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'
Copy link

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.

Comment on lines +8 to +12
const document = runtime.document

afterAll(() => {
process.env.FRAMEWORK = ''
})
Copy link

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.

Suggested change
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.

Comment on lines +10 to +12
afterAll(() => {
process.env.FRAMEWORK = ''
})
Copy link

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.

Comment on lines +14 to +26
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)
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

用例未断言结果,存在“假阳性”风险

应验证监听器是否被触发一次。

   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.

Suggested change
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.

Comment on lines +96 to +115
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)
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

测试名与实现不符:未覆盖事件名大小写归一化

当前用例完全使用 '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.

Suggested change
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.

Comment on lines +210 to +228
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)
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

未真正验证 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.

Suggested change
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).

Comment on lines +260 to +266
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)
})

Copy link

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.

Suggested change
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.

Comment on lines +303 to +313
// 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)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

Suggested change
// 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.

Comment on lines +369 to +397
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)
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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).

This was referenced Sep 6, 2025
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.

2 participants