Skip to content Skip to sidebar Skip to footer

How Do I Get A Jest/enzyme Test To Pass This Require Statement Containing A Path And A Prop

Hi I'm trying to test a react module that contains the code avatar which concats a path and a prop containing a file nam

Solution 1:

First of all you need to pass some props when shallow rendering your component:

describe('<UserProfile /> component', () => {
    it('renders enclosing html tag', () =>expect(
            shallow(<UserProfilename="fakeName"avatar="fakeAvatar" />)
                .find('section')
                .length
        ).toBe(1);
    );
});

Then after that enzyme will look for a module at the path ../../assets/${avatar} so in the case of the test above: ../../assets/fakeAvatar

There is obviously nothing at that path so you need to mock it:

jest.mock('../../assets/fakeAvatar', () =>'fake/image/url.png');

So your full test would be

describe('<UserProfile /> component', () => {
    it('renders enclosing html tag', () => 
        jest.mock('../../assets/fakeAvatar', () =>'fake/image/url.png');
        expect(
            shallow(<UserProfilename="fakeName"avatar="fakeAvatar" />)
                .find('section')
                .length
        ).toBe(1);
    );
});

Post a Comment for "How Do I Get A Jest/enzyme Test To Pass This Require Statement Containing A Path And A Prop"