I am looking to use Jest to test the responses from my API. This method is from my controller.
@Post('send-banana')
async sendBanana(
@Body() request: BananaRequest,
@Res() res: Response,
) {
const responseCodeService = await this.bananaService.sendBanana(
request,
)
res.status(responseCodeService).send({
code: responseCodeService,
message: HttpStatus[responseCodeService],
})
}
Here is the test I have written:
describe('Banana Controller', () => {
fit('should return httpcode(201)', async () => {
const result = await Promise['']
const request = {
origin: 'dummy origin',
receiver: 'dummy@dummy',
data: {
name: 'Elsa Pallo',
},
} as BananaRequest
const response = jest.fn((resObj) => ({
status: jest.fn((status) => ({
res: { ...resObj, statusCode: status },
})),
}))
jest
.spyOn(bananaService, 'sendBanana')
.mockImplementation(() => result)
expect(
await bananaController.sendBanana(request, response),
).toBe(result)
})
})
I encountered an error:
https://i.sstatic.net/Q64MB.png
Could anyone provide guidance on how I can mock the response?