【问题标题】:React + Karma : Uncaught ReferenceError: require is not defined\natReact + Karma : 未捕获的 ReferenceError: 要求未定义\nat
【发布时间】: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


【解决方案1】:

这实际上意味着你必须定义require。 Here 是 stackoverflow 的链接

这是因为浏览器/客户端 JavaScript 中不存在 require()

【讨论】:

  • 但是我没有在 event-ticket.spec.js 文件中的任何地方使用过 require() 。它来自哪里?你能解释一下为什么我需要这样做吗?
猜你喜欢
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 2012-12-16
  • 2018-02-24
  • 2017-02-28
相关资源
最近更新 更多