I am using TypeScript and I wish to create unit tests for functions in the same file that call one another.
I have the following function:
export function validateParentFeatureId(parentFeatureId: string | null) {
const isIdvalid = isParentFeatureIdValid(parentFeatureId);
if (!isIdvalid) throw new BadRequestError(FeatureErrorMessages.FeatureParentIdInvalid);
}
isParentFeatureIdValid
is in the same file as the function above. I don't want the functions to be in different files.
Whenever I create the following unit test it fails:
it('Should invoke isParentFeatureIdValid when validateParentFeatureId is invoked', () => {
//const isParentFeatureIdInValidSpy = jest.spyOn(validators, 'isParentFeatureIdValid');
mockedIsParentFeatureIdInValid.mockReturnValueOnce(true);
validators.validateParentFeatureId(null)
expect(mockedIsParentFeatureIdInValid).toBeCalled();
});
I recieve the following error:
expect(jest.fn()).toBeCalled()
Expected number of calls: >= 1
Received number of calls: 0
I'm not sure why it fails when I have always spied on functions and it worked fine.