简体   繁体   English

反应酶反应路由器测试用例警告:React.createElement:类型无效

[英]React enzyme react router test case warning: React.createElement: type is invalid

I am using react-router-dom in my react app.我在我的 React 应用程序中使用了 react-router-dom。 App works fine in browser without any error or warning, but when I am writing tests, I am getting a warning 'React.createElement: type is ....'应用程序在浏览器中运行良好,没有任何错误或警告,但是当我编写测试时,我收到警告“React.createElement: type is ....”

Here is my test code:这是我的测试代码:

import React from 'react';
import NavBar from './components/nav-bar';
import {configure, mount, shallow} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16'
import {expect} from 'chai';
import App from "./App";
import {MemoryRouter} from 'react-router';

configure({adapter: new Adapter()});

jest.mock('react-router-dom', () => ({
    useLocation: jest.fn().mockReturnValue({
        pathname: '/another-route',
        search: '',
        hash: '',
        state: null,
        key: '5nvxpbdafa',
    }),
}));

describe("Movie Library Testing", () => {
    it('renders react app', () => {
        const wrapper = shallow(
            <App/>
        );
        expect(wrapper.find(NavBar)).to.have.lengthOf(1);
    });

    it('renders an `.navbar-brand', () => {
        const wrapper = mount(
            <MemoryRouter>
                <NavBar user={null}/>
            </MemoryRouter>
        );
        expect(wrapper.find('.navbar-brand')).to.have.lengthOf(1);
    });
});

I am getting the following error:我收到以下错误:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your componen
t from the file it's defined in, or you might have mixed up default and named imports.
    
    Check your code at nav-bar.tsx:41.
        in NavBar (at App.test.tsx:31)
        in Router (created by MemoryRouter)
        in MemoryRouter (created by WrapperComponent)
        in WrapperComponent

The NavBar.tsx has following code at given Line: NavBar.tsx 在给定的行有以下代码:

import { NavLink } from 'react-router-dom'
<NavLink className="nav-link" to="/login">Login</NavLink>

export default NavBar;

Change改变

import {NavBar} from './components';

to

import NavBar from './components';

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM