【发布时间】:2018-02-22 05:03:18
【问题描述】:
以下是我的 react 组件单元测试规范文件(helloworld.spec.js):
import React from 'react';
import ReactTestUtils from 'react-dom/test-utils'; // ES6
import {HelloWorld} from './helloworld';
describe("HelloWorld Test",function(){
it("check header", function(){
var component = ReactTestUtils.renderIntoDocument(
<HelloWorld />
);
var eventRow = TestUtils.findRenderedDOMComponentWithTag(
component, 'h1'
);
expect(eventRow.getDOMNode().textContent)
.toEqual("template header");
});
});
这里是 helloworld.js:
import React from 'react';
export class HelloWorld extends React.Component{
render(){
return(
<h1>Hello World!</h1>
)
}
}
我已经为单元测试设置了 karma 和 jasmine 并得到了错误:
{ "message": "Uncaught ReferenceError: require is not defined\nat src/components/hello-world/helloworld.spec.js:3:14\n\nReferenceError: require is not defined\n at src/components/hello-world /helloworld.spec.js:3:14", "str": "Uncaught ReferenceError: require is not defined\nat src/components/hello-world/helloworld.spec.js:3:14\n\nReferenceError: require 未在 src/components/hello-world/helloworld.spec.js:3:14 处定义\n" }
但我在代码中没有看到 require 怎么没有定义。 有什么线索可以说明问题出在哪里,可能是配置问题?
【问题讨论】:
-
1.您能否分享./eventticketcontainer 的导出声明,2. 请检查您的package.json 和config.js 是否包含requireJS。
-
我已经尝试过使用另一个基本组件,但它仍然给出了“需要”错误。更新了代码 sn-p。你现在可以看看@SanchitGoel
标签: reactjs karma-jasmine