feat: 增加测试覆盖率并优化代码质量

test: 添加单元测试和端到端测试
refactor: 重构登录页面和上传模块
ci: 更新测试覆盖率阈值至42%
build: 添加测试相关依赖
docs: 更新测试文档
style: 修复代码格式问题
This commit is contained in:
张翔
2026-03-11 11:14:37 +08:00
parent 8fd7ed84ed
commit b207bfa7af
58 changed files with 14494 additions and 655 deletions
+80
View File
@@ -0,0 +1,80 @@
import { test, expect, Page } from '@playwright/test';
import * as fs from 'fs';
import * as path from 'path';
let coverageData: any[] = [];
async function startCoverage(page: Page) {
await page.coverage.startJSCoverage({
resetOnNavigation: true,
reportAnonymousScripts: false,
});
}
async function stopCoverage(page: Page) {
const coverage = await page.coverage.stopJSCoverage();
coverageData = coverage;
console.log(`Collected ${coverage.length} JS coverage entries`);
}
async function saveCoverage() {
const outputPath = path.join(__dirname, '../../coverage/e2e/coverage-data.json');
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(outputPath, JSON.stringify(coverageData, null, 2));
console.log(`Coverage data saved to: ${outputPath}`);
}
test.describe('E2E覆盖率测试', () => {
test.beforeEach(async ({ page }) => {
await startCoverage(page);
});
test.afterEach(async ({ page }) => {
await stopCoverage(page);
});
test('首页应该正常加载', async ({ page }) => {
await page.goto('http://localhost:3000/');
await expect(page).toHaveURL(/localhost:3000\//);
await page.waitForLoadState('networkidle');
});
test('关于页面应该正常加载', async ({ page }) => {
await page.goto('http://localhost:3000/about');
await expect(page).toHaveURL(/about/);
await page.waitForLoadState('networkidle');
});
test('产品页面应该正常加载', async ({ page }) => {
await page.goto('http://localhost:3000/products');
await expect(page).toHaveURL(/products/);
await page.waitForLoadState('networkidle');
});
test('服务页面应该正常加载', async ({ page }) => {
await page.goto('http://localhost:3000/services');
await expect(page).toHaveURL(/services/);
await page.waitForLoadState('networkidle');
});
test('案例页面应该正常加载', async ({ page }) => {
await page.goto('http://localhost:3000/cases');
await expect(page).toHaveURL(/cases/);
await page.waitForLoadState('networkidle');
});
test('新闻页面应该正常加载', async ({ page }) => {
await page.goto('http://localhost:3000/news');
await expect(page).toHaveURL(/news/);
await page.waitForLoadState('networkidle');
});
test.afterAll(async () => {
await saveCoverage();
});
});