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

170
Views
Calling jest.clearAllMocks() in beforeEach clears calls within a test?

I'm seeing an issue in Jest where calling jest.clearAllMocks() inside a beforeEach callback seems to wipe out calls to a mocked function that are made not before but WITHIN a specific test. I can repro it as follows:

THIS PASSES:

__tests__/testy.test.js

const AWS = require("aws-sdk");
const { handler } = require("../index.js");

describe("Lambda function tests", () => {
    it("sent the expected CSV data to S3", async () => {
        // RUN THE LAMBDA, await completion
        await handler();
        // The S3 constructor should have been called during Lambda execution
        expect(AWS.S3).toHaveBeenCalled();
    });
});

THIS GIVES ME A FAILURE:

__tests__/testy.test.js

const AWS = require("aws-sdk");
const { handler } = require("../index.js");

describe("Lambda function tests", () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });
    it("sent the expected CSV data to S3", async () => {
        // RUN THE LAMBDA, await completion
        await handler();
        // The S3 constructor should have been called during Lambda execution
        expect(AWS.S3).toHaveBeenCalled();
    });
});

The failure message in the console:

expect(jest.fn()).toHaveBeenCalled()

    Expected number of calls: >= 1
    Received number of calls:    0

I'm using jest version 27.4.3 in Node v14.18.1. In case it matters, I'm mocking aws-sdk like this:

__mocks__/aws-sdk.js

const AWS = jest.createMockFromModule("aws-sdk");

AWS.S3 = jest.fn().mockImplementation(() => ({
    upload: jest.fn(() => ({ promise: jest.fn() })),
    headObject: jest.fn(() => ({
        promise: jest.fn(() => ({ ContentLength: 123 })),
    })),
}));

module.exports = AWS;

What could be happening here?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I resolved this by restructuring my main code. Previously I was instantiating S3 outside of the handler function:

index.js

const AWS = require("aws-sdk");
const S3 = new AWS.S3();

exports.handler = async () => {
    S3.upload({ ... });
}

Now I'm doing it this way, and the problem is resolved:

index.js

const AWS = require("aws-sdk");

exports.handler = async () => {
    const S3 = new AWS.S3();
    S3.upload({ ... });
}

In hindsight, it makes sense. index.js is run before beforeEach executes, not each time a test is run.

about 4 years ago · Juan Pablo Isaza Report
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!