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

147
Visualizações
How to mock a const with jest?

I've been getting an error while trying to unit test a new method on my service that returns an object that looks like this:

    { 
     requestHeaders: [
      {
        'header1': '...',
        'header2': '...',
        'header3': '...'
      },
      {
        'header1': '...',
        'header2': '...',
        'header3': '...'
    },
    {
        'header1': '...',
        'header2': '...',
        'header3': '...'
    },
   ]
  }

My service method:

    const requestObject = this.otherService.getRequestObject(payload);
    const [request] = requestObject.headers.attribute; // This doesn't look good

    if(request){
    // Do stuff
    }

When I run npm run test


    TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))

      32 |     );
    > 33 |     const [request] = requestObject.headers.attribute;
         |                           ^

My breaking test:


  it('should call sendNewRequest() ', async () => {

    jest
      .spyOn(myService, 'isExistingRequest')
      .mockImplementation(() => Promise.resolve(false));

    jest
      .spyOn(myService, 'createNewRequest')
      .mockImplementation();

    await myService.handleRequest({});
    expect(myService.createNewProductionOrder).toHaveBeenCalled();
  });
});

I think that my getRequestObject is returning undefined because I'm sending an empty object to my handleRequest method, and getRequestObject is expecting a different thing. Now, have in mind that the object I exemplified here is not exactly as the original object is (the original one is much larger and have hundreds of fields), but it is an object with an array of objects.

How should I approach this since I want to avoid creating manually a requestObject inside my testing file?

It is worth mentioning that my getRequestObject method is declared like this in an utils file:

export const objectDtoTransformer = (params) => {doStuff}
about 4 years ago · Juan Pablo Isaza
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