如何用Jest模拟S3 PutObject使用Promise功能

发布于 2025-01-25 09:52:37 字数 1806 浏览 0 评论 0原文

我在这里看到答案模拟AWS-SDK S3#putObject实例,但它没有正常工作,并且根本没有解释,所以我不知道发生了什么。

我有一个功能:

const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.saveImageToS3 = async (params) => { 
    try {
        const s3resp = await s3.putObject(params).promise();
        /* Happy path response looks like this:
                    data = {
                        ETag: "\"6805f2cfc46c0f04559748bb039d69ae\"", 
                        VersionId: "psM2sYY4.o1501dSx8wMvnkOzSBB.V4a" // version optional
                    }*/
        if (s3resp.hasOwnProperty('ETag')) { // expected successful response            
            return { success: true, key: params.Key }  // returning key of item if we have reason to think this was successful.
        } else {
            console.warn("Unexpected s3 response format: ", s3resp)
            return s3resp;
        }

    } catch (err) {
        throw err;
    }
}

我想测试它是否有效。

我不明白如何模拟S3.pupobject函数。

我已经尝试了:

describe('FUNCTION: saveImageToS3', () => {
    const mockedPutObject = jest.fn();
    jest.mock('aws-sdk', () => {
        return class S3 {
            putObject(params, cb) {
                console.log("Mocked putObject function");
                mockedPutObject(params, cb);
            }
        }
    });

    test('returns success object with correct key value', async () => {
        await expect(await utils.saveImageToS3(params)).toEqual({ success: true, key: `original/testCamId/testCamId___2022-04-06T06-30-59Z.jpg` })
    })
})

根据上述答案,但是测试失败(实际上是),并且输出“模拟的putobject函数”永远不会写入控制台,告诉我未使用模拟的AWS-SDK。 。

I see the answer here Mocking aws-sdk S3#putObject instance method using jest, but it's not working as-is and it's not explained at all so I don't know what's going on.

I have a function:

const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.saveImageToS3 = async (params) => { 
    try {
        const s3resp = await s3.putObject(params).promise();
        /* Happy path response looks like this:
                    data = {
                        ETag: "\"6805f2cfc46c0f04559748bb039d69ae\"", 
                        VersionId: "psM2sYY4.o1501dSx8wMvnkOzSBB.V4a" // version optional
                    }*/
        if (s3resp.hasOwnProperty('ETag')) { // expected successful response            
            return { success: true, key: params.Key }  // returning key of item if we have reason to think this was successful.
        } else {
            console.warn("Unexpected s3 response format: ", s3resp)
            return s3resp;
        }

    } catch (err) {
        throw err;
    }
}

I'd like to test if it works.

I do not understand how to mock the s3.putObject function.

I have tried:

describe('FUNCTION: saveImageToS3', () => {
    const mockedPutObject = jest.fn();
    jest.mock('aws-sdk', () => {
        return class S3 {
            putObject(params, cb) {
                console.log("Mocked putObject function");
                mockedPutObject(params, cb);
            }
        }
    });

    test('returns success object with correct key value', async () => {
        await expect(await utils.saveImageToS3(params)).toEqual({ success: true, key: `original/testCamId/testCamId___2022-04-06T06-30-59Z.jpg` })
    })
})

per the above-linked answer, but the test fails (times out, actually) and the output "Mocked putObject function" never is written to the console, telling me the mocked aws-sdk isn't being used...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文