【发布时间】:2018-04-29 23:36:00
【问题描述】:
我对 react 很陌生,我使用 React 14 编写了一个没有任何转译器的代码。现在我想使用 Karma-Jasmine 进行单元测试,但似乎我的测试无法渲染应用程序。
我有以下结构:
node_modules
MyApp/
/js/ *.js
/test/*.js
Karma.conf.js
package.json
index.html
我的 index.html:
<html>
<div id="content"></div>
<script src="js/react-0.14.7.js"></script>
<script src="js/react-dom-0.14.7.js"></script>
<script src="js/App.js"></script>
<script src="js/main.js"></script>
<link rel="stylesheet" href="style.css">
</body>
</html>
我的 main.js:
ReactDOM.render(React.createElement(App), document.getElementById('content'))
我的应用程序如下:
var h = React.createElement;
var Command = React.createClass({
render: function(){
return h(....)
}
})
我的测试代码如下:
describe('App', function() {
beforeEach(function() {
fixture.load('index.htm');
});
beforeEach(function () {
ReactDOM.render(React.createElement(App), document.getElementById('content'));
});
it('accepts elements', function() {
document.getElementById('x').value = 1;
document.getElementById('y').value = 2;
document.getElementById('setbtn').click();
});
});
这是错误:
Uncaught ReferenceError: App is not defined
at main.js:2
(anonymous) @ main.js:2
debug.html:1 (WEB_PAGE context) Lazy require of app.binding did not set the binding field
.
.
.
ReferenceError: fixture is not defined
at UserContext.<anonymous> (main.test.js:6)
调试 Karma 显示我的文件已加载,因为我可以看到组件中的功能。我安装了 Html2js 并添加到我的 Karma.conf.js 中。我已经阅读了网络上的大部分文档,但它们没有帮助。
我做错了什么?我
【问题讨论】:
-
Uncaught ReferenceError: App is not defined- 似乎您将其导出为名为“Command”而不是“App”的全局变量。此外,您需要包含 karma-fixture(请参阅 github.com/billtrik/karma-fixture 上的安装说明)。最后,您的 html 格式错误。没有开始正文标签
标签: javascript reactjs unit-testing jasmine karma-jasmine