Issues with mocking AWS SDK calls in Jest for a Node.js Lambda function
I'm currently working on unit tests for a Node.js Lambda function that interacts with AWS services using the AWS SDK. I've set up Jest for testing, but I'm having trouble effectively mocking the SDK calls, specifically the S3 upload method. Despite using `jest.mock` to mock the `AWS.S3` class, the tests are failing with the following error message: `Error: A promise was not returned from jest.fn()`. Here's a simplified version of my Lambda function: ```javascript const AWS = require('aws-sdk'); const s3 = new AWS.S3(); exports.handler = async (event) => { const params = { Bucket: 'my-bucket', Key: 'my-object', Body: event.body, }; return s3.upload(params).promise(); }; ``` In my test file, I attempted to mock the S3 upload like this: ```javascript const AWS = require('aws-sdk'); const myLambda = require('./myLambda'); jest.mock('aws-sdk', () => { return { S3: jest.fn(() => ({ upload: jest.fn(() => ({ promise: jest.fn().mockResolvedValue('Success') })) })) }; }); test('should upload to S3 and return success message', async () => { const event = { body: 'Test data' }; const response = await myLambda.handler(event); expect(response).toBe('Success'); }); ``` When I run the test, it fails at the `await myLambda.handler(event)` line, and I keep getting the same promise error. I've confirmed that the mock setup is correct and that Jest is indeed mocking the AWS SDK, but it seems like the promise is not being handled correctly. I also tried returning a resolved promise directly from the mocked `upload` method, but that didn't resolve the issue either. Could someone help me understand why the promise isn't being returned correctly in my mock setup, or suggest a better approach for mocking AWS SDK calls in Jest?