我有一个使用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>
);
};
Button
在core-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应用程序时,启动该组件没有任何问题。因此,只有Jest无法解析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);
});
});