Home > Net >  React JS Jest mock implementation of Class with static members
React JS Jest mock implementation of Class with static members

Time:04-28

I am working on a project in which I am writing jest unit testing but I am stuck with below problem. I also search on google but not get any proper solution.

ActivityDetailsModal.js

class ActivityDetailsModal {
    static Activity = {
        activityType : ‘PLOT’
    }
}

Xyz.test.js

Import { ActivityDetailsModal, useCustomHook } from ‘@cc-module’;

// This mock being used in other test case
jest.mock(‘@cc-module’,() => ({
    useCustomHook : jest.fn();
}));

it('should fail test’, async () => {
    …..
    // Here I get the error : ActivityDetailsModal is undefined
    const activityType = ActivityDetailsModal.Activity.activityType;
    …..
});

So I think because of mock implementation of @cc-module I am unable to access ActivityDetailsModal here So how can I have mock implementation of this class or some how I can ignore mocking for one particular test. I don’t know how is this possible.

CodePudding user response:

You can also create mock implementation of ActivityDetailsModal as following

// This mock being used in other test case
jest.mock(‘@cc-module’,() => ({
    useCustomHook : jest.fn();
    ActivityDetailsModal : {
       Activity : {
           activityType : 'PLOT'
       }
    };
}));

CodePudding user response:

You could do

jest.mock('@cc-module', () => ({
  ...jest.requireActual('@cc-module'),
   useCustomHook : jest.fn();
}));

if you only want to mock the useCustomHook

  • Related