[英]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 令牌问题,即使使用正确的令牌也会失败)
After some beating around I found out...经过一番折腾,我发现...
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 文件中使用的所有依赖项。
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.