Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
how to mock a nested response object parameter in jest?

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!