Enzyme と React を使い始めたばかりで、少し戸惑っています。React を必要としない単純なテストに合格しました。
src/containers/App/App.spec.js
:
import { expect } from 'chai'
import { shallow } from 'enzyme'
describe('hello world', function() {
it('hello world', () => {
expect(true).to.be.true;
})
});
npm test
hello world
✓ hello world
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 SUCCESS (0.011 secs / 0.005 secs)
TOTAL: 1 SUCCESS
ここで、App コンポーネントの基本的なテストを行い、それが div としてレンダリングされるかどうかを確認したいと思います [これは、ブラウザーにロードしたときに行われます]。ここで失敗が発生します。
src/containers/App/App.spec.js
:
import React from 'react'
import { expect } from 'chai'
import { shallow } from 'enzyme'
import App from './App'
import styles from './styles.module.css'
describe('<App />', function () {
it('renders as a div', () => {
const wrapper = shallow(<App />);
//wrapper.debug();
expect(wrapper.type()).to.eql('div');
})
});
npm test
<App />
✗ renders as a div
null is not an object (evaluating 'ReactInstrumentation.debugTool.onSetDisplayName')
ShallowComponentWrapper@tests.webpack.js:75869:34
_render@tests.webpack.js:75919:48
_batchedRender@tests.webpack.js:75901:20
batchedUpdates@tests.webpack.js:15977:16
batchedUpdates@tests.webpack.js:1068:35
render@tests.webpack.js:75894:31
render@tests.webpack.js:39176:40
tests.webpack.js:51532:33
perform@tests.webpack.js:2952:25
batchedUpdates@tests.webpack.js:15979:27
batchedUpdates@tests.webpack.js:1068:35
tests.webpack.js:51531:42
withSetStateAllowed@tests.webpack.js:40282:6
ShallowWrapper@tests.webpack.js:51530:39
shallow@tests.webpack.js:70431:39
tests.webpack.js:56497:40
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.016 secs / 0.005 secs)
null is not an object
エラーの原因を知りたいです。App
何かがオブジェクトであることを期待しているように感じますが、そうではありませんか?
ブラウザをレンダリングする実際のアプリは次のとおりです。
src/containers/App/App.js
import React from 'react'
import ReactDOM from 'react-dom'
import styles from './styles.module.css'
const App = React.createClass({
render: function() {
return (
<div className={styles.wrapper}>
<h1>
Hello!
</h1>
</div>
)
}
});
module.exports = App;
src/app.js
import React from 'react'
import ReactDOM from 'react-dom'
import 'font-awesome/css/font-awesome.css'
import './app.css'
import App from 'containers/App/App'
const mountNode = document.querySelector('#root');
ReactDOM.render(<App />, mountNode);
参考までに、私のpackage.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"start": "NODE_ENV=development ./node_modules/.bin/hjs-dev-server",
"test": "NODE_ENV=test ./node_modules/karma/bin/karma start karma.conf.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^6.3.7",
"babel-core": "^6.10.4",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.11.1",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.9.0",
"chai": "^3.5.0",
"chai-enzyme": "^0.5.0",
"css-loader": "^0.23.1",
"cssnano": "^3.7.3",
"dotenv": "^2.0.0",
"enzyme": "^2.4.1",
"expect": "^1.20.2",
"file-loader": "^0.9.0",
"hjs-webpack": "^8.3.0",
"jasmine-core": "^2.4.1",
"json-loader": "^0.5.4",
"karma": "^1.1.1",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-mocha": "^1.1.1",
"karma-phantomjs-launcher": "^1.0.1",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.26",
"karma-webpack": "^1.7.0",
"mocha": "^2.5.3",
"phantomjs-polyfill": "0.0.2",
"phantomjs-prebuilt": "^2.1.7",
"postcss-loader": "^0.9.1",
"precss": "^1.4.0",
"react-addons-test-utils": "^15.2.1",
"sinon": "^1.17.4",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "^1.13.1"
},
"dependencies": {
"font-awesome": "^4.6.3",
"react": "^15.2.1",
"react-dom": "^15.2.1",
"react-router": "^2.5.2"
}
}