0

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?

Community
  • 1
  • 1
Pegasus
  • 88
  • 2
  • 9
  • 1
    See [Client on node: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/q/19059580/6188402) – Washington Guedes Dec 26 '17 at 14:33
  • @WashingtonGuedes will do. Thanks. – Pegasus Dec 26 '17 at 14:44
  • still can't figure out how to apply that to my issue. – Pegasus Dec 26 '17 at 15:02
  • The gulp test created seems wrong somehow. You need to pass options but you are assigning a function object using configFile. Is that an allowed option def? http://karma-runner.github.io/2.0/dev/public-api.html – Gary Dec 26 '17 at 15:50

1 Answers1

0

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.

fingeron
  • 1,152
  • 1
  • 9
  • 20
  • Thank you. Now I can understand the problem here. But do you have any thoughts on how to solving this? – Pegasus Dec 27 '17 at 03:33
  • @Pegasus What exactly is it you're trying to test? The javascript in Node.js and the javascript in Google Chrome run in different environments, and as far as I know, Karma is used for client-side (browser) testing. Although I found this: http://mherman.org/blog/2015/09/10/testing-node-js-with-mocha-and-chai which might help you find a way to test your node.js code. If you're trying to test your client side code, you must set the `files` property in `karma.conf.js` to: `path/to/client/code/**/*.js` – fingeron Dec 27 '17 at 07:29
  • I need jasmine to do unit test in AngularJS controllers in visual studio project. I need to figure out how to configure jasmine in visual studio. – Pegasus Dec 27 '17 at 11:51
  • `files: [ '*.js' ],` Isn't this what you're looking for? Just change the path to the `app` folder containing all AngularJS code. For example: `files: [ './app/**/*.js', './assets/**/*.js` ]` etc. – fingeron Dec 27 '17 at 13:51