I am struggling to jest mock the below method. Not able to mock. Please see my test case below it. Test case fails with error
TypeError: XXXXXXX is not a function.
When I run coverage report, it says all lines are covered. What is that I am missing?
import { Lambda } from "aws-sdk";
import fsReadFilePromise = require("fs-readfile-promise");
export class AWSLambdaDeployer {
readonly mylambda: Lambda;
public async deploy(zipPath: string, fName: string) {
const fileData = await fsReadFilePromise(zipPath);
const params: Lambda.Types.UpdateFunctionCodeRequest = {
FunctionName: fName,
ZipFile: fileData,
Publish: true
};
return this.mylambda.updateFunctionCode(params).promise();
}
}
Below is my jest test case
const mockUpdateFunctionCode = jest.fn().mockResolvedValueOnce('Ready');
jest.mock("../node_modules/aws-sdk/clients/lambda", () =>{
return{
updateFunctionCode: mockUpdateFunctionCode,
}
});
jest.mock("fs-readfile-promise", () => {
return jest.fn();
});
import { Lambda } from "aws-sdk";
import fsReadFilePromise = require("fs-readfile-promise");
import { AWSLambdaDeployer } from "../src/index";
describe('LambdaDeployer class', () => {
afterEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
});
it(' functionality under test', async () =>{
(fsReadFilePromise as any).mockResolvedValueOnce('get it done');
const awslambdaDeployer = new AWSLambdaDeployer();
const actual = await awslambdaDeployer.deploy('filePath', 'workingFunction');
expect(fsReadFilePromise).toBeCalledWith('filePath');
})
});
This gives me error as below. The test case fails. The coverage report shows all lines covered.
TypeError: this.mylambda.updateFunctionCode is not a function
Here is the unit test solution:
index.ts
:index.spec.ts
:Unit test result with 100% coverage:
Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59463491