I am writing a unit tests for this specific endpoint but I can't mock the Response Parameter, it is throwing an error in the .json part whenever it tries to access it.
async createTenant(@Body() clientDto: ClientDto, @Res() res: any)
This is where I access it inside the controller. It is throwing an error inside the .json function, how do I mock this property?
return res.status(HttpStatus.BAD_REQUEST).json({
message: 'Client already exists.',
});
I already tried something like this:
const jsonFn = jest.fn().mockImplementation().mockResolvedValueOnce({ message: 'something' });
const res = { status: jest.fn().mockResolvedValueOnce({ json: jsonFn })};
It is now working for the .status part but for the .json it is still throwing an error. Any ideas that might help?
You should try using a library to mock the Request and Response objects, the best one I have found so far is @jest-mock/express.
You can use it like this:
import { getMockRes } from '@jest-mock/express'
const { res } = getMockRes();
You can similarly mock Request as well, the docs are very clear on the usage.