reactjs - TypeError : wrapper. 存在不是函数 && TypeError : wrapper. find 不是函数

标签 reactjs testing

我正在为我的一个组件编写测试用例,该组件具有路由器(使用 withrouter)。我收到错误 wrapper.find is not a function。 基本要求是需要检查我的渲染中是否存在标签,还要检查标签的 aria-label 属性

常量组件 = () => const WrappedComponent = withRouter(组件)

  describe('Error Boundary...', () => {
    it('should render children when no error is encountered', () => {
      const wrapper = ReactTestRenderer.create(
        <Router><WrappedComponent /></Router>
      );
      expect(wrapper.toJSON().type).toEqual('div');
      expect(wrapper.toJSON()find('a').attr('aria-label')).toEqual('Hello World');
    });
  });

最佳答案

您不应该在期望行中调用 .toJSON()

 describe('Error Boundary...', () => {
    it('should render children when no error is encountered', () => {
      const wrapper = ReactTestRenderer.create(
        <Router><WrappedComponent /></Router>
      );
      expect(wrapper.root.type).toEqual('div');
      expect(wrapper.root.find('a').attr('aria-label')).toEqual('Hello World');
    });
  });

关于reactjs - TypeError : wrapper. 存在不是函数 && TypeError : wrapper. find 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57305080/

相关文章:

ruby-on-rails - Rails Cucumber 测试 - 将表单数据发送到方法

java - 在 Spring 应用程序中进行集成测试期间,您如何处理(或阻止)电子邮件的发送?

ruby-on-rails - 使用 capybara rspec 和工厂女孩测试设计

reactjs - 有没有办法在状态中存储组件引用?或者更多的 "react-y"替代方案?

reactjs - 如何在浏览器中获取 Cognito ID token ?

javascript - React-倒计时-现在不在渲染上更新

javascript - 故事书导航侧边栏卡住加载

java - JUnit 参数化测试 - IllegalArgumentException : wrong number of arguments

testing - 用于将 RTP 流式传输到特定端口的测试工具

reactjs - 渲染一个作为 Prop 传入的组件