From da0b5189003d562afce6f52199a582cb508df72a Mon Sep 17 00:00:00 2001 From: Xi Xu Date: Thu, 24 Jul 2025 21:28:47 +0800 Subject: [PATCH] Update test tooling and scripts, add tsconfig.json Modernizes test and lint scripts in package.json to use Vitest, Prettier, and ESLint directly. Updates devDependencies to latest versions. Adds a tsconfig.json for type checking and editor support. Improves test mocking in performance.test.js and updates documentation in README.md to reflect new test commands. Cleans up unused options in vitest.config.js. --- README.md | 32 +++++++++++++++++++++++++++++++- package.json | 26 +++++++++++--------------- test/performance.test.js | 10 +++++++--- tsconfig.json | 27 +++++++++++++++++++++++++++ vitest.config.js | 4 +--- 5 files changed, 77 insertions(+), 22 deletions(-) create mode 100644 tsconfig.json diff --git a/README.md b/README.md index 5a376e3..c78362b 100644 --- a/README.md +++ b/README.md @@ -654,10 +654,40 @@ export const PLATFORMS = { ```bash npm run dev # 启动开发服务器 (http://localhost:8787) - npm test # 运行测试 + npm run test:run # 运行完整测试套件 + npm run test:coverage # 生成测试覆盖率报告 + npm run lint # 代码检查 + npm run format # 代码格式化 npm run deploy # 部署到生产 ``` +## 🧪 测试 + +项目包含完整的测试套件,确保代码质量和功能正确性。 + +### 完整测试 + +```bash +# 安装测试依赖 +npm install + +# 运行所有测试 +npm run test:run + +# 生成覆盖率报告 +npm run test:coverage + +# 监视模式 +npm run test:watch +``` + +### 测试覆盖 + +- **单元测试**: 核心功能、平台配置、性能监控 +- **集成测试**: 端到端流程、平台集成、Git 协议 +- **安全测试**: 输入验证、安全头、权限控制 +- **性能测试**: 响应时间、内存使用、并发处理 + ## 🔍 故障排除 ### 常见问题 diff --git a/package.json b/package.json index 9ec7f5d..7e25858 100644 --- a/package.json +++ b/package.json @@ -3,32 +3,28 @@ "xget": "file:" }, "devDependencies": { - "@cloudflare/vitest-pool-workers": "^0.5.0", - "@cloudflare/workers-types": "^4.20241218.0", - "eslint": "^9.17.0", - "prettier": "^3.4.2", - "vitest": "^2.1.8", - "wrangler": "^4.18.0" + "@cloudflare/vitest-pool-workers": "^0.8.36", + "@cloudflare/workers-types": "^4.20250724.0", + "eslint": "^9.31.0", + "prettier": "^3.6.2", + "vitest": "^3.1.4", + "wrangler": "^3.100.0" }, "name": "xget", "private": true, "scripts": { "deploy": "wrangler deploy", "dev": "wrangler dev", - "format": "node scripts/test.js format", + "format": "prettier --write src/ test/ *.js *.json *.md", "format:check": "prettier --check src/ test/ *.js *.json *.md", - "lint": "node scripts/test.js lint", + "lint": "eslint src/ test/", "lint:fix": "eslint src/ test/ --fix", "start": "wrangler dev", - "test": "node scripts/test.js run", - "test:bench": "node scripts/test.js bench", - "test:ci": "node scripts/test.js ci", - "test:clean": "node scripts/test.js clean", - "test:coverage": "node scripts/test.js coverage", + "test": "vitest", + "test:coverage": "vitest run --coverage", "test:run": "vitest run", - "test:setup": "node scripts/test.js setup", "test:ui": "vitest --ui", - "test:watch": "node scripts/test.js watch", + "test:watch": "vitest --watch", "type-check": "tsc --noEmit" }, "version": "1.0.0" diff --git a/test/performance.test.js b/test/performance.test.js index f6ecd47..ea70ee1 100644 --- a/test/performance.test.js +++ b/test/performance.test.js @@ -53,14 +53,18 @@ describe('Performance Monitoring', () => { }); it('should warn on duplicate mark names', () => { - const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + // Mock console.warn for this test + const originalWarn = console.warn; + const mockWarn = vi ? vi.fn() : jest.fn(); + console.warn = mockWarn; monitor.mark('duplicate'); monitor.mark('duplicate'); - expect(consoleSpy).toHaveBeenCalledWith('Mark with name duplicate already exists.'); + expect(mockWarn).toHaveBeenCalledWith('Mark with name duplicate already exists.'); - consoleSpy.mockRestore(); + // Restore original console.warn + console.warn = originalWarn; }); it('should return metrics as plain object', () => { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..c9c5cd1 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "allowJs": true, + "checkJs": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "module": "ESNext", + "moduleResolution": "node", + "noEmit": true, + "skipLibCheck": true, + "strict": true, + "target": "ES2022", + "types": [ + "@cloudflare/workers-types", + "vitest/globals" + ] + }, + "exclude": [ + "node_modules", + "dist", + "coverage" + ], + "include": [ + "src/**/*", + "test/**/*" + ] +} \ No newline at end of file diff --git a/vitest.config.js b/vitest.config.js index 0baa8fa..6f3e794 100644 --- a/vitest.config.js +++ b/vitest.config.js @@ -31,8 +31,6 @@ export default defineWorkersConfig({ statements: 80 } } - }, - globals: true, - environment: 'node' + } }, });