qwe*_*iuy 11 unit-testing reactjs jestjs react-router enzyme
我有一个简单的组件,它使用 React Router 的 useLocation 钩子。
// App.jsx
import React from 'react';
import { useLocation } from 'react-router-dom';
function App() {
const location = useLocation();
const pathName = location.pathname;
useEffect(() => {
// Use pathName here
}, [pathName]);
}
Run Code Online (Sandbox Code Playgroud)
// App.test.js
import App from './App';
describe('App component', () => {
it('Renders correctly', () => {
const wrapper = shallow(<App />);
expect(wrapper).toMatchSnapshot();
});
});
Run Code Online (Sandbox Code Playgroud)
// 更新 App.test.js(使用 jest mock)
import App from './App';
describe('App component', () => {
jest.mock('react-router-dom', () => ({
useLocation: jest.fn().mockReturnValue({
pathname: '/another-route',
search: '',
hash: '',
state: null,
key: '5nvxpbdafa',
}),
}));
it('Renders correctly', () => {
const wrapper = shallow(<App />);
expect(wrapper).toMatchSnapshot();
});
});
Run Code Online (Sandbox Code Playgroud)
你能告诉我如何解决这个问题吗?谢谢。
mga*_*cia 11
只需将您的jest.mock声明移动到文件的顶部,它应该可以工作:
import App from './App';
jest.mock('react-router-dom', () => ({
useLocation: jest.fn().mockReturnValue({
pathname: '/another-route',
search: '',
hash: '',
state: null,
key: '5nvxpbdafa',
}),
}));
describe('App component', () => {
it('Renders correctly', () => {
const wrapper = shallow(<App />);
expect(wrapper).toMatchSnapshot();
});
});
Run Code Online (Sandbox Code Playgroud)
万一其他人也面临类似的问题。
\n\n我的方法有两个问题。首先,我在描述中使用了模拟(感谢约翰尼和姆加西亚帮助我解决这个问题)。
\n\n但是,在移动模拟代码后,我遇到了另一个错误。错误:React.createElement: type is invalid \xe2\x80\x94 expected a string
这是我的另一个 Jest 新手错误,忘记了它的底层都是 JavaScript。使用这种方法,模拟 useLocation 的副作用是其余方法react-router-dom不可用。
这是对我有用的方法:
\n\nhttps://github.com/facebook/jest/issues/936#issuecomment-265074320
\n\n最终解决方案:
\n\nfunction mockFunction() {\n const original = require.requireActual(\'react-router-dom\');\n return {\n ...original,\n useLocation: jest.fn().mockReturnValue({\n pathname: \'/another-route\',\n search: \'\',\n hash: \'\',\n state: null,\n key: \'5nvxpbdafa\',\n }),\n };\n}\n\njest.mock(\'react-router-dom\', () => mockFunction());\nRun Code Online (Sandbox Code Playgroud)\n
问题是您需要指定您正在使用 es6 模块语法。尝试这个:
import App from './App';
jest.mock('react-router-dom', () => ({
__esModule: true,
useLocation: jest.fn().mockReturnValue({
pathname: '/another-route',
search: '',
hash: '',
state: null,
key: '5nvxpbdafa',
}),
}));
describe('App component', () => {
it('Renders correctly', () => {
const wrapper = shallow(<App />);
expect(wrapper).toMatchSnapshot();
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11501 次 |
| 最近记录: |