Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!