-
Notifications
You must be signed in to change notification settings - Fork 310
单元测试优化(错误修正,注入修正,concurrent,加速) #904
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
Conversation
75e75cd to
9a25e8c
Compare
9a25e8c to
9dabdc6
Compare
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.
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()toit.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", |
Copilot
AI
Nov 4, 2025
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.
设置 --isolate=false 可能会导致测试之间的状态泄漏和难以调试的问题,特别是在使用全局 mock 的情况下。虽然这样可以提高测试速度,但建议保留隔离模式(默认值)以确保测试的可靠性,或者至少在 CI 环境中使用隔离模式运行测试。
| "test": "vitest --test-timeout=500 --no-coverage --isolate=false --reporter=verbose", | |
| "test": "vitest --test-timeout=500 --no-coverage --reporter=verbose", |
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.
可以不用isolate. 執行速度加快
* 单元测试优化(错误修正,注入修正,concurrent,加速) * 统一 `vitest.test` -> `vitest.it` * scriptscat#903 * 代码修正
|
https://github.com/scriptscat/scriptcat/actions/runs/19186784933/job/54854800629 scriptcat/src/pkg/utils/utils.test.ts Line 9 in 349613e
这也超时了,是不是超时时间太短了,又是并发,action的垃圾cpu(也可能是限制了)处理不过来 想调成 1s 了 |
我也有点不理解
每星期 8ms 就执行结束了 我再看看 #927 已處理 |

概述 Descriptions
单元测试优化(错误修正,注入修正,concurrent,加速)
变更内容 Changes
src/app/service/gm_api.test.tspackage.json]"test": "vitest",->"test": "vitest --test-timeout=500 --no-coverage --isolate=false --reporter=verbose",nextTick模擬異步tests/pages/popup/App.test.tsx和tests/pages/options/MainLayout.test.tsx的vi.mock截图 Screenshots