简体   繁体   English

使用Jasmine测试React组件时出错

[英]Error while testing React component using Jasmine

I kept getting this error while testing a react component: 我在测试React组件时一直出现此错误:

Error: Invariant Violation: findComponentRoot(..., .0): Unable to find element. 错误:始终违反:findComponentRoot(...,.0):无法找到元素。 This probably means the DOM was unexpectedly mutated (eg, by the browser), usually due to forgetting a <tbody> when using tables, nesting tags like <form>, <p>, or <a>, or using non-SVG elements in an <svg> parent. 这可能意味着DOM被意外地突变(例如,通过浏览器),通常是由于在使用表,嵌套标签(如<form>,<p>或<a>)或使用非SVG元素时忘记了<tbody>在<svg>父级中。 Try inspecting the child nodes of the element with React ID ``. 尝试检查带有React ID``的元素的子节点。

Code I used to test: 我用来测试的代码:

describe(`Method: flattenData`, () => {

  const tests = [
    {
      toFlatten: {
        'key1': 'k',
        'key2': 'n',
        'key3': 'o'
      },
      expectedResult: 'key1: k; key2: n; key3: o'
    }
  ];

  tests.forEach((test) => {

    expect(instance.flattenData(test.toFlatten)).toEqual(test.expectedResult);

   });

});

I fixed this bug by adding a parameter to the render call. 我通过向render调用添加参数来修复此错误。 This forces the component (which is just a <td> </td> to be rendered inside a table. 这将强制组件(只是<td> </td>呈现在表中。

Before: 之前:

[container, instance] = render(TableCell, {
  data: `content`,
  dataTh: `content`,
  key: `content`,
  dataLocator: `content`
});

With the fix: 解决方法:

[container, instance] = render(TableCell, {
  data: `content`,
  dataTh: `content`,
  key: `content`,
  dataLocator: `content`
}, `table`);

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

相关问题 使用 Jasmine 在 React 组件上测试 onClick 事件 - Testing onClick event on react component using Jasmine 使用反应测试库测试我的组件时出现此错误 - Getting this error while testing my component using react testing library 使用业力茉莉花进行单元测试时出现控制器错误 - Getting error in controller while unit testing using karma jasmine 使用业力和茉莉测试具有依赖性的组件 - Testing a component with dependencies using karma & jasmine 使用 Jasmine 和 Karma 进行角度单元测试时出错 - Error while angular unit testing with Jasmine and Karma 使用反应测试库和茉莉花 - Using react-testing-library and Jasmine 使用业力,茉莉花,jQuery进行测试并使用Browserify进行反应 - Testing with karma, jasmine, jquery and react using browserify 单元测试使用Karma和Jasmine进行反应 - Unit testing react using Karma and Jasmine 收到此错误“错误:beforeEach 需要一个函数参数; 在使用 karma 和 jasmine 测试 angularjs 时收到 [object Object]” - Getting this error “Error: beforeEach expects a function argument; received [object Object]” while testing angularjs using karma and jasmine 使用Jasmine间谍进行Angular 2组件测试“没有Http提供商!”错误 - Angular 2 component testing with Jasmine spies “No provider for Http!” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM