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

156
Vistas
jest fn mock isnt working if not assigned in it and assigned in for each

i am trying to write tests using jest the integration is a call that have a private function called test- gets called by funcTest trying to mock implementation in before each how ever the mock does not work and it goes to the real function "test". when i put the assignment (integration['test']=mockFn;) inside the "it" it works perfectly . example for working code:

describe('test', () => {
        const mockFn: jest.Mock = jest.fn();
    beforeEach(async () => {
    const integration = new integrationTest();
    })
        afterEach(() => {
          jest.restoreAllMocks();
        }); 
       it('call mock implemntaion', async () => {
          integration['test']=mockFn;
          mockFn.mockImplementation(() => {
            throw new Error('error');
          });
    
          try {
            await integration.funcTest();
          } catch (e) {
            expect(e).toBeInstanceOf(Error);
          }
        });
})

example of not working code:

    describe('test', () => {
        const mockFn: jest.Mock = jest.fn();
    beforeEach(async () => {
    const integration = new integrationTest();
    integration['test']=mockFn;
    })
        afterEach(() => {
          jest.restoreAllMocks();
        }); 
       it('call mock implemntaion', async () => {
         
          mockFn.mockImplementation(() => {
            throw new Error('error');
          });
    
          try {
            await integration.funcTest();
          } catch (e) {
            expect(e).toBeInstanceOf(Error);
          }
        });
})

why does this happen and how to fix thanks

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