Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda