小编qwe*_*iuy的帖子

类型错误:无法读取未定义的属性“位置”(开玩笑)

我有一个简单的组件,它使用 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', () => …
Run Code Online (Sandbox Code Playgroud)

unit-testing reactjs jestjs react-router enzyme

11
推荐指数
3
解决办法
1万
查看次数

标签 统计

enzyme ×1

jestjs ×1

react-router ×1

reactjs ×1

unit-testing ×1