Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do not able to execute karma unit test in PhantomJS browser

In grunt we execute unit test in PhantomJs by following ways.

karma: {
        options: {
    configFile: 'karma.conf.js',
    runnerPort: 9876,
    browsers: ['Chrome']
    },
unitTestDev: {
        browsers: ['PhantomJS'],
        singleRun: true
     }
}

grunt.registerTask('unitTest',['karma:unitTestDev']);

Here is karma.conf.js file.

    // Karma configuration
// Generated on Thu Apr 03 2014 13:44:39 GMT-0500 (Central Daylight 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: [
        {pattern: 'dist/lib/jquery/dist/jquery.js', watched: false, served: true, included: true},
        {pattern: 'dist/lib/jasmine-jquery/lib/jasmine-jquery.js', watched: false, served: true, included: true},
        {pattern: 'dist/lib/angular/angular.js', watched: false, served: true, included: true},
        {pattern: 'dist/lib/angular-route/angular-route.min.js', watched: false, served: true, included: true},
        {pattern: 'dist/lib/angular-mocks/angular-mocks.js', watched: false, served: true, included: true},

        {pattern: 'dist/main.min.js', watched: false, served: true, included: true},

        {pattern: 'public/test/unit/**/*Spec.js', watched: false, served: true, included: true},

        // fixtures
        {pattern: 'public/test/mock/**/*.json', watched: true, served: true, included: false}
    ],


    // 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 results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress', 'html', 'junit', 'story'],

    htmlReporter: {
      outputDir: 'build/unit-test/html-report',
      templatePath: 'node_modules/karma-html-reporter/jasmine_template.html'
    },

    junitReporter: {
      outputFile: 'build/unit-test/junit-report/test-results.xml',
      suite: ''
    },

    // 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: false,


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

When execute "karma:unitTestDev" command, then getting following errors.

Running "karma:unitTestDev" (karma) task
INFO [karma]: Karma v0.11.14 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
ERROR [launcher]: Cannot start PhantomJS
    Error: spawn OK
INFO [launcher]: Trying to start PhantomJS again (1/2).
ERROR [launcher]: Cannot start PhantomJS
    Error: spawn OK
INFO [launcher]: Trying to start PhantomJS again (2/2).
ERROR [launcher]: Cannot start PhantomJS
    Error: spawn OK
ERROR [launcher]: PhantomJS failed 2 times (cannot start). Giving up.
Warning: Task "karma:unitTestDev" failed. Use --force to continue.

Aborted due to warnings.

We are getting above issue in Windows and Linux OS. In Mac, karma successfully execute tests on PhantomJS.

In windows, If we execute above test in Chrome browser, instead of PhantomJS, then karma successfully execute all unit tests.

Here are karma related dependencies in Package.json

"phantomjs": "~1.9.7-3",
"karma": "~0.12.3",
"karma-chrome-launcher": "~0.1.3",
"karma-jasmine": "~0.1.5",
"karma-html-reporter": "~0.2.3",
"karma-junit-reporter": "~0.2.1",
"grunt-karma": "~0.8.2",
"karma-phantomjs-launcher": "~0.1.3"

Let me know if are there any known issue in karma or grunt with PhantomJS.

like image 886
furqan kamani Avatar asked Apr 10 '14 23:04

furqan kamani


People also ask

How do I run a karma test without a browser?

Correct - Karma requires a browser to run. BUT - you can run Chrome in Headless mode, which means although you do need the browser installed, it will not open it's UI, and you can therefore run the tests purely through an SSH session for example.

What is Karma in unit testing?

Karma is a node-based test tool that allows you to test your JavaScript codes across multiple real browsers. A node-based tool is any tool that needs the Nodejs engine installed for it to run and can be accessed (installed) through the node package manager (npm).

What is karma Phantomjs launcher?

The karma-phantomjs-launcher enables the karma test runner to launch and interact with PhantomJS. Use NPM to install the karma-phantomjs-launcher package as a dev dependency by executing the following command in the root of the project: npm install karma-phantomjs-launcher --save-dev.


Video Answer


1 Answers

There is an issue with "Karma-phantomjs-launcher" v0.1.3. Change it to v0.1.4 and then it work fine.

like image 125
furqan kamani Avatar answered Oct 18 '22 00:10

furqan kamani