为什么Jest无法解析node_modules下存在的模块?

时间:2019-03-09 01:06:58

标签: reactjs jestjs enzyme create-react-app

我有一个使用Create React App创建的React应用程序。我尝试使用Jest测试以下组件:

import React from 'react';
import { Button } from 'core-ui';

export const Example = () => {
  return (
    <div className="Example">
      <Button>Click me!</Button>
    </div>
  );
};

Buttoncore-ui包中,对core-utils包具有依赖性:

import * as React from 'react';
import { Logging } from 'core-utils';

export const Button = (props) => {
  logger.log('Rendering button');
  return (
    <button>
      {props.children}
    </button>
  );
}

当我尝试使用Jest测试此按钮时,出现以下错误:

Test suite failed to run

Cannot find module 'core-utils' from 'Button.js'

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
  at Object.<anonymous> (node_modules/core-ui/src/components/Button/Button.tsx:2:1)

Jest无法解析core-utils模块,即使它位于node_modules下。请注意,当我运行React应用程序时,启动该组件没有任何问题。因此,只有Jes​​t无法解析core-utils模块。有什么想法可以解决问题吗?

这是开玩笑的测试供参考:

import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';

import { Example } from './Example';

describe('<Example />', () => {
  let wrapper: ShallowWrapper;
  beforeEach(() => {
    wrapper = shallow(<Example />);
  });

  it('renders', () => {
    expect(wrapper.find('div.Example')).toHaveLength(1);
  });
});

0 个答案:

没有答案