Search code examples
javascriptnode.jsgulpjasminekarma-jasmine

Uncaught ReferenceError: Required is not define


I am new to jasmine-karma testing and stuck with some configuration issues. I've been gone through some tutorials and so far I've crated package.json, karma.conf.js and gulpfile.js.

When I try to run the test, it opens the chrome and karma starts to running. But it gives an error as below;

Uncaught ReferenceError: require is not defined

Here is my gulpfile.js file

'use strict'
var gulp = require('gulp');
var Server = require('karma').Server;


//Run test once and exit
gulp.task('test', function (done){
    new Server(
        {
            configFile: __dirname + '/karma.conf.js',
            singleRun: false
        }, done).start();
});

// Watch for file changes and re-run tests on each change
gulp.task('tdd', function (done) {
    new Server({
        configFile: __dirname + '/karma.conf.js'
    }, done).start();
});

gulp.task('default', ['tdd']);

Here's my karma.conf.js file.

// Karma configuration
// Generated on Fri Dec 22 2017 16:57:52 GMT+0530 (Sri Lanka 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'],


    // list of files / patterns to load in the browser
    files: [
        '*.js'
    ],


    // list of files / patterns to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // 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
  })
}

Here is packages.json

{
  "name": "package",
  "version": "1.0.0",
  "description": "THis version of the webdriver works with Windows 10 post fall 2005 update",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.9.1",
    "jasmine-core": "^2.8.0",
    "karma": "2.0.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-jasmine": "^1.1.1"
  }
}

Another problem I'm facing is, these tests are going to run locally first and then those tests are going to checked-in to server. How I can test these jasmine tests in the server side? Do I need any addtional setups?


Solution

  • It seems like you're trying to run a client-side test for server-side code (eg. your gulpfile).

    Node.js is run on the V8 engine which is also used by google, but has a different global object containing different properties.

    In browsers the standard global object is 'window', which for example contains the 'document' property. Therefore document, window.document and this.document are all the same.

    Althought if you try to use 'document' in a nodejs environment, you will get the same type error.

    Buttom line - Google Chrome does not know what 'require' is since it's not a property within the global 'window' object.