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

79
Vistas
Overwriting a module import for mocking during tests?

I am writing unit tests and would like to mock a function in a module but avoid using jest.mock. Is such a thing possible?

If not, how does jest.mock work and will it survive the transition from commonjs to esmodules in Node?

import * as foo from './foo'

describe('foo', () => {
  let mockFn: jest.Mock

  beforeEach(() => {
    mockFn = jest.fn()
    
    Object.defineProperty(foo, 'bar', {
      value: mockFn,
      writable: true,
      configurable: true,
    })
  })

  it('Should not throw', () => {
    const testFunc = () => foo()
    expect(testFunc).not.toThrow()
  })
})
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