+ 2

I have to write my unit test case in jest, can someone help me with error I am getting.

this is my service method to get list of users: async getAllUsers(): Promise<UserDto[]>{ return this.queryBus.execute<UsersQuery, UserDto[]>(new UsersQuery()); } this is my test case: describe('getUsers', () => { describe('get users is called', () => { let users: User[]; //axios.get.mockResolvedValue(resp); beforeEach(async () => { await userController.getAllUsers(); }) test('it will call the user service', async() => { expect(await userService.getAllUsers()).toHaveBeenCalled(); }) test('return all the users', () => { expect(users).toEqual([userDTOStub()]) }) }) }); when i run this method it is telling type error: TypeError: metatype is not a constructor. how to compare the promise with mock sample data.

8th Jan 2023, 9:20 AM
Bhavani Sankar
Bhavani Sankar - avatar
1 Respuesta
+ 1
The issue with your test is that you are not mocking the getAllUsers() method of the userService. You need to mock this method so that you can control what it returns in your test. You can do this using jest.mock() or jest.spyOn().
11th Jan 2023, 9:27 PM
Aditya Dixit