Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda