1

I am trying to ran tests using karma and jasmine, but I am facing this "Uncaught ReferenceError: require is not defined" error. Sorry... could you help me to figure out what am I doing wrong?

  1. Please see my environment, where this error is happening, and directory structure is:

    myproj
    |_ tests
    |  |_ example_spec.js
    |
    |_ karma-conf.js
    |_ package.json
    
  2. The example_spec.js file has following code:

    var chai = require('chai');
    var expect = chai.expect;
    
    describe('Given an array', function () {
      'use strict';
      describe('the method push', function () {
        it('should add an element', function () {
          var arr = [];
          arr.push("hello");
          expect(arr.length).to.equal(1);
          expect(arr).to.contain("hello");
        });
      });
    });
    
  3. The package.json file has following dependencies:

    "devDependencies": {
      "grunt": "~0.4.5",
      "frisby": "~0.8.5",
      "load-grunt-config": "~0.16.0",
      "karma": "~0.12.31",
      "karma-jasmine": "~0.3.5",
      "karma-chrome-launcher": "~0.1.7",
      "karma-junit-reporter": "~0.2.2",
      "chai": "~2.2.0",
      "chai-as-promised": "~4.3.0",
      "jasmine-node-karma": "~1.6.1",
      "jasmine-node": "~1.14.5"
    }
    
  4. Then, I tried to ran test following command:

    ./node_modules/karma/bin/karma start karma-conf.js
    
  5. And following reference error is raised:

    Uncaught ReferenceError: require is not defined
    at /home/dir/dir/myproj/tests/example_spec.js:4
    

Note: Mocha is not involved in this project.

estelisa rva
  • 33
  • 1
  • 5

0 Answers0