0

Following is my spec file for the unit testing of the react component (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");
  });
});

And here is the helloworld.js:

import React from 'react';

export class HelloWorld extends React.Component{
    render(){
        return(
            <h1>Hello World!</h1>
        )
    }
}

I have setup karma and jasmine for the unit testing and getting the error :

{ "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 is not defined\n at src/components/hello-world/helloworld.spec.js:3:14" }

But I don't see require in the code so how come it's not defined. Any leads for where the issue could be, probably a config one?

Learner
  • 800
  • 1
  • 12
  • 34
  • 1. Can you please share export statement of ./eventticketcontainer, 2. Please check your package.json and config.js for any inclusion of requireJS. – Sanchit Goel Feb 22 '18 at 07:08
  • I have tried it out with another basic component, still it gives the 'require' error. Updated the code snippet. Can you have a look now @SanchitGoel – Learner Feb 22 '18 at 10:18

1 Answers1

0

It actually means you've to define require. Here is the link to stackoverflow

This is because require() does not exist in the browser/client-side JavaScript

sbp
  • 913
  • 8
  • 19
  • But I haven't used require() anywhere in my event-ticket.spec.js file. Where is it coming from? Can you explain why do I need to do that? – Learner Feb 22 '18 at 05:11