Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
How to approach the following TypeScript code for testing?

I have been asked to test the following code, but have no idea about it.

import AWS from 'aws-sdk';
import db from './db';

async function uploadUserInfo(userID: number) {
const user = db.findByPk(userID);
    if(!user) throw new Error('User not Found')
const S3 = new AWS.S3();
await S3.putObject({
    Bucket: 'users',
    Key:userID,
    Body: JSON.stringify(user.get())
})
    
}

As far as I understand the code, it is an asynchronous function to upload user information, validate it, and uses a new instance of a AWS Class to put the userId into the database.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You might need to test it by inputing different values of UserID. Since I assume no other part of code has been given, you need to test it with varied input values only, and play around with the Error handling

about 4 years ago · Juan Pablo Isaza Relatório

0

As written, the function won't run as it has what appears to be an undeclared variable (order). So you can probably test that and return it to the dev as 'broken'.

It's testable assuming your test environment knows the database content, and that the AWS credentials are correct (and presumably not a live/production instance). If this is true, then you can supply known userID values and check that the AWS bucket receives the data.

The function is not well implemented in that its dependencies (db and AWS) are not injectable, and therefore they cannot easily be substituted with mock implementations for testing purposes.

Some testing tools might allow you to intercept the module loading so that you can provide a mock db and AWS implementation, and that would allow you to check that the relevant calls are made with the correct data, based on the userID parameter.

If you have the ability to modify the function so that it is more testible, then if you add the db and AWS instance as parameters, then you can supply mocks, and use those to validate the internal implementation.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda