I’m trying to write unit test for my authenticateUser
method.In side that method i’m generating private key signed jwt token and return to the user.But my signToken
method seem not mocked properly.it return Promise {}
.
userService.ts
async authenticateUser(authDto: AuthDTO): Promise<AuthResponse> {
const formatedMobile = this.commonHelper.formatMobile(
decodedIdToken.Mobile,
);
const userData = await this.userRepository.findOne({
msisdn: formatedMobile,
});
if (!userData) {
//save user info
const user = new UserEntity();
user.firstName = decodedIdToken['first_name'];
user.lastName = decodedIdToken['last_name'];
user.msisdn = formatedMobile;
await this.userRepository.save(user);
}
const jwtPayload={createdTime: moment(),msisdn: formatedMobile}
const jwtToken = this.commonHelper.signToken(jwtPayload);
return new AuthResponse(
HttpStatus.OK,
STRING.SUCCESS,
`${STRING.USER} ${STRING.AUTHENTICATE} ${STRING.SUCCESS}`,
{jwtToken},
);
}
userService.specs.ts
it('authenticateUser should return success response (User Exist)', async () => {
const mockJWTToken = {
jwtToken:"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDgyNjIzMDYsImRhdGEiOnsiY3JlY"
}
const mockAuthBody = { authCode: 'c79da50f-4eb1',platform:'MOBILE' };
const mockSuccessResponse = new AuthResponse(
HttpStatus.OK,
STRING.SUCCESS,
`${STRING.USER} ${STRING.AUTHENTICATE} ${STRING.SUCCESS}`,
mockJWTToken,
);
jest.spyOn(commonHelper, 'formatMobile');
jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(userEntity);
jest.spyOn(commonHelper, 'signToken').mockResolvedValueOnce(mockJWTToken);
expect(await userService.authenticateUser(mockAuthBody)).toEqual(
mockSuccessResponse,
);
});