2017-04-06 71 views
0

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

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) 

从一些interwebs侦探,似乎这是渲染<ReactHighcharts />作为子组件的固有问题。如何避免重组我的组件或使测试复杂化?

+0

为什么不在这里使用reactTestUtils? 'const myComponent = ReactTestUtils.renderIntoDocument();'这种方式你有组件的引用。 aka ..'myComponent.state'将是该组件的状态 –

回答

1

由于问题渲染<ReactHighcharts />作为子组件,而我们只是想确保父组件不炸毁,我们可以使用酶的shallow方法来呈现只有父组件没有把孩子:

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

您也可以使用快照测试来确保子组件正在传递正确的道具。 –

+0

好主意!在这个特殊情况下,我传递的唯一道具是我在可测试函数中生成的配置对象,所以在这里不需要。但仍然是一个好主意,我会记住其他组件。 – dangerismycat