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
+2 -2
View File
@@ -24,7 +24,7 @@ export const event = (action: string, category: string, label?: string, value?:
}
};
export const trackContactForm = (formData: Record<string, string>) => {
export const trackContactForm = (_formData: Record<string, string>) => {
event('submit', 'contact_form', 'contact_form_submission');
};
@@ -32,6 +32,6 @@ export const trackButtonClick = (buttonName: string, location: string) => {
event('click', 'button', `${location}_${buttonName}`);
};
export const trackPageView = (pageTitle: string, pagePath: string) => {
export const trackPageView = (pageTitle: string, _pagePath: string) => {
event('page_view', 'navigation', pageTitle);
};
-2
View File
@@ -1,5 +1,3 @@
import { describe, it, expect } from '@jest/globals';
export interface SessionData {
userId: string;
role?: string;
+1 -1
View File
@@ -32,7 +32,7 @@ export class PerformanceMonitor {
const sorted = [...values].sort((a, b) => a - b);
const index = Math.ceil((percentile / 100) * sorted.length) - 1;
return sorted[Math.max(0, index)];
return sorted[Math.max(0, index)] ?? 0;
}
getCount(name: string): number {
+7 -8
View File
@@ -1,5 +1,4 @@
import { describe, it, expect, jest, beforeEach, afterEach } from '@jest/globals';
import path from 'path';
import { writeFile, mkdir, unlink, stat } from 'fs/promises';
import { existsSync } from 'fs';
@@ -215,9 +214,9 @@ describe('Upload Module', () => {
name: 'test.jpg',
size: 1024,
type: 'image/jpeg',
arrayBuffer: jest.fn().mockResolvedValue(new ArrayBuffer(1024)),
arrayBuffer: jest.fn().mockResolvedValue(new ArrayBuffer(1024)) as any,
...overrides,
} as any;
} as File;
};
it('should upload a valid image file successfully', async () => {
@@ -225,7 +224,7 @@ describe('Upload Module', () => {
const validJpegBuffer = Buffer.from([0xFF, 0xD8, 0xFF, 0x00, 0x00]);
const mockFile = createMockFile({
arrayBuffer: jest.fn().mockResolvedValue(validJpegBuffer),
arrayBuffer: jest.fn().mockResolvedValue(validJpegBuffer) as any as any,
});
mockedExistsSync.mockReturnValue(true);
@@ -280,7 +279,7 @@ describe('Upload Module', () => {
const fakeJpegBuffer = Buffer.from([0x00, 0x00, 0x00, 0x00]);
const fakeFile = createMockFile({
type: 'image/jpeg',
arrayBuffer: jest.fn().mockResolvedValue(fakeJpegBuffer),
arrayBuffer: jest.fn().mockResolvedValue(fakeJpegBuffer) as any as any,
});
await expect(uploadFile(fakeFile, { type: 'image' }))
@@ -292,7 +291,7 @@ describe('Upload Module', () => {
const validJpegBuffer = Buffer.from([0xFF, 0xD8, 0xFF, 0x00, 0x00]);
const mockFile = createMockFile({
arrayBuffer: jest.fn().mockResolvedValue(validJpegBuffer),
arrayBuffer: jest.fn().mockResolvedValue(validJpegBuffer) as any as any,
});
mockedExistsSync.mockReturnValue(false);
@@ -309,7 +308,7 @@ describe('Upload Module', () => {
const validJpegBuffer = Buffer.from([0xFF, 0xD8, 0xFF, 0x00, 0x00]);
const mockFile = createMockFile({
arrayBuffer: jest.fn().mockResolvedValue(validJpegBuffer),
arrayBuffer: jest.fn().mockResolvedValue(validJpegBuffer) as any as any,
});
mockedExistsSync.mockReturnValue(true);
@@ -326,7 +325,7 @@ describe('Upload Module', () => {
const validJpegBuffer = Buffer.from([0xFF, 0xD8, 0xFF, 0x00, 0x00]);
const mockFile = createMockFile({
name: 'Test<>File.JPG',
arrayBuffer: jest.fn().mockResolvedValue(validJpegBuffer),
arrayBuffer: jest.fn().mockResolvedValue(validJpegBuffer) as any as any,
});
mockedExistsSync.mockReturnValue(true);