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

189
Vistas
Jest - Making imported functions from source file use mocked instances of function in same file

I'm writing some unit tests for my Node backend using jest, and keep running into the same problem. I have a test file, let's say logic.test.js, that tests functionality from logic.js. logic.js consists of two functions - dataRequestFunction and logicFunction, where logicFunction makes use of dataRequestFunction to retrieve some data with an HTTP request and then performs some sort of data handling afterwards.

I want to mock the functionality of dataRequestFunction so that I can control what is returned, so that I know logicFunction operates on the correct data. What I've done so far is using jest.requireActual, like the following

jest.mock('./logic', () => ({
  ...jest.requireActual('./logic'),
  dataRequestFunction: jest
    .fn()
    .mockImplementation(() => ({ data: 'someData' }))
}))

I then import logicFunction as usual. I want to use logicFunction in the test file, for example expect(logicFunction()).toEqual('somedata') (let's say the logic function's job is capitalizing the result from (await dataRequestFunction()).data). How do I accomplish this? With this implementation, logicFunction calls the non-mocked version of the function - however, if I call dataRequestFunction explicitly in the test file, ({ data: 'someData' }) is returned.

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