简体   繁体   English

未捕获的错误:useNavigate() 只能用于<Router> cypress 单元测试用例中的组件

[英]Uncaught Error: useNavigate() may be used only in the context of a <Router> component in cypress unit testcases

I am trying to write "unit-test" for components in react with cypress.我正在尝试为与柏树反应的组件编写“单元测试”。
Followed the link cypress docs on component testing按照有关组件测试的链接cypress docs

when I wrote testcase for a component and tried to run with "npx cypress open-ct"当我为一个组件编写测试用例并尝试使用“npx cypress open-ct”运行时
then getting the above error (question title)...然后得到上述错误(问题标题)...
Note: for login oAuth2 is implemented !!!注意:对于登录 oAuth2 已实施!!!
My index.spec.js file is我的 index.spec.js 文件是

import * as React from 'react';
import { mount } from '@cypress/react';
import User from './index';

describe('User component', ()=>{
    before('loginApi', ()=>{
        cy.login();  
        //login() has login-logic and setting localStorage (placed in "commands.js" file)  
        //even if cy.login() is commented, error is same (so guessing this not reason of error)
    })
    it('Mount user', () => {            
        mount(<User />); 
    });
})

Observation1: on cypress browser under TEST BODY "mount" value is <Unknown.../>观察1:在柏树浏览器上TEST BODY“mount”值为<Unknown.../>
Observation2: [Network tab] User component makes api-call and failing with 401观察 2:[网络选项卡] 用户组件进行 api 调用并以 401 失败
(#known token issue, even fails with right token) (#known 令牌问题,即使使用正确的令牌也会失败)

Find attached error screenshot.查找附加的错误屏幕截图。 错误截图

After some beating around I found out...经过一番折腾,我发现...

  1. mount <Unknown... /> basically results in the case, if you have not added all the dependency which are used in respective index.jsx file. mount <Unknown... />基本上会导致这种情况,如果您尚未添加在相应 index.jsx 文件中使用的所有依赖项。

  2. useNavigation() will be used only in context results in the case, if you are missing BrowserRouter dependency the way you have used in your routing page (either one) useNavigation() 将仅在上下文结果中使用,如果您在路由页面中使用的方式缺少 BrowserRouter 依赖项(任一)
    import {BrowserRouter as Router} from 'react-router-dom';
    import {BrowserRouter} from 'react-router-dom';

     import * as React from 'react'; import { mount } from '@cypress/react'; import User from './index'; import {BrowserRouter as Router} from 'react-router-dom'; describe('User component', ()=>{ it('Mount user', () => { const onSubmit= cy.stub(); //or any other function which is probably used by ur component mount( <Router> <User onSumbit={onSubmit}/> </Router> ); }); });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的错误:useNavigate() 只能在 a 的上下文中使用<router>零件</router> - Uncaught Error: useNavigate() may be used only in the context of a <Router> component 未捕获的错误:useNavigate() 只能在<router>组件 [react-router-dom 6.4]</router> - Uncaught Error: useNavigate() may be used only in the context of a <Router> component [react-router-dom 6.4] 未捕获的错误:useNavigate() 只能在<router>成分。 - 我做错了什么?</router> - Uncaught Error: useNavigate() may be used only in the context of a <Router> component. - What is it that I am doing wrong? 错误:useNavigate() 只能在 a 的上下文中使用<router>成分</router> - Error: useNavigate() may be used only in the context of a <Router> component useNavigate() 只能在<router>零件</router> - useNavigate() may be used only in the context of a <Router> component (反应路由器 v6)“错误:useNavigate() 只能在<router>特定组件中的组件”错误</router> - (react router v6) "Error: useNavigate() may be used only in the context of a <Router> component" error in hoc component npm 测试:useNavigate() 只能在<router>成分</router> - npm test: useNavigate() may be used only in the context of a <Router> component 未捕获的错误:useLocation() 只能在 a 的上下文中使用<router>零件</router> - Uncaught Error: useLocation() may be used only in the context of a <Router> component React Router V6 - useNavigate() 只能在一个上下文中使用<router>零件</router> - React Router V6 - useNavigate() may be used only in the context of a <Router> component 错误:useNavigate() 只能在 a 的上下文中使用<router>组件虽然我的登录页面的路由位于里面<browserrouter></browserrouter></router> - error: useNavigate() may be used only in the context of a <Router> component Although my login page's route is located inside <BrowserRouter>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM