简体   繁体   English

笑话:- TypeError:无法读取未定义的属性(读取“params”)。 开玩笑时出错

[英]Jest:- TypeError: Cannot read properties of undefined (reading 'params'). Error coming in jest

I have below component in react.我在反应中有以下组件。 I put in short only我只是简而言之

export interface EditCertificateProps {
    id:string;
}

export function EditCertificate(props: any) {
 injectStyle();

 const {id} = props.match.params;
 const history = useHistory();
}

When I am doing jest testing it is throwing error.当我进行开玩笑测试时,它会抛出错误。

const id = '123';
describe('EditCertificate', () => {
    const params = {
        id: '123',
    };
    it('should render successfully', () => {
        const { baseElement } = render(<EditCertificate id={id} />);
        expect(baseElement).toBeTruthy();
    });
});

it is throwing error它正在抛出错误在此处输入图像描述

from another component this page gets called like below.从另一个组件调用此页面,如下所示。

  <SecureRoute path="/:id/edit" component={EditCertificate} />

I changed my testcase like below still error.我像下面这样更改了我的测试用例仍然错误。

describe('EditCertificate', () => {
    const props = {
        match: {
            params: 123,
        },
    };
    it('should render successfully', () => {
        const { baseElement } = render(<EditCertificate {...props.match.params} />);
        expect(baseElement).toBeTruthy();
    });
});

what wrong I am doing?我做错了什么?

The EditCertificate component is expecting a match prop with a params property. EditCertificate组件需要一个带有params属性的match道具。

export function EditCertificate(props: any) {
  injectStyle();

  const {id} = props.match.params;
  const history = useHistory();

  ...
}

This match prop needs to be provided in the unit test.这个match道具需要在单元测试中提供。 You are creating a props object so you can just spread that into EditCertificate .您正在创建道具 object,因此您可以将其传播到EditCertificate中。 Spread the entire props object, not props.match.params , the latter only spreads the individual params.传播整个props object,而不是props.match.params ,后者只传播个别参数。

describe('EditCertificate', () => {
  const props = {
    match: {
      params: {
        id: 123, // <-- props.match.params.id
      },
    },
  };

  it('should render successfully', () => {
    const { baseElement } = render(<EditCertificate {...props} />);
    expect(baseElement).toBeTruthy();
  });
});

The next issue will be a missing routing context for the useHistory hook.下一个问题是useHistory挂钩缺少路由上下文。 You can provide a wrapper for the render util, or simply wrap EditCertificate directly.您可以为render实用程序提供wrapper ,或者直接包装EditCertificate

const RouterWrapper = ({ children }) => (
  <MemoryRouter>{children}</MemoryRouter> // *
);

...

const { baseElement } = render(
  <EditCertificate {...props} />,
  {
    wrapper: RouterWrapper
  },
);

or或者

const { baseElement } = render(
  <MemoryRouter>
    <EditCertificate {...props} />
  </MemoryRouter>
);

* MemoryRouter used for unit testing since there is no DOM * MemoryRouter用于单元测试,因为没有 DOM

暂无
暂无

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

相关问题 Jest Vuejs 测试 - 类型错误:无法读取未定义的属性(读取“参数”) - Jest Vuejs Test - TypeError: Cannot read properties of undefined (reading 'params') TypeError:无法读取未定义(读取“长度”)JEST 测试的属性 - TypeError: Cannot read properties of undefined (reading 'length') JEST test 开玩笑:无法读取未定义的属性(读取“0”) - Jest: Cannot read properties of undefined (reading '0') TypeError: Cannot read properties of undefined (reading &#39;then&#39;) - while Mocking Fetch call in Jest - TypeError: Cannot read properties of undefined (reading 'then') - while Mocking Fetch call in Jest Jest 导入模块,TypeError:无法读取未定义的属性(读取“utf16le”) - Jest import module, TypeError: Cannot read properties of undefined (reading 'utf16le') TypeError:无法在 React js 中读取未定义的属性(读取“参数”) - TypeError: Cannot read properties of undefined (reading 'params') in React js 未捕获的 TypeError:无法读取未定义的属性(读取“params”)-React - Uncaught TypeError: Cannot read properties of undefined (reading 'params') - React 未处理的拒绝(TypeError):无法读取未定义的属性(读取“参数”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'params') React - TypeError:无法读取未定义的属性(读取“params”)? - React - TypeError: Cannot read properties of undefined (reading 'params')? React TypeError:无法读取未定义的属性(读取“params”) - React TypeError: Cannot read properties of undefined (reading 'params')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM