我正在使用这个包装器函数将对象上传到s3
// upload.js
async function uploadToS3 (body, bucket, key) {
console.log(`Uploading data to s3://${bucket}${key}`)
await s3
.upload({
Body: body,
Bucket: bucket,
Key: key
})
.promise()
.then((data) => {
console.log(`Successfully uploaded data to ${data.Location}`)
}
)
.catch((err) => {
console.error(err)
})
}我正在尝试使用aws-sdk-mock来模拟这个函数,这样当它被调用时,它实际上不会将项目推送到s3,我可以验证它是记录成功还是失败。
以下是我尝试过的方法
// upload.test.js
describe( 'uploadToS3', () => {
test('should upload a file to s3', async () => {
const body = 'test|data'
const bucket = 'testBucket'
const key = 'testKey'
AWS.mock( 'S3', 'upload',function (params, callback){
callback(null, 'successfully put item in s3');
});
await util.uploadToS3(body, bucket, key)
})
})不幸的是,当我调用uploadToS3函数时,它仍然使用实际的s3.upload实现并尝试将对象发送到S3。我在其他AWS服务中使用过类似的模仿方法,并取得了成功,但这一次似乎给我带来了问题。
如何模拟AWS.S3.upload函数以及后续的.then和.catch函数?
也许用Jest可以做到这一点?
发布于 2021-04-22 12:53:59
您不需要使用aws-sdk-mock包。您可以自行使用jest.mock(moduleName, factory, options)方法模拟aws-sdk包。
例如。
upload.js
import AWS from 'aws-sdk';
const s3 = new AWS.S3();
export async function uploadToS3(body, bucket, key) {
console.log(`Uploading data to s3://${bucket}${key}`);
await s3
.upload({
Body: body,
Bucket: bucket,
Key: key,
})
.promise()
.then((data) => {
console.log(`Successfully uploaded data to ${data.Location}`);
})
.catch((err) => {
console.error(err);
});
}upload.test.js
import { uploadToS3 } from './upload';
import AWS from 'aws-sdk';
jest.mock('aws-sdk', () => {
const mockedS3 = {
upload: jest.fn().mockReturnThis(),
promise: jest.fn(),
};
return { S3: jest.fn(() => mockedS3) };
});
describe('uploadToS3', () => {
afterAll(() => {
jest.resetAllMocks();
});
afterEach(() => {
jest.clearAllMocks();
});
it('should upload file to s3', async () => {
const mockedS3 = new AWS.S3();
const logSpy = jest.spyOn(console, 'log');
mockedS3.promise.mockResolvedValueOnce({ Location: 'us' });
const body = 'test|data';
const bucket = 'testBucket';
const key = 'testKey';
await uploadToS3(body, bucket, key);
expect(mockedS3.upload).toBeCalledWith({ Body: body, Bucket: bucket, Key: key });
expect(mockedS3.promise).toBeCalledTimes(1);
expect(logSpy).toBeCalledWith('Successfully uploaded data to us');
});
it('should handle error when upload file to s3 failed', async () => {
const mockedS3 = new AWS.S3();
const errorLogSpy = jest.spyOn(console, 'error');
const mError = new Error('network');
mockedS3.promise.mockRejectedValueOnce(mError);
const body = 'test|data';
const bucket = 'testBucket';
const key = 'testKey';
await uploadToS3(body, bucket, key);
expect(mockedS3.upload).toBeCalledWith({ Body: body, Bucket: bucket, Key: key });
expect(mockedS3.promise).toBeCalledTimes(1);
expect(errorLogSpy).toBeCalledWith(mError);
});
});单元测试结果:
PASS examples/67204024/upload.test.js (6.42 s)
uploadToS3
✓ should upload file to s3 (16 ms)
✓ should handle error when upload file to s3 failed (6 ms)
console.log
Uploading data to s3://testBuckettestKey
at console.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)
console.log
Successfully uploaded data to us
at console.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)
console.log
Uploading data to s3://testBuckettestKey
at console.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)
console.error
Error: network
at /Users/dulin/workspace/github.com/mrdulin/jest-v26-codelab/examples/67204024/upload.test.js:35:20
at Generator.next (<anonymous>)
at /Users/dulin/workspace/github.com/mrdulin/jest-v26-codelab/examples/67204024/upload.test.js:8:71
at new Promise (<anonymous>)
at Object.<anonymous>.__awaiter (/Users/dulin/workspace/github.com/mrdulin/jest-v26-codelab/examples/67204024/upload.test.js:4:12)
at Object.<anonymous> (/Users/dulin/workspace/github.com/mrdulin/jest-v26-codelab/examples/67204024/upload.test.js:32:70)
at Object.asyncJestTest (/Users/dulin/workspace/github.com/mrdulin/jest-v26-codelab/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)
at /Users/dulin/workspace/github.com/mrdulin/jest-v26-codelab/node_modules/jest-jasmine2/build/queueRunner.js:45:12
at new Promise (<anonymous>)
at mapper (/Users/dulin/workspace/github.com/mrdulin/jest-v26-codelab/node_modules/jest-jasmine2/build/queueRunner.js:28:19)
at /Users/dulin/workspace/github.com/mrdulin/jest-v26-codelab/node_modules/jest-jasmine2/build/queueRunner.js:75:41
at processTicksAndRejections (internal/process/task_queues.js:93:5)
16 | })
17 | .catch((err) => {
> 18 | console.error(err);
| ^
19 | });
20 | }
21 |
at console.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)
at examples/67204024/upload.js:18:15
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 6.896 s, estimated 7 shttps://stackoverflow.com/questions/67204024
复制相似问题