Actually you can use jest.spyOn jest.spyOn

If method is called when component created use:

import { mount } from 'enzyme'; 

describe('My component', () => {
  it('should call getData', () => {
    const spy = jest.spyOn(Component.prototype, 'getData');
    mount(<Component />);
    expect(spy).toHaveBeenCalledTimes(1)
  });
})

or if you have it in your DOM and method use bind you can use:

import { shallow } from 'enzyme'; 

describe('My component', () => {
  it('should call getData', () => {
    const wrapper = shallow(<Component />);
    const instance = wrapper.instance()
    const spy = jest.spyOn(instance, 'getData');
    wrapper.find('button').simulate('click')
    expect(spy).toHaveBeenCalledTimes(1)
  });
})
Answer from Denis Rybalka on Stack Overflow
🌐
Jest
jestjs.io › the jest object
The Jest Object · Jest
May 7, 2026 - By default, jest.spyOn also calls the spied method. This is different behavior from most other test libraries.
🌐
Meticulous
meticulous.ai › blog › how-to-use-jest-spyon
How to use Jest spyOn with React.js and Fetch
This also verifies the country ISO code and percent are as expected, for example US - 4.84% for the US. Similarly, it inspects that there are flag images with expected alt text. Congratulations! Now we have successfully mocked the fetch call with Jest SpyOn and also verified the happy path result.
🌐
Echobind
echobind.com › post › how-to-mock-using-jest-spyon-part-2-3
How to Mock Using Jest.spyOn (Part 2)
October 16, 2019 - test('creates Contract on correct date', () => { const NOW = '2019-05-03T08:00:00.000Z'; const mockDateNow = jest .spyOn(global.Date, 'now') .mockImplementation(() => new Date(NOW).getTime()); const mutation = ` mutation createContract { createContract { startedOn } } `; const response = await ...
🌐
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 - In the previous example, why would we use a complete mock vs a spy? test('app() with mock counter .toHaveBeenCalledTimes(1)', () => { const mockCounter = { increment: jest.fn() }; app(mockCounter); expect(mockCounter.increment).toHaveBeenCalledTimes(1); }); test('app() with jest.spyOn(counter) .toHaveBeenCalledTimes(1)', () => { const incrementSpy = jest.spyOn(counter, 'increment'); app(counter); expect(incrementSpy).toHaveBeenCalledTimes(1); });
🌐
Jest
jestjs.io › mock functions
Mock Functions · Jest
May 7, 2026 - Constructs the type of a spied class or function (i.e. the return type of jest.spyOn()).
🌐
JavaScript in Plain English
javascript.plainenglish.io › jest-spies-and-mocks-in-explained-via-examples-71229077277f
Jest Spies and Mocks Explained via Examples | by John C. Lokman | JavaScript in Plain English
July 24, 2024 - Testing Jest Spies and Mocks Explained via Examples Make your JavaScript tests deeper, leaner, and faster with these two Jest methods 👁 Spying jest.spyOn( PROTOTYPE_OR_CLASS, METHOD_NAME ) Spying …
🌐
Sevic
sevic.dev › notes › spies-mocking-jest
Spies and mocking with Jest | Željko Šević | Node.js Developer
jest · .spyOn(calculationService, 'calculate') .mockImplementationOnce((a) => a + 3) .mockImplementationOnce((a) => a + 5); External modules can be mocked similarly to spies. For the following example, let's suppose axios package is already used in one function.
Find elsewhere
🌐
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 - const spy = jest.spyOn(analytics, ... events · Catches over-calling or under-calling. Production example: analytics tracking firing twice per action....
🌐
Medium
medium.com › @eklavya_ › jest-spy-vs-mock-when-to-use-what-60b8720f3ed0
Jest Spy vs Mock — when to use what! | by Gunjan Kalita | Medium
April 25, 2025 - describe('What time is it:', () => { it('Verify that 15 returns afternoon', () => { const mockDate = new Date(2021, 3, 24, 15, 0, 30, 0) // Create a system spy. const spy = jest.spyOn(global, 'Date'); spy.mockImplementationOnce(() => mockDate); ...
🌐
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 - 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.
🌐
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!
🌐
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 - Some key points are: It is a functional app with React that guesses the nationality of a given name by calling an API. Jest spyon has the ability to spy on the method calls and parameters like Jest Mock/fn.
🌐
Silvenon
silvenon.com › blog › mocking-with-jest › functions
Mocking with Jest: Spying on Functions and Changing Implementation
May 22, 2022 - We can’t just replace Math.random with a mock function because we want to preserve its functionality, instead we can spy on it using jest.spyOn, which wraps it in a mock function and returns it so we can track it: const MontyPython = require('./monty-python') describe('MontyPython', () => { describe('getTheMeaningOfLife', () => { it('reveals the cold hard truth about life', () => { const montyPython = new MontyPython() const mathRandomSpy = jest.spyOn(Math, 'random') montyPython.getTheMeaningOfLife() expect(mathRandomSpy).toHaveBeenCalled() mathRandomSpy.mockRestore() }) }) }) Here Math.random does its thing, we’re not changing its functionality, we’re only temporarily wrapping it in a mock function in order to make assertions on it.
🌐
Meticulous
meticulous.ai › blog › mocking-a-javascript-class-with-jest-two-ways-to-make-it-easier
Mocking a JavaScript Class with Jest, Two Ways to Make it Easier
Using jest spyOn to mock a specific method of an ES6 class is like performing a surgical operation. Using it you can target one or more specific methods called in the test’s context. Below is an example code of the ES6 class mock using Jest spyOn to mock the ExchangeRateClient class' ``getLatestExchangeRate` method only...
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › ise developer blog › jest mocking best practices
Jest Mocking Best Practices - ISE Developer Blog
November 4, 2024 - Then we can use the jest.spyon() function on the PersonFactory so that the class’s getInstance method will be overwritten to return our mocked person object. jest.spyOn(PersonFactory, 'getInstance').mockReturnValue(mockAnnika);
🌐
Medium
rrish7g.medium.com › jest-fn-vs-jest-spyon-understanding-the-differences-for-effective-testing-0f5928f7a411
Jest.fn() vs Jest.spyOn(): Understanding the Differences for Effective Testing | by Rrish | Medium
November 27, 2023 - Real Behaviour Testing: For integration tests where the actual implementation contributes to the behaviour under test, jest.spyOn() allows you to verify the integration without stubbing out the code.
🌐
Jest
jestjs.io › es6 class mocks
ES6 Class Mocks · Jest
May 7, 2026 - A simple example: // your jest test file below · import SoundPlayer from './sound-player'; import SoundPlayerConsumer from './sound-player-consumer'; const playSoundFileMock = jest · .spyOn(SoundPlayer.prototype, 'playSoundFile') .mockImplementation(() => { console.log('mocked function'); }); // comment this line if just want to "spy" it('player consumer plays music', () => { const player = new SoundPlayerConsumer(); player.playSomethingCool(); expect(playSoundFileMock).toHaveBeenCalled(); }); Lets imagine our class SoundPlayer has a getter method foo and a static method brand ·
🌐
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 - // 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…
🌐
Code with Hugo
codewithhugo.com › jest-mock-spy-module-import
Jest Full and Partial Mock/Spy of CommonJS and ES6 Module Imports · Code with Hugo
October 15, 2019 - Spying on the import/mocking part of the module becomes possible in the following fashion (full code at examples/spy-module-esm-default/lib.jest-test.js): import db from './db'; import lib from './lib'; const {addTodo, getTodo} = lib; beforeEach(() => jest.clearAllMocks()); test('ESM Default Export > addTodo > inserts with new id', async () => { const dbSetSpy = jest.spyOn(db, 'set').mockImplementationOnce(() => {}); await addTodo({name: 'new todo'}); expect(dbSetSpy).toHaveBeenCalledWith('todos:1', {name: 'new todo', id: 1}); }); test('ESM Default Export > getTodo > returns output of db.get', async () => { const dbGetSpy = jest.spyOn(db, 'get').mockResolvedValueOnce({ id: 1, name: 'todo-1' }); const expected = { id: 1, name: 'todo-1' }; const actual = await getTodo(1); expect(dbGetSpy).toHaveBeenCalledWith('todos:1'); expect(actual).toEqual(expected); });