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

150
Vistas
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 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