Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
Reject promise with string in function that has a different return type

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: 'test@test.com',
        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?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda