0

I am using jasmine for node.js unit testing using karma tool.

I am getting an error while running it : " require is not defined".

I tried a lot of things like using karma-browserify , requirejs but none of them of worked.

I tried all the suggested solution in this link but none of them worked for me.

This is my karma.config.js

// Karma configuration
// Generated on Mon Nov 28 2016 13:37:38 GMT+0530 (India Standard Time)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: [
        'jasmine',
        'browserify'
    ],


    // list of files / patterns to load in the browser
    files: [
      'src/**/*.js',
      'src/server.js',
      'test/*.js'
    ],
      browserify: {
          debug: true,
          transform: [ 'brfs' ]
      },
      watchify: {
          poll: true
      },


    // list of files to exclude
    exclude: [

    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
        'test/*.js': [ 'browserify' ]
    },
      browserify: {
               bundleDelay: 2000,  // This may reduce instances of the karma-browserify "require is not defined" bug
              debug: true,
            },



    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
};
package.json
"devDependencies": {
    "browserify": "^13.1.1",
    "grunt": "^0.4.5",
    "grunt-contrib-jshint": "^0.11.3",
    "grunt-jasmine-node": "^0.3.1",
    "grunt-jscs": "^2.5.0",
    "jasmine-core": "^2.2.0",
    "karma": "^1.3.0",
    "karma-browserify": "^5.1.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-jasmine": "^1.0.2",
    "load-grunt-tasks": "^3.4.0",
    "watchify": "^3.7.0"
  }
Community
  • 1
  • 1
user7131571
  • 239
  • 2
  • 3
  • 12

1 Answers1

0

This error may be caused by require calls in your src/**/*.js and src/server.js files.

You load these files in browser:

   files: [
      'src/**/*.js',
      'src/server.js',
      'test/*.js'
   ],

Without preprocessing with broserify:

preprocessors: {
    'test/*.js': [ 'browserify' ]
},
idmitme
  • 899
  • 7
  • 13