I have a function like so:
invoke(token: string): Promise<IUserDto> {
return new Promise((reject, resolve) => {
// Tokens are generally passed in the header of the request
// Due to security reasons.
let jwtSecretKey = process.env.JWT_SECRET_KEY;
try {
const verified = jwt.verify(token, jwtSecretKey!);
if(verified){
resolve(verified);
}else{
// Access Denied
reject();
}
} catch (error) {
// Access Denied
reject();
}
});
}
And im trying to test it like so:
it('Validate user token use case', async () => {
let jwtSecretKey = process.env.JWT_SECRET_KEY;
let userData: IUserDto = {
id: 'testid',
firstName: 'firstName',
lastName: 'lastName',
username: 'username',
email: '[email protected]',
password: 'testpassword',
birthDate: new Date('0'),
reports: [
new Report('report_id', 'report data', 0)
]
};
let token = jwt.sign(userData, jwtSecretKey!);
let validateUserTokenUseCase = new ValidateUserTokenUseCase();
let validated: IUserDto | String = await validateUserTokenUseCase.invoke(token);
expect(validated.id).toBe(userData.id);
})
I cannot do this because when rejecting the promise in the function, it doesnt return an object of IUserDto, it returns whatever error string i put in the reject.
I have tried defining the return type like Promise<IUserDto | String>
, but my IDE doesnt pick this up well, i cant see for example validated.id
when typing it in my IDE, which is annoying.
Is there a way to reject this method without defining the promise type as IUserDto | String
?