【问题标题】:How can I write a unit test using jest, react, testing-library for this component?如何使用 jest、react、testing-library 为这个组件编写单元测试?
【发布时间】:2021-07-02 12:46:58
【问题描述】:

我需要在 React 打字稿项目中使用 Jest 和 testing-library 编写单元测试。以及如何为我的组件 ErrorModal 创建一个 ID 以在我的测试中使用?以前没写过测试!!谢谢。

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

  const { message } = props;

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

export default ErrorModal;

【问题讨论】:

    标签: reactjs typescript unit-testing jestjs react-testing-library


    【解决方案1】:

    阅读一点关于笑话的内容。

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

    基本上,您将通过查询来测试“消息”是否真的在您的组件中呈现。

    【讨论】:

    • 我一个人做不到,只是不知道怎么做 DD:我需要一个例子。我还有其他组件要测试。只是一个例子,它会对我有很大帮助
    • 你能帮帮我吗?
    猜你喜欢
    • 2022-01-20
    • 2021-02-11
    • 2020-03-22
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 2022-06-22
    相关资源
    最近更新 更多