【问题标题】:React Highcharts Jest testing error: `InvalidCharacterError`React Highcharts Jest 测试错误:`InvalidCharacterError`
【发布时间】:2017-04-06 20:51:33
【问题描述】:

我在尝试对使用 react-highcharts 的 React 组件进行一些基本的冒烟测试时遇到了问题。我使用基本 Jest 的典型方法会产生错误:

it('renders without crashing', () => {
  const div = document.createElement('div');
  render(<MyComponent {...props} />, div);
});

 —>
InvalidCharacterError

  at exports.name (node_modules/jest-environmentjsdom/node_modules/jsdom/lib/jsdom/living/helpers/validate-names.js:10:11)
  at a.createElement (node_modules/highcharts/highcharts.js:17:221)
  at Object.a.svg.z.init (node_modules/highcharts/highcharts.js:92:155)
  at Object.z.createElement (node_modules/highcharts/highcharts.js:63:3)
  at Object.a.svg.z.createElement (node_modules/highcharts/highcharts.js:107:525)
  at Object.a.svg.z.init (node_modules/highcharts/highcharts.js:101:44)
  at Object.a.svg.a.VMLRenderer.B (node_modules/highcharts/highcharts.js:109:320)
  at Object.N.getContainer (node_modules/highcharts/highcharts.js:252:329)

从一些互联网调查来看,这似乎是将&lt;ReactHighcharts /&gt; 渲染为子组件的固有问题。如何在不重组组件或使测试复杂化的情况下解决此问题?

【问题讨论】:

  • 为什么不在这里使用 reactTestUtils? const myComponent = ReactTestUtils.renderIntoDocument(&lt;MyComponent /&gt;); 这样你就有了组件的引用。又名.. myComponent.state 将是该组件的状态

标签: unit-testing reactjs highcharts jestjs smoke-testing


【解决方案1】:

由于问题是将 &lt;ReactHighcharts /&gt; 渲染为子组件,而我们只是试图确保父组件不会爆炸,我们可以使用 Enzyme 的 shallow 方法仅渲染该父组件而无需孩子们:

it('renders without crashing', () => {
  expect(shallow(<MyComponent {...props} />).exists()).toBeTruthy();
});

【讨论】:

  • 您还可以使用快照测试来确保子组件通过了正确的道具。
  • 好主意!在这种特殊情况下,我传递的唯一道具是我在可测试函数中生成的配置对象,因此这里没有必要。但仍然是个好主意,对于其他组件,我会牢记这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 2021-12-24
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多