m-radzikowski / aws-sdk-client-mock

AWS JavaScript SDK v3 mocks for easy unit testing. 🖋️ Typed 🔬 Tested 📄 Documented 🛠️ Maintained
https://m-radzikowski.github.io/aws-sdk-client-mock/
MIT License
749 stars 37 forks source link

Upload throws error when using mocked S3 client #206

Closed jesterhazy closed 3 months ago

jesterhazy commented 5 months ago

Checklist

Bug description

The Upload class (from @aws-sdk/lib-storage) throws a TypeError when used with a mocked S3 client.

Trace:

TypeError: Cannot read properties of undefined (reading 'requestHandler')
    at _Upload.__uploadUsingPut (~/mock-s3-upload-issue/node_modules/@aws-sdk/lib-storage/dist-cjs/index.js:201:41)
    at _Upload.__doConcurrentUpload (~/mock-s3-upload-issue/node_modules/@aws-sdk/lib-storage/dist-cjs/index.js:280:29)
    at async Promise.all (index 0)
    at async _Upload.__doMultipartUpload (~/mock-s3-upload-issue/node_modules/@aws-sdk/lib-storage/dist-cjs/index.js:366:5)
    at async _Upload.done (~/mock-s3-upload-issue/node_modules/@aws-sdk/lib-storage/dist-cjs/index.js:190:12)

Reproduction

const {mockClient} = require('aws-sdk-client-mock');
const {S3Client, PutObjectCommand} = require('@aws-sdk/client-s3');
const {Upload} = require('@aws-sdk/lib-storage');

class S3ClientWrapper {        
    constructor(client) {
        this.client = client;
    }

    async doUpload(bucket, key, data) {
        const upload = new Upload({
            client: this.client,
            params: {
                Bucket: bucket,
                Key: key,
                Body: data
            }
        });

        const response = await upload.done();
        console.log(response);        
    }
}

async function main() {
    const s3Mock = mockClient(S3Client);
    s3Mock.on(PutObjectCommand).resolves({});

    const wrapper = new S3ClientWrapper(s3Mock);

    await wrapper.doUpload('bucket', 'key', 'some data');
}

main();

Environment

paul-uz commented 5 months ago

Seems to have broken with the latest SDK v3 versions

jesterhazy commented 5 months ago

Can you identify a version this works on? The lines that the SDK throws on haven’t been modified recently (7-8 months or more).

On Tue, Jan 23, 2024 at 8:36 AM Paul Canning @.***> wrote:

Seems to have broken with the latest SDK v3 versions

— Reply to this email directly, view it on GitHub https://github.com/m-radzikowski/aws-sdk-client-mock/issues/206#issuecomment-1906460210, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAD3LKSY4RHOQZCAJLQQ7BTYP7RH3AVCNFSM6AAAAABCCOM7VGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBWGQ3DAMRRGA . You are receiving this because you authored the thread.Message ID: @.***>

m-radzikowski commented 3 months ago

@jesterhazy the problem is you are providing the mockClient(S3Client) output as an SDK Client to the Upload. The mockClient() returns AwsClientStub, an object with methods to set up mock behavior. It's not the Client that you create with new S3Client().

In line 30 of your snippet you should create and pass a new S3 Client with new S3Client() to the S3ClientWrapper.

Also, please see the example in the README on mocking lib-storage.