1
votes

I'm trying to mock a destructed import with Jest.

In the component is the destructed import:

import { getSomething } from 'utils/paymentUtils';

In the test:

import React from 'react';
import { shallow } from 'enzyme';
import Component from 'components/Component';
import * as utils from 'utils/paymentUtils';

jest.mock('utils');

describe('Something', () => {
  let wrapper;    
  utils.getSomething.mockImplementation(() => 'Blah');

The error I'm getting is:

Cannot find module 'utils' from 'Component.spec.js'

1
Is your component in the same directory of your test ? - Thomas Betous
try mocking the same path: jest.mock('utils/paymentUtils') - skyboyer

1 Answers

0
votes

You have to mock the module itself, in this case utils/paymentUtils