I am not expert in jest, but my recommendation to think about:

1) When the function is exported as default I use something like:

import Funct1 from "../../../src/components/Funct1";
...
jest.mock("../../../src/components/Funct1");
...
expect(Funct1).toHaveBeenCalledWith(params);

2) When the module (utils.js) has multiple exports as

export const f1 = () => {};
...
export const f8 = () => {};

You can try

import * as Utils from "../../../src/components/utils"
const f8Spy = jest.spyOn(Utils, 'f8');
...
expect(f8Spy).toHaveBeenCalledWith(params);

Similar discussion here

Answer from Dima Dorogonov on Stack Overflow
🌐
Position Is Everything
positioniseverything.net › home › jest spyon: all you need to know about this function
Jest Spyon: All You Need To Know About This Function - Position Is Everything
November 12, 2025 - The user can use Jest.spyOn to return specified values if getName or getAddress is called. However, Jest requires the user to first provide an object in order to use spyOn. The user import significantly affects how they mock a particular function ...
Discussions

Cannot spy on individual functions that are individually exported
Having done a lot of research I cannot find a way to mock functions that are exported with no parent object. For example I'm trying to mock functions exported the following way: module.exports ... More on github.com
🌐 github.com
69
August 22, 2017
How to mock only a non-default function in Jest
I think you need to set __esModule: true in your jest object in order for the default export to work properly. There's an example here: https://archive.jestjs.io/docs/en/23.x/jest-object More on reddit.com
🌐 r/reactjs
4
4
November 6, 2023
When Unit Testing with Jest, How do you Mock Getter Properties?

Why aren't you just mocking the entire authentication service?

More on reddit.com
🌐 r/Angular2
4
2
November 25, 2019
[AskJS] Am I the only one that just cannot grasp how to mock in tests?
Do you understand how the CommonJS module system works? A lot of the "magic" here is because mocks work by hooking into the module system to change how and where require statements find their files. More on reddit.com
🌐 r/javascript
52
90
August 24, 2022
People also ask

What Does Jest.clearallmocks() Function Do?
The jest.clearAllMocks() function clears all the mock.calls, mock.instances, mock.contexts and mock.results properties of all mocks. This function is equivalent to calling a .mockClear() function on every mocked function. It returns the jest object for chaining.
🌐
positioniseverything.net
positioniseverything.net › home › jest spyon: all you need to know about this function
Jest Spyon: All You Need To Know About This Function - Position ...
What Does the Jest.runonlypendingtimers() Function Do in the Program?
The Jest.runOnlyPendingTimers() function only executes the macro-tasks that are currently pending, such as tasks that are queued by setTimeout() or setInterval() functions, when any of the pending macro-tasks schedule the new macro-tasks, those tasks will not be executed by this call. This is useful in a program when the module being tested schedules a setTimeout(), and its callback schedules another setTimeout() recursively. This means the scheduling process never stops. In such situations, using Jest.runOnlyPendingTimers() is useful.
🌐
positioniseverything.net
positioniseverything.net › home › jest spyon: all you need to know about this function
Jest Spyon: All You Need To Know About This Function - Position ...
What Does Jest.restoreallmocks() Do?
The jest.restoreAllMocks() function restores all the mocks back to their original value. This function is equivalent to calling a .mockRestore() function on every mocked function. However, the Jest.restoreAllMocks() only works when the mock is created with Jest.spyOn. The other mocks will be required to be done manually to restore them.
🌐
positioniseverything.net
positioniseverything.net › home › jest spyon: all you need to know about this function
Jest Spyon: All You Need To Know About This Function - Position ...
🌐
DhiWise
dhiwise.com › post › grow-testing-efficiency-with-jest-spyon-for-default-exports
A Deep Dive into Jest spyOn for Default Exports
April 30, 2025 - When you spy on only the default export, you might encounter issues if the default export is not an object or a class instance. In such cases, you may need to employ workarounds or adjust your testing strategy. The main difference between a mock function and a spyOn function in Jest is that mocking completely replaces the function with a fake version, while spying wraps the actual function, allowing you to observe its behavior without changing its implementation.
🌐
CopyProgramming
copyprogramming.com › howto › javascript-jest-spyon-function-without-object
Spying on a JavaScript function without an object using Jest could be a possible - Javascript
August 1, 2023 - Is there an alternative method to testing these functions without making significant changes to the codebase? ... In the given code snippets, I need to write unit tests for function A , which executes function B . To set mock return value to true, I have to use B which contains a set of API calls for validation. Despite utilizing the spyOn method, the API calls present in B are still executed. Other techniques like mocking the complete module with jest.requireActual() have also been attempted but have proven ineffective.
🌐
IQCode
iqcode.com › code › javascript › jest-spyon
jest spyon Code Example
September 8, 2021 - //class.js class MyClass { methodOne() { return 1; } methodTwo() { return 2; } } module.exports = MyClass; // class.test.js test('spy using class method', () => { const result = new MyClass() const spy = jest.spyOn(result, 'methodOne') result.methodOne() // check class method is call or not expect(spy).toHaveBeenCalled() // expect old value expect(result.methodOne()).toBe(1) // expect new value spy.mockReturnValueOnce(12) expect(result.methodOne()).toBe(12) }) ... Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond. Sign up · Develop soft skills on Bra
🌐
GitHub
github.com › jasmine › jasmine › issues › 1414
Cannot spy on individual functions that are individually exported · Issue #1414 · jasmine/jasmine
August 22, 2017 - Having done a lot of research I cannot find a way to mock functions that are exported with no parent object. For example I'm trying to mock functions exported the following way: ... import {thing} from 'emvio-util-responses; //call some function that calls thing() spyOn(???, 'thing').and.returnValue({}); expect(???.thing).toHaveBeenCalled();
Author   kevinlbatchelor
🌐
Zirkelc
zirkelc.dev › posts › jest-spy-on-classes
How To Spy on Classes
June 28, 2023 - class Person { static hello() { return 'Hello, I am a static method.'; } goodbye() { return 'Goodbye, I am an instance method.'; } } // Create an instance and call the instance method function goodbye() { const person = new Person(); return person.goodbye(); } test('should call instance method of all objects', () => { // Spy on the instance method for all instances const spy = jest.spyOn(Person.prototype, 'goodbye'); // Call function without access to underlying instance goodbye(); goodbye(); // Test if the method has been called on any instance expect(spy).toHaveBeenCalledTimes(2); });
Find elsewhere
🌐
Chakshunyu
chakshunyu.com › blog › how-to-spy-on-a-named-import-in-jest
How To Spy On An Exported Function In Jest | A technical blog by Chak Shun Yu
import * as moduleApi from '@module/api'; // Somewhere in your test case or test suite jest.spyOn(moduleApi, 'functionToMock').mockReturnValue({ someObjectProperty: 42 }); One caveat to this approach is that there’s the chance that you will run across a TypeError: Cannot redefine property: functionToMock at Function.defineProperty (<anonymous>). In that case, you can’t use the spyOn function to mock the exported function.
🌐
Medium
medium.com › javascript-journal-unlocking-project-potential › demystifying-jest-functions-mock-spyon-and-fn-a312fafb46b9
Demystifying Jest Functions: Mock, SpyOn, and Fn | by Rrish | JavaScript Journal: Unlocking Project Potential | Medium
May 2, 2025 - To test a function that uses fetchUser without making real API calls, you can mock the userApi module: // userApi.test.js import { fetchUser } from './userApi'; jest.mock('./userApi', () => ({ fetchUser: jest.fn(() => Promise.resolve({ id: 1, name: 'John Doe' })), })); test('fetches a user', async () => { const…
🌐
Jest
jestjs.io › mock functions
Mock Functions · Jest
1 week ago - Mock functions are also known as "spies", because they let you spy on the behavior of a function that is called indirectly by some other code, rather than only testing the output.
🌐
Meticulous
meticulous.ai › blog › how-to-use-jest-spyon
How to use Jest spyOn with React.js and Fetch
On the contrary, now it is a bit ... using spyOn on an object method is easier. Jest spyOn can target only the function relevant for the test rather than the whole object or module....
🌐
Jest
jestjs.io › the jest object
The Jest Object · Jest
1 week ago - ... Returns a mock module instead of the actual module, bypassing all checks on whether the module should be required normally or not. Registers a callback function that is invoked whenever Jest generates a mock for a module.
🌐
Frontend Masters
frontendmasters.com › courses › testing-practices-principles › using-jest-spyon
Using Jest spyOn - JavaScript Testing Practices and ...
Whether you want to learn professional JavaScript and TypeScript, to back-end courses on Node.js, SQL, and beyond we have courses to bring your skills to the next level!
🌐
DEV Community
dev.to › devin-rosario › complete-guide-to-jestspyon-for-unit-testing-4io6
Complete Guide to Jest.spyOn for Unit Testing - DEV Community
October 16, 2025 - Testing React components without jest.spyOn feels like debugging in production. You know something broke. Just cannot prove where or why. Catherine Angel, frontend developer documenting testing patterns in May 2024, observes: "Spies allow you to monitor the behavior of functions indirectly called by other code.
🌐
DEV Community
dev.to › qmenoret › mocks-and-spies-with-jest-32gf
Mocks and Spies with Jest - DEV Community
December 31, 2020 - If you don't importing the module will just return an empty object. It is also possible to write this factory in a specific file. For instance if you were to import the file src/my-module.js, and wanted a specific factory for it in every test, ...
🌐
Medium
medium.com › @catherineangelr › testing-with-spy-and-mock-in-jest-a-beginners-guide-7a25f87010c2
Testing with Spy, Mock, and Stub in Jest: A Beginner’s Guide | by Catherine Angel | Medium
May 21, 2024 - Spies are functions that allow you to spy the behavior of functions which is called indirectly by other codes. To create spy, you can use jest.spyOn() with 2 arguments given: the object and the method. Some functionalities that are provided by spy functions are tracking the number of times the funtion has been called, the parameters that is is called with and also the value it returns.
🌐
Code with Hugo
codewithhugo.com › jest-fn-spyon-stub-mock
Jest .fn() and .spyOn() spy/stub/mock assertion reference · Code with Hugo
November 5, 2019 - Jest spies are instantiated using jest.spyOn(obj, 'functionName'). Note: you can’t spy something that doesn’t exist on the object. jest.toBeCalled() and jest.toHaveBeenCalled() are aliases of each other. expect(stubOrSpy).toBeCalled() passes ...
🌐
Reddit
reddit.com › r/reactjs › how to mock only a non-default function in jest
r/reactjs on Reddit: How to mock only a non-default function in Jest
November 6, 2023 -

I am trying to mock a non default function in jest which is part of a larger module and I don't want to mock any of the rest of it. I tried something along the lines of the solution found here (https://stackoverflow.com/questions/59312671/mock-only-one-function-from-module-but-leave-rest-with-original-functionality). The problem is that importing the default function gives you the whole module instead of the default function.

For example:

// myModule.js
export const foo = () => {
    console.log("in foo");
};

const bar = () => {
    console.log("in bar");
};

export default bar;
// useMyModule.test.js
import bar, { foo } from "./myModule";
import "./useMyModule";

jest.mock("./myModule", () => ({
    ...jest.requireActual("./myModule"),
    foo: jest.fn(() => {
        console.log("in test foo");
    }),
}));

test("see if this works", () => {
    console.log(bar);
    foo();
});
// useMyModule.js
import bar from "./myModule";

console.log(bar);
//bar(); // <== commented out because bar is not the function I expect it to be

This is what the output looks like:

 PASS  src/useMyModule.test.js
  ● Console

    console.log
      {
        default: [Function: bar],
        foo: [Function: mockConstructor] {
          _isMockFunction: true,
          getMockImplementation: [Function (anonymous)],
          mock: [Getter/Setter],
          mockClear: [Function (anonymous)],
          mockReset: [Function (anonymous)],
          mockRestore: [Function (anonymous)],
          mockReturnValueOnce: [Function (anonymous)],
          mockResolvedValueOnce: [Function (anonymous)],
          mockRejectedValueOnce: [Function (anonymous)],
          mockReturnValue: [Function (anonymous)],
          mockResolvedValue: [Function (anonymous)],
          mockRejectedValue: [Function (anonymous)],
          mockImplementationOnce: [Function (anonymous)],
          mockImplementation: [Function (anonymous)],
          mockReturnThis: [Function (anonymous)],
          mockName: [Function (anonymous)],
          getMockName: [Function (anonymous)]
        }
      }

      at Object.<anonymous> (src/useMyModule.js:3:9)

    console.log
      {
        default: [Function: bar],
        foo: [Function: mockConstructor] {
          _isMockFunction: true,
          getMockImplementation: [Function (anonymous)],
          mock: [Getter/Setter],
          mockClear: [Function (anonymous)],
          mockReset: [Function (anonymous)],
          mockRestore: [Function (anonymous)],
          mockReturnValueOnce: [Function (anonymous)],
          mockResolvedValueOnce: [Function (anonymous)],
          mockRejectedValueOnce: [Function (anonymous)],
          mockReturnValue: [Function (anonymous)],
          mockResolvedValue: [Function (anonymous)],
          mockRejectedValue: [Function (anonymous)],
          mockImplementationOnce: [Function (anonymous)],
          mockImplementation: [Function (anonymous)],
          mockReturnThis: [Function (anonymous)],
          mockName: [Function (anonymous)],
          getMockName: [Function (anonymous)]
        }
      }

      at Object.<anonymous> (src/useMyModule.test.js:12:13)

However, if you uncomment the bar() invocation above, you get this:

 FAIL  src/useMyModule.test.js
  ● Test suite failed to run

    TypeError: (0 , _myModule.default) is not a function

      2 |
      3 | console.log(bar);
    > 4 | bar();
        |    ^
      5 |

      at Object.<anonymous> (src/useMyModule.js:4:4)
      at Object.<anonymous> (src/useMyModule.test.js:2:1)

So, how should I mock the foo function, but leave the default bar function intact?