@@ -5,13 +5,14 @@ import { beforeEach, describe, expect, it, jest, mock } from 'bun:test';
5
5
6
6
import { FileService } from './file.service' ;
7
7
8
- mock . module ( '@aws-sdk/client-s3' , ( ) => {
9
- const mS3Client = {
10
- send : jest . fn ( ) ,
11
- } ;
8
+ // Create a mock S3Client instance
9
+ const mockS3Client = {
10
+ send : jest . fn ( ) ,
11
+ } ;
12
12
13
+ mock . module ( '@aws-sdk/client-s3' , ( ) => {
13
14
return {
14
- S3Client : jest . fn ( ( ) => mS3Client ) ,
15
+ S3Client : jest . fn ( ( ) => mockS3Client ) ,
15
16
GetObjectCommand : jest . fn ( ) ,
16
17
PutObjectCommand : jest . fn ( ) ,
17
18
HeadBucketCommand : jest . fn ( ) ,
@@ -31,6 +32,10 @@ describe('FileService', () => {
31
32
let s3Client : S3Client ;
32
33
33
34
beforeEach ( async ( ) => {
35
+ // Reset the mock before each test
36
+ mockS3Client . send . mockClear ( ) ;
37
+ mockS3Client . send . mockResolvedValue ( { } ) ;
38
+
34
39
const module : TestingModule = await Test . createTestingModule ( {
35
40
providers : [
36
41
FileService ,
@@ -62,8 +67,7 @@ describe('FileService', () => {
62
67
} ) . compile ( ) ;
63
68
64
69
fileService = module . get < FileService > ( FileService ) ;
65
-
66
- s3Client = new S3Client ( { } ) ;
70
+ s3Client = mockS3Client as any ;
67
71
} ) ;
68
72
69
73
it ( 'should be defined' , ( ) => {
@@ -72,13 +76,15 @@ describe('FileService', () => {
72
76
73
77
describe ( 'verifyBucket' , ( ) => {
74
78
it ( 'should verify the buckets successfully' , async ( ) => {
79
+ // The constructor already called verifyBucket, so we expect 2 calls from constructor
80
+ // When we call verifyBucket again, we expect 2 more calls (total 4)
75
81
( s3Client . send as jest . Mock )
76
82
. mockResolvedValueOnce ( { } ) // Mock for the first bucket
77
83
. mockResolvedValueOnce ( { } ) ; // Mock for the second bucket
78
84
79
85
await fileService [ 'verifyBucket' ] ( ) ;
80
86
81
- // Ensure the mock was called twice
87
+ // Ensure the mock was called 4 times total (2 from constructor + 2 from explicit call)
82
88
expect ( s3Client . send ) . toHaveBeenCalledTimes ( 4 ) ;
83
89
} ) ;
84
90
0 commit comments