'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();
  });
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source