我有一个使用react_on_rails gem的应用程序。一切都很好。当我使用Mocha + TestUtils + jsdom编写测试时,我收到了错误消息
TypeError: Cannot read property 'addEventListener' of undefined
我找到了类似设置和相同错误消息here的答案。按照建议的说明进行操作。现在我遇到了来自react-rails的错误消息:
client/node_modules/react-on-rails/node_package/lib/ReactOnRails.js:238
ReactOnRails.resetOptions();
^
ReferenceError: ReactOnRails is not defined
由于我的应用程序很复杂,为了重现相同的错误信息,我写了一个准系统反应应用程序并使用相同的测试工具。
hello.jsx
import React from 'react';
export default class Hello extends React.Component {
render() {
return (
<div>
<h1>Hello world</h1>
</div>
);
}
}
test.js
import Hello from '../hello.jsx';
import addons from 'react/addons';
const TestUtils = addons.addons.TestUtils;
import React from 'react';
var jsdom = require('jsdom');
import assert from 'assert';
import ror from 'react-on-rails';
describe('render correctly', function() {
it('should render', function() {
let comp = TestUtils.renderIntoDocument(<Hello/>);
let h1 = TestUtils.findRenderedDOMComponentWithTag(comp, 'h1');
assert.equal(h1.getDOMNode().textContent, 'Hello world');
});
});
的package.json
{
"name": "react-testing",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha --compilers js:babel-core/register test/tests.js --require test/setup.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^0.14.8",
"react-dom": "^15.0.1",
"react-on-rails": "^5.2.0",
"react-tools": "^0.13.3"
},
"devDependencies": {
"babel-core": "^6.7.4",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"glob": "^4.3.5",
"jsdom": "^3.1.1",
"mocha": "2.3.3",
"mocha-jsdom": "^0.2.1",
"mocha-lcov-reporter": "0.0.1",
"webpack": "^1.12.14"
}
}
我没有为这个简单的应用包含npm包react-on-rails
。但重点是重现我在rails上做出反应的错误消息,所以我在应用程序中有它。
当我运行npm test
时,我收到了完全相同的错误消息:
node_modules/react-on-rails/node_package/lib/ReactOnRails.js:238
ReactOnRails.resetOptions();
^
ReferenceError: ReactOnRails is not defined
知道如何解决这个问题吗?
答案 0 :(得分:0)
我认为将Mact测试包含在ReactOnRails npm库中是不合适的。如果您想使用ReactOnRails进行真正的集成测试,请使用capybara测试。有关示例,请参阅https://github.com/shakacode/react_on_rails/tree/master/spec/dummy的/ spec / dummy。
我是React on Rails的主要作者。