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

193
Visualizações
Jest Auto Mocking Issue - AWS SDK - Not properly auto mocking

I've ran into a strange issue when attempting to auto mock the AWS S3 client. I'm trying to write test cases to check if the S3 Class is instantiated with the correct parameters.

To write my test cases I'm auto mocking the S3 client which works for testing the upload function in S3. The problem I'm having is with the deleteObject function, every time I run my test around this function, Jest complains TypeError: s3Bucket.deleteObject is not a function

It seems like Jest isn't properly auto mocking the deleteObject function.

Wondering if anyone has any ideas here? I've tried various approaches and cannot figure out how to get around this...

Thank you!

Source file

import S3 from "aws-sdk/clients/s3";

function uploadToS3(bucketParams, cb) {
  const s3Bucket = new S3(bucketParams);
  s3Bucket.upload({ Key: "test", Body: "test" }, cb);
}

function deleteFromS3(bucketParams, cb) {
  const s3Bucket = new S3(bucketParams);
  s3Bucket.deleteObject({ Key: "test", Body: "test" }, cb);
}

module.exports = { uploadToS3, deleteFromS3 };

Test file:

jest.mock("aws-sdk/clients/s3");
import S3 from "aws-sdk/clients/s3";

const targetModule = require("source code");

// this passes as expected
it("should set s3 bucket parameters on upload", async () => {
  const bucketParams = { ... some params };

  targetModule.uploadToS3(bucketParams, (err: any) => {
    expect(S3).toBeCalledTimes(1);
    expect(S3).toBeCalledWith(bucketParams);
    expect(S3.prototype.upload).toBeCalledTimes(1);
  });
});

// this fails - TypeError: s3Bucket.deleteObject is not a function
it("should set s3 bucket parameters on delete", () => {
  const bucketParams = { ...some params };

  targetModule.deleteFromS3(bucketParams, (err: any) => {
    expect(S3).toBeCalledTimes(1);
    expect(S3).toBeCalledWith(bucketParams);
    expect(S3.prototype.deleteObject).toBeCalledTimes(1);
  });
});
about 4 years ago · Juan Pablo Isaza
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