【发布时间】:2021-10-05 23:47:31
【问题描述】:
我正在尝试为我的组件编写快照测试以响应
在我的组件中,我有useParams()
在运行测试时,它返回一个未定义的错误:
TypeError: Cannot read property 'match' of undefined
20 | const [value, setValue] = useState();
21 | const { show, changeDetails } = props;
> 22 | const { id } = useParams();
| ^
组件位于
<Switch>
<Route
exact
path="/myroute/:id"
component={() => (
<>
<ModuleDetails />
</>
)}
/>
</Switch>
而 test.js 就像
it('renders correctly the assessment show page', () => {
const history = createMemoryHistory();
history.push('/myroute/1');
let tree = renderer
.create(
<Router history={history}>
<ModuleDetails />
</Router>
)
.toJSON();
expect(tree).toMatchInlineSnapsot(``);
});
【问题讨论】:
-
我检查并尝试过,但测试没有正确呈现
-
it('正确渲染', () => { let tree = renderer .create(
) .toJSON(); expect(tree).toMatchInlineSnapshot(``); });