简体   繁体   English

如何使用 jest、react、testing-library 为这个组件编写单元测试?

[英]How can I write a unit test using jest, react, testing-library for this component?

I need to write unit tests using Jest and testing-library in a React typescript project.我需要在 React typescript 项目中使用 Jest 和 testing-library 编写单元测试。 And how can I create an ID for my component ErrorModal to use in my test?以及如何为我的组件 ErrorModal 创建一个 ID 以在我的测试中使用? I never wrote a test before.!我以前从来没有写过测试。! Thank you.谢谢你。

const ErrorModal = (props: {message: string}) => {

  const { message } = props;

  return (
    <ErrorModalStyled>
      <ErrorMessageStyled>{ message }</ErrorMessageStyled>
    </ErrorModalStyled>
  );
}

export default ErrorModal;

Read a little bit about jest.阅读一点关于笑话的内容。

In your case, try this.在你的情况下,试试这个。 https://jestjs.io/docs/tutorial-react#dom-testing https://jestjs.io/docs/tutorial-react#dom-testing

Basically, you will test if "message" is actually being rendered in your component by querying it.基本上,您将通过查询来测试“消息”是否真的在您的组件中呈现。

暂无
暂无

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

相关问题 如何使用 jest 和(react-)testing-library 测试浏览器后退按钮的行为? - how can i test browser back-button behaviour using jest and (react-) testing-library? 如何使用 React、Jest 和 React-testing-library 为带有令牌的 api 调用编写单元测试? - How can I write unit test for api call with token using React, Jest and React-testing-library? 使用 testing-library 和 jest 的 React 组件抛出的测试错误 - Testing error thrown by a React component using testing-library and jest 如何在 jest 和 @testing-library/react 中测试 catch 部分 - How to test catch part in jest and @testing-library/react 如何使用 React 测试库为返回 Jest 中的组件的 Switch 语句编写单元测试用例 - How to write Unit Test case for Switch Statement that returns Component in Jest with React Testing Library 使用 Jest 和 @testing-library/react-hooks 在 TypeScript 中单元测试自定义 React Hook - Unit Testing Custom React Hooks in TypeScript using Jest and @testing-library/react-hooks 如何使用 react 测试库 jest 测试下拉组件? - How to test a dropdown component in react using react testing library jest? 如何使用带有Jest的酶在React Component中编写DataTable的单元测试? - How to write unit test for DataTable in React Component using Enzyme with Jest? 如何使用@testing-library/user-event 版本 14 在 React 中测试 `onKeyDown` 道具? - How can I test `onKeyDown` prop in React with @testing-library/user-event version 14? 我如何断言哪个值已传递给<li>与 jest 和 testing-library/react 反应的关键属性 - How can I assert which value has been passed to a <li> key attribute in react with jest and testing-library/react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM