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

160
Visualizações
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 à 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