您好,我是使用create-react-app方法创建的React应用,并且没有在外部安装Jest,因为create-react-app具有完整的Jest库。 我的问题是,当我尝试运行代码时,出现以下错误:
Cannot find module 'react' from 'react-router.js'
However, Jest was able to find:
'./Menu.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (../node_modules/react-router/cjs/react-router.js:5:29)
这是我的代码:
import React from 'react';
import { shallow } from 'enzyme';
import ApplyLoan from './Components/applyloan.js';
describe('Test Button component', () => {
it('Test click event', () => {
const mockCallBack = jest.fn();
const button = shallow((<ApplyLoan onClick={mockCallBack}>Ok!</ApplyLoan>));
button.find('applyloansubmit').simulate('click');
expect(mockCallBack.mock.calls.length).toEqual(1);
});
});