[英]Enzyme/Jest Testing Error - Invariant Violation: Target container is not a DOM element
I'm new to doing unit testing and I keep getting the error: Invariant Violation: Target container is not a DOM element. 我是单元测试的新手,但总是收到错误消息:Invariant Violation:目标容器不是DOM元素。 I think it has to do with the way my app.js is structured because it keeps referencing this line ReactDOM.render(, document.getElementById('app'));.
我认为这与我的app.js的结构方式有关,因为它一直引用此行ReactDOM.render(,document.getElementById('app'));。 Here's how it's structured now:
现在的结构如下:
import React from 'react';
import ReactDOM from 'react-dom';
import HomePage from './HomePage';
class App extends React.Component {
render() {
return (
<div>
<HomePage />
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('app'));
Does this need to be restructured for testing purposes? 是否出于测试目的需要重组?
You should separate between the two parts of the file, the Component declaration & mounting into the Dom via ReactDom
. 您应该将文件的两个部分分开,即组件声明和通过
ReactDom
装入Dom。
The ReactDom
has nothing todo with tests. ReactDom
与测试无关。
import React from 'react'; 从'react'导入React; import ReactDOM from 'react-dom';
从'react-dom'导入ReactDOM; import HomePage from './HomePage';
从'./HomePage'导入HomePage;
// App.jsx
import React from 'react';
import HomePage from './HomePage';
export class App extends React.Component {
render() {
return (
<div>
<HomePage />
</div>
)
}
}
// index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import {App} from './App';
ReactDOM.render(<App />, document.getElementById('app'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.