我是进行单元测试的新手,并且不断收到此错误:
永久违反:目标容器不是DOM元素。
不变(node_modules / fbjs / lib / invariant.js:42:15)
在legacyRenderSubtreeIntoContainer(node_modules / react-dom / cjs / react-dom.development.js:17238:34)
在Object.render(node_modules / react-dom / cjs / react-dom.development.js:17317:12)
at Object。 (src / client / index.js:243:73)
at Object。 (src / client / components / block / Header.js:294:40)
at Object。 (src / client / components / block / index.js:123:41)
at Object。 (src / client / containers / app.js:195:40)
at Object。 (tests / client / containers / App.test.js:13:12)
我认为这与我的app.js的结构方式有关,但不知道为什么...
这是我的App.js:
const App = () => (
<Box>
<Router getUserConfirmation={getConfirmation} hashType={'noslash'} basename={'/'}>
<Box>
<Route exact path="/" component={Home}/>
<Route path="/:room" component={Room}/>
</Box>
</Router>
</Box>
)
const mapStateToProps = (state) => {
return {
message: state.message
}
}
export default connect(mapStateToProps, null)(App)
我正在使用jest / enzyme / chai进行单元测试
我的App.test.js:
import { expect } from 'chai'
import React from 'react'
import { shallow } from 'enzyme'
import App from '../../../src/client/containers/app'
jest.mock('react-router')
describe('<App />', () => {
it('renders without crashing', () => {
const wrapper = shallow(
<App/>
).dive()
})
})
package.json中的我的devDependecies:
"devDependencies": {
"babel-cli": "^6.7.7",
"babel-core": "^6.7.7",
"babel-eslint": "^7.2.2",
"babel-jest": "^23.4.0",
"babel-loader": "^6.2.4",
"babel-plugin-__coverage__": "^0.111111.1",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.5.0",
"babel-watch": "^2.0.2",
"chai": "^4.1.2",
"chai-enzyme": "^1.0.0-beta.1",
"chai-jest-diff": "^1.0.2",
"chai-jest-snapshot": "^2.0.0",
"dirty-chai": "^2.0.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "^4.0.0",
"eslint-plugin-babel": "^3.3.0",
"eslint-plugin-react": "^7.10.0",
"jest": "^23.4.1",
"nyc": "^6.4.4",
"react-test-renderer": "^16.4.1",
"redux-devtools-extension": "^2.13.5",
"webpack": "^1.13.0",
"webpack-dev-middleware": "^1.6.1",
"webpack-dev-server": "^1.14.1",
"webpack-hot-middleware": "^2.10.0"
},
和我的webpack.config.js:
var path = require('path');
module.exports = {
entry: './src/client/index.js',
output: {
path: path.join(__dirname, 'build'),
filename: 'bundle.js'
},
devServer: {
compress: true,
disableHostCheck: true,
},
watchOptions: {
poll: 1000,
aggregateTimeout:300,
},
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel',
query:{
presets: ["es2015", "react", "stage-0"]
}
}]
}
};
谢谢您的帮助:)
编辑:
在寻找解决方案时,我注意到在导入组件期间发生错误!它会来自webpack吗?
编辑:
我的错误出在HEADER.JS中,因为我使用persistor来管理用户连接。 我暂时将其删除
答案 0 :(得分:0)
您要确保id="root"
元素上未设置index.html
文件中带有body
的元素。原因是在脚本完成执行时,document
元素尚不可用。
请参阅@Dan Abramov here的答案。