Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!