Skip to content

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Nov 3, 2025

概述 Descriptions

单元测试优化(错误修正,注入修正,concurrent,加速)

变更内容 Changes

  • global.xxxx = yyyy -> vi.stubGlobal("xxxx", yyyy);
  • 可以 concurrent 的小單元測試改成 concurrent
  • 刪無用 src/app/service/gm_api.test.ts
  • [package.json] "test": "vitest", -> "test": "vitest --test-timeout=500 --no-coverage --isolate=false --reporter=verbose",
  • 改用 nextTick 模擬異步
  • 修正 tests/pages/popup/App.test.tsxtests/pages/options/MainLayout.test.tsxvi.mock

截图 Screenshots

@CodFrm CodFrm requested a review from Copilot November 4, 2025 03:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR modernizes the test infrastructure by migrating to Vitest's concurrent testing features and improving test isolation. The changes focus on:

  • Migrating from test()/describe() to it.concurrent()/describe.concurrent() for better test parallelization
  • Replacing manual global variable assignments with vi.stubGlobal() for better test isolation
  • Refactoring test setup to use vi.hoisted() to avoid mock hoisting pitfalls
  • Removing fake timers in favor of real async timing with helper functions
  • Updating test timeouts and isolation settings in package.json

Reviewed Changes

Copilot reviewed 32 out of 32 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
package.json Updated test script with concurrent settings and timeout configuration
tests/vitest.setup.ts Migrated to vi.stubGlobal() for test globals and added chrome mock setup
tests/utils.test.ts Added concurrent flag and replaced global assignment with vi.stubGlobal()
tests/test-utils.tsx Refactored setupGlobalMocks to use vi.stubGlobal()
tests/pages/popup/App.test.tsx Major refactor using vi.hoisted() for mock definitions and proper paths
tests/pages/options/MainLayout.test.tsx Applied similar hoisting pattern and added missing mock configurations
tests/pages/components/ScriptMenuList.test.tsx Moved setup/teardown outside describe blocks for better isolation
src/pkg/utils/*.test.ts Converted all tests to concurrent with it.concurrent() and describe.concurrent()
src/app/service/**/*.test.ts Applied concurrent patterns across service layer tests
packages/message/*.test.ts Refactored message tests with proper async handling and concurrent support
packages/chrome-extension-mock/* Updated mock to remove global assignment and add runtime.id
src/pages/popup/App.tsx Fixed import paths to use absolute imports
src/app/service/service_worker/gm_api.ts Added return values and error handling to GM API methods
Comments suppressed due to low confidence (1)

src/app/service/service_worker/gm_api.ts:99

  • GM_setValues 方法新增了返回值 true,但没有文档说明这个返回值的含义。建议添加注释说明返回值的用途,或者如果这是为了与 Tampermonkey API 兼容,应该在注释中说明。
    return true;

"scripts": {
"preinstall": "pnpm dlx only-allow pnpm",
"test": "vitest",
"test": "vitest --test-timeout=500 --no-coverage --isolate=false --reporter=verbose",
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

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

设置 --isolate=false 可能会导致测试之间的状态泄漏和难以调试的问题,特别是在使用全局 mock 的情况下。虽然这样可以提高测试速度,但建议保留隔离模式(默认值)以确保测试的可靠性,或者至少在 CI 环境中使用隔离模式运行测试。

Suggested change
"test": "vitest --test-timeout=500 --no-coverage --isolate=false --reporter=verbose",
"test": "vitest --test-timeout=500 --no-coverage --reporter=verbose",

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

可以不用isolate. 執行速度加快

@CodFrm CodFrm merged commit 2e76b3c into scriptscat:main Nov 4, 2025
2 checks passed
cyfung1031 added a commit to cyfung1031/scriptcat that referenced this pull request Nov 6, 2025
* 单元测试优化(错误修正,注入修正,concurrent,加速)

* 统一 `vitest.test` -> `vitest.it`

* scriptscat#903

* 代码修正
@CodFrm
Copy link
Member

CodFrm commented Nov 8, 2025

https://github.com/scriptscat/scriptcat/actions/runs/19186784933/job/54854800629

it.concurrent("每分钟表达式", () => {

这也超时了,是不是超时时间太短了,又是并发,action的垃圾cpu(也可能是限制了)处理不过来

想调成 1s 了

@cyfung1031
Copy link
Collaborator Author

cyfung1031 commented Nov 8, 2025

https://github.com/scriptscat/scriptcat/actions/runs/19186784933/job/54854800629

it.concurrent("每分钟表达式", () => {

这也超时了,是不是超时时间太短了,又是并发,action的垃圾cpu(也可能是限制了)处理不过来

想调成 1s 了

我也有点不理解

Screenshot 2025-11-08 at 12 25 56

每星期 8ms 就执行结束了

我再看看


#927 已處理

@cyfung1031 cyfung1031 deleted the pr-vitest-006 branch December 21, 2025 09:39
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