Veg*_*eta 5 javascript reactjs jestjs enzyme
我有一个使用 useLocation 挂钩从 URL 获取路径的组件。
const { pathname } = useLocation();
useEffect(() => { }, [pathname]);
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 来模拟该位置时,
import React from 'react';
import ExampleComponent from './ExampleComponent';
import { fireEvent, render } from '@testing-library/react';
import { shallow } from 'enzyme';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: () => ({
pathname: 'https://URL/'
})
}));
describe('<ExampleComponent />', () => {
it('should render correctly', () => {
shallow(<ExampleComponent />);
});
});
Run Code Online (Sandbox Code Playgroud)
我在运行测试时收到此错误, TypeError:无法读取未定义的属性“位置”
尝试将 useLocation 模拟为 jest.fn().mockImplementation
jest.mock('react-router', () => ({
...jest.requireActual("react-router") as {},
useLocation: jest.fn().mockImplementation(() => {
return { pathname: "/testroute" };
})
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10425 次 |
| 最近记录: |