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

153
Visualizações
Override a mocked consumer body for certain tests

I have the following test which works.

import React from 'react';
import { mount } from 'enzyme';
import MyLogin from '../../src/components/MyLogin';

const mockContent = {
  data: {
    config: {
      myImage: 'mock_image',
    },
  },
};

jest.mock('../../src/utils/config', () => ({
  Consumer: ({ children }) => children(mockContent),
}));

describe('Component Test', () => {
  beforeEach(() => {
    jest.resetModules();
  });

  const render = () => mount(
    <MyLogin />
  );

  it('should render the component', () => {
    const renderedModule = render();
    expect(renderedModule).toMatchSnapshot();
  });
});

But I wish to add another test where the values inside the mockContent changes.
I have added a isValid key.

Thus tried the following where I am trying to over ride mock only for this test.
But this doesn't work. No errors. No effect from this mock.
If I had added this new key isValid at the above mock outside the test, it does have the desired effect. (Can't do that of course cos that would affect my other tests)
Could I get some help with this pls.

describe('Component Test', () => {
  // other tests

  it('my new test', () => {

    // attempting to override the above mock. 
    const myNewMockContent = {
        data: {
            config: {
                myImage: 'mock_image_2',
                isValid: true,
            },
        },
    };

    jest.mock('../../src/utils/config', () => ({
        Consumer: ({ children }) => children(myNewMockContent),
    }));

    const renderedModule = render();
    expect(renderedModule).toMatchSnapshot();
  });
});
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