-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: helper to mock @aws-sdk/lib-storage Upload (#47)
* feat: helper to mock @aws-sdk/lib-storage Upload * fix: bump tsd to ignore type errors in node_modules * fix: add @aws-sdk/client-s3 to peerDependencies
- Loading branch information
1 parent
537380d
commit 10780e8
Showing
6 changed files
with
1,020 additions
and
942 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './mockClient'; | ||
export * from './awsClientStub'; | ||
export * from './libStorage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {AwsClientStub} from './awsClientStub'; | ||
import {CreateMultipartUploadCommand, S3Client, UploadPartCommand} from '@aws-sdk/client-s3'; | ||
import {mockClient} from './mockClient'; | ||
|
||
/** | ||
* Configures required command mocks of the S3Client mock to support Lib Storage Upload helper | ||
* for multipart file upload. | ||
* | ||
* If S3Client mocks is not provided, a new one is created. | ||
* @param s3Mock S3Client mock created with {@link mockClient} function | ||
*/ | ||
export const mockLibStorageUpload = (s3Mock?: AwsClientStub<S3Client>): AwsClientStub<S3Client> => { | ||
if (!s3Mock) { | ||
s3Mock = mockClient(S3Client); | ||
} | ||
|
||
s3Mock.on(CreateMultipartUploadCommand).resolves({UploadId: '1'}); | ||
s3Mock.on(UploadPartCommand).resolves({ETag: '1'}); | ||
|
||
return s3Mock; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {Progress, Upload} from '@aws-sdk/lib-storage'; | ||
import {S3Client} from '@aws-sdk/client-s3'; | ||
import {AwsClientStub, mockClient, mockLibStorageUpload} from '../src'; | ||
|
||
let s3Mock: AwsClientStub<S3Client>; | ||
|
||
afterEach(() => { | ||
s3Mock.restore(); | ||
}); | ||
|
||
it('mocks small file upload to S3', async () => { | ||
s3Mock = mockClient(S3Client); | ||
mockLibStorageUpload(s3Mock); | ||
|
||
const s3Upload = new Upload({ | ||
client: new S3Client({}), | ||
params: { | ||
Bucket: 'mock', | ||
Key: 'test', | ||
Body: 'qwe', | ||
}, | ||
}); | ||
|
||
const uploadProgress: Progress[] = []; | ||
s3Upload.on('httpUploadProgress', (progress) => { | ||
uploadProgress.push(progress); | ||
}); | ||
|
||
await s3Upload.done(); | ||
|
||
expect(uploadProgress).toHaveLength(1); | ||
expect(uploadProgress[0]).toStrictEqual({ | ||
Bucket: 'mock', | ||
Key: 'test', | ||
loaded: 3, | ||
total: 3, | ||
part: 1, | ||
}); | ||
}); | ||
|
||
it('mocks multipart upload to S3', async () => { | ||
s3Mock = mockClient(S3Client); | ||
mockLibStorageUpload(s3Mock); | ||
|
||
const s3Upload = new Upload({ | ||
client: new S3Client({}), | ||
partSize: 5 * 1024 * 1024, // 5 MB | ||
params: { | ||
Bucket: 'mock', | ||
Key: 'test', | ||
Body: 'x'.repeat(6 * 1024 * 1024), // 6 MB | ||
}, | ||
}); | ||
|
||
const uploadProgress: Progress[] = []; | ||
s3Upload.on('httpUploadProgress', (progress) => { | ||
uploadProgress.push(progress); | ||
}); | ||
|
||
await s3Upload.done(); | ||
|
||
expect(uploadProgress).toHaveLength(2); | ||
}); | ||
|
||
it('mocks multipart upload to S3 without explicit client mock', async () => { | ||
const localS3Mock = mockLibStorageUpload(); | ||
|
||
const s3Upload = new Upload({ | ||
client: new S3Client({}), | ||
partSize: 5 * 1024 * 1024, // 5 MB | ||
params: { | ||
Bucket: 'mock', | ||
Key: 'test', | ||
Body: 'x'.repeat(6 * 1024 * 1024), // 6 MB | ||
}, | ||
}); | ||
|
||
const uploadProgress: Progress[] = []; | ||
s3Upload.on('httpUploadProgress', (progress) => { | ||
uploadProgress.push(progress); | ||
}); | ||
|
||
await s3Upload.done(); | ||
|
||
expect(uploadProgress).toHaveLength(2); | ||
|
||
localS3Mock.restore(); | ||
}); |
Oops, something went wrong.