As a newcomer to Jest, I am navigating my way through using Dependency Injection with a UserService
.
public async getAll() {
const userRecords = await this.userModel.find().select('name').catch((e) => {
throw new HttpException(500, 'Error while fetching users.', e)
});
return <[IUser]>userRecords;
}
I am looking to test this feature and have identified three possible tests:
- Verify the JSON response after calling the route
- Check the accuracy of DB content retrieval
- Test the functionality of the
getAll
function independently
While tests 1 and 2 are straightforward in terms of covering request and DB aspects, how can I effectively isolate and test only the getAll
function?
In attempting to do so, I experimented with the following code snippet:
const userModel = {
find: (user) => {
return [
{ id: 'user1' },
{ id: 'user2' }
]
},
};
const userService = new UserService(userModel);
const userRecords = await userService.getAll();
expect(argumentRecord).toBeDefined();
However, the test failed due to the error stating that select is undefined
.
Should I consider mocking select()
as well? Would restructuring my code help resolve this issue?