I'm totally new to Jest and typescript. my 2nd test case to be honest. I want in my jest test - when s3.getObject is called in the actual class, it should return the mocked value.
my handler code:
var aws = require("aws-sdk");
var s3 = new aws.S3({apiVersion: '2006-03-01'});
exports.handler = async function (event, context, callback) {
const bucket = 'event.s3.bucket.name';
const filename = 'fileName';
const inputBucketParams = {
Bucket: bucket,
Key: filename,
};
let result;
try {
**//I want the result to be the mocked value in my test case.**
result = await s3.getObject(inputBucketParams).promise();
const fileContent = getFileContents(result.Body.toString("utf-8"));
my test case:
import {getFileContent} from "../../utils/FileContent";
import anything = jasmine.anything;
const lambdaHandler = require('../lambda/myLambda');
const AWSMock = require('aws-sdk-mock');
const AWS = require("aws-sdk");
describe('Test getS3Object', () => {
beforeEach(() => AWSMock.setSDKInstance(AWS));
})
let s3Object = {
"bucket": {
},
"object": {
}
};
let event = {Records: [
{
s3: s3Object --> from above.
}
]
}
var aws = require("aws-sdk");
var s3 = new aws.S3({apiVersion: '2006-03-01'});
describe('my handler test', async function () {
const s3GetObject = AWSMock.mock('S3', 'getObject', getFileContent('fileName.csv'));
const s3mock = jest.fn();
const getObjectMock = jest.fn(() => getFileContent('fileName.csv'));
const params = {
Bucket: 'bucketName',
Key: 'filename'
}
var returnValue = s3mock.mockReturnValue({
Body: getFileContent('fileName.csv')
});
test('s3 getObject response mock', async () => {
//jest.spyOn(s3, "getObject")
const getObjectMockValue = s3.spyOn('S3', 'getObject').mockReturnValue({
Body: getFileContent('fileName.csv')
})
// my handler is called from this, and when it get to s3.getObject, it fails.
const handlerResponse = await lambdaHandler.handler(event, anything(), anything());
});
});
I want in my jest test - when s3.getObject is called in the actual class, it should return the mocked value.