浏览代码

✅ test(minio): improve test isolation and coverage

- move environment variable mocking to global beforeEach/afterEach for better isolation
- add bucketExists mock in error handling test to ensure proper test setup
- add bucketExists mock in large file test to complete test scenario
- update putObject mock to return realistic response object with etag and versionId
yourname 2 月之前
父节点
当前提交
98f833d390
共有 1 个文件被更改,包括 21 次插入9 次删除
  1. 21 9
      src/server/__integration_tests__/minio.integration.test.ts

+ 21 - 9
src/server/__integration_tests__/minio.integration.test.ts

@@ -7,6 +7,20 @@ import { logger } from '@/server/utils/logger';
 vi.mock('minio');
 vi.mock('@/server/utils/logger');
 
+// Mock process.env using vi.stubEnv for proper isolation
+beforeEach(() => {
+  vi.stubEnv('MINIO_HOST', 'localhost');
+  vi.stubEnv('MINIO_PORT', '9000');
+  vi.stubEnv('MINIO_USE_SSL', 'false');
+  vi.stubEnv('MINIO_ACCESS_KEY', 'minioadmin');
+  vi.stubEnv('MINIO_SECRET_KEY', 'minioadmin');
+  vi.stubEnv('MINIO_BUCKET_NAME', 'test-bucket');
+});
+
+afterEach(() => {
+  vi.unstubAllEnvs();
+});
+
 describe('MinIO Integration Tests', () => {
   let minioService: MinioService;
   let mockClient: Client;
@@ -14,17 +28,10 @@ describe('MinIO Integration Tests', () => {
   beforeEach(() => {
     mockClient = new Client({} as any);
     (Client as any).mockClear();
-
-    // Mock environment variables for testing
-    vi.stubEnv('MINIO_HOST', 'localhost');
-    vi.stubEnv('MINIO_PORT', '9000');
-    vi.stubEnv('MINIO_USE_SSL', 'false');
-    vi.stubEnv('MINIO_BUCKET_NAME', 'test-bucket');
+    (Client as any).mockImplementation(() => mockClient);
 
     // Create MinioService with mock client
     minioService = new MinioService();
-    // Replace the internal client with our mock
-    minioService['client'] = mockClient;
   });
 
   afterEach(() => {
@@ -211,6 +218,10 @@ describe('MinIO Integration Tests', () => {
 
     it('should handle file operation errors', async () => {
       const operationError = new Error('Operation failed');
+
+      // 确保桶存在成功
+      mockClient.bucketExists = vi.fn().mockResolvedValue(true);
+      // 但文件操作失败
       mockClient.putObject = vi.fn().mockRejectedValue(operationError);
 
       await expect(minioService.createObject(
@@ -274,7 +285,8 @@ describe('MinIO Integration Tests', () => {
     it('should handle large file operations', async () => {
       // Use smaller buffer size to avoid memory issues
       const largeBuffer = Buffer.alloc(1 * 1024 * 1024); // 1MB instead of 10MB
-      mockClient.putObject = vi.fn().mockResolvedValue(undefined);
+      mockClient.bucketExists = vi.fn().mockResolvedValue(true);
+      mockClient.putObject = vi.fn().mockResolvedValue({ etag: 'etag123', versionId: null });
 
       await minioService.createObject('test-bucket', 'large-file.bin', largeBuffer, 'application/octet-stream');