Struggling to Mock a Static Method in a TypeScript Class with Jest
I'm wondering if anyone has experience with I've looked through the documentation and I'm still confused about I've searched everywhere and can't find a clear answer. I'm trying to write unit tests for a TypeScript class that has a static method, but I'm running into issues mocking that static method using Jest. Here's a simplified version of my class: ```typescript class UserService { static getUser(userId: string) { // Simulating an API call return fetch(`https://api.example.com/users/${userId}`) .then(response => response.json()); } } class UserController { getUserDetails(userId: string) { return UserService.getUser(userId); } } ``` In my test file, I am trying to mock `getUser` like this: ```typescript import UserService from './UserService'; import UserController from './UserController'; describe('UserController', () => { let userController: UserController; beforeEach(() => { userController = new UserController(); jest.spyOn(UserService, 'getUser'); }); it('should return user details', async () => { const mockResponse = { id: '1', name: 'John Doe' }; (UserService.getUser as jest.Mock).mockResolvedValue(mockResponse); const userDetails = await userController.getUserDetails('1'); expect(userDetails).toEqual(mockResponse); expect(UserService.getUser).toHaveBeenCalledWith('1'); }); }); ``` However, I'm working with the following behavior: `TypeError: want to read properties of undefined (reading 'getUser')`. I've confirmed that `getUser` is defined and should be accessible. I've also tried using `jest.mock('./UserService')` to mock the entire module, but that led to issues with the function not being recognized as a static method. I'm using Jest 27.4.5 and TypeScript 4.5.4, and I suspect there might be some nuances with how Jest handles static methods that I'm missing. Any guidance on how to properly mock a static method in this context would be greatly appreciated! I'm working on a web app that needs to handle this. Could someone point me to the right documentation? This is part of a larger REST API I'm building. Any feedback is welcome!