Test with karma is working, and coverage says it's running, but it doesn't store a file anywhere

4,250 views
Skip to first unread message

David Karr

unread,
Oct 3, 2013, 1:20:11 PM10/3/13
to karma...@googlegroups.com
Win7x32, Karma, Phantomjs, AngularJS, Jasmine, Maven.

I now have a single test that is running successfully.  I even tried forcing the test to fail by changing the conditions checked for.  It works fine.

I'm still having trouble generating code coverage, which I'll need to eventually integrate with Sonar. I'm somewhat intimidated by reports like this, which make it seem like a huge number of hacks are required to get really useful information.  Nevertheless, I'm still trying to move forward.

What appears to be happening right now is that the coverage preprocessor seems to be running, but I'm not getting any data stored anywhere.

This is the current state of my karma.conf.js file:
module.exports = function(config) {
  config
.set({
    basePath
: '',
    frameworks
: ['jasmine'],
    files
: [
     
"../../../../src/test/webapp/js/libs/angular.js",
     
"../../../../src/test/webapp/js/libs/angular-mocks.js",
     
"../../../../src/main/webapp/js/*.js",
     
"../../../../src/test/webapp/js/*.js"
   
],
    exclude
: [    ],
    plugins
:[
             
'karma-jasmine',
             
'karma-coverage',
             
'karma-phantomjs-launcher'
             
],
    preprocessors
: {
       
"../../../../src/main/webapp/js/*.js": 'coverage'
   
},
    coverageReporter
: {
        type
: "html",
        dir
: "target/karma-coverage"
   
},
    reporters
: ['dots', 'junit', 'coverage'],
    port
: 9876,
    colors
: false,
    logLevel
: config.LOG_DEBUG,
    autoWatch
: false,
    browsers
: ['PhantomJS'],
    captureTimeout
: 60000,
    singleRun
: true
 
});
};



The following is an excerpt from my build output. Note the "preprocessor.coverage" line:
cmd /C karma start myhome\workspace6\angularjsexp\src\test\webapp\js\karma.conf.js --browsers PhantomJS --reporters dots,coverage --single-run --no-auto-watch --colors false
DEBUG
[plugin]: Loading plugin karma-jasmine.
DEBUG
[plugin]: Loading plugin karma-coverage.
DEBUG
[plugin]: Loading plugin karma-phantomjs-launcher.
DEBUG
[plugin]: Loading inlined plugin (defining ).
INFO
[karma]: Karma v0.10.2 server started at http://localhost:9876/
INFO
[launcher]: Starting browser PhantomJS
DEBUG
[launcher]: Creating temp dir at C:\Cygwin\tmp\karma-3247752
DEBUG
[launcher]: myhome\AppData\Roaming\npm\node_modules\karma-phantomjs-launcher\node_modules\phantomjs\lib\phantom\phantomjs.exe C:\Cygwin\tmp\karma-3247752/capture.js
DEBUG
[watcher]: Excluded file "myhome/workspace6/angularjsexp/src/test/webapp/js/karma.conf.js"
DEBUG
[preprocessor.coverage]: Processing "myhome/workspace6/angularjsexp/src/main/webapp/js/blog.js".
DEBUG
[watcher]: Resolved files:
    myhome
/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js
    myhome
/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js
    myhome
/workspace6/angularjsexp/src/test/webapp/js/libs/angular.js
    myhome
/workspace6/angularjsexp/src/test/webapp/js/libs/angular-mocks.js
    myhome
/workspace6/angularjsexp/src/main/webapp/js/blog.js
    myhome
/workspace6/angularjsexp/src/test/webapp/js/blogSpec.js
DEBUG
[web-server]: serving: myhome\AppData\Roaming\npm\node_modules\karma\static/client.html
DEBUG
[web-server]: serving: myhome\AppData\Roaming\npm\node_modules\karma\static/karma.js
DEBUG
[karma]: A browser has connected on socket i8z2fLFVN5jEUJNjRSPL
INFO
[PhantomJS 1.9.2 (Windows 7)]: Connected on socket i8z2fLFVN5jEUJNjRSPL
DEBUG
[karma]: All browsers are ready, executing
DEBUG
[web-server]: serving: myhome\AppData\Roaming\npm\node_modules\karma\static/context.html
DEBUG
[web-server]: serving: myhome/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js
DEBUG
[web-server]: serving: myhome/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js
DEBUG
[web-server]: serving: C:\Cygwin\tmp/f83cc12edd8df4433c233d87dfc0474d118dce14.js
DEBUG
[web-server]: serving: myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular.js
DEBUG
[web-server]: serving: myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular-mocks.js
DEBUG
[web-server]: serving: myhome/workspace6/angularjsexp/src/test/webapp/js/blogSpec.js
.
PhantomJS 1.9.2 (Windows 7): Executed 1 of 1 SUCCESS (0.194 secs / 0.008 secs)
DEBUG
[launcher]: Disconnecting all browsers
DEBUG
[launcher]: Killing PhantomJS
DEBUG
[launcher]: Process PhantomJS exitted with code 0
DEBUG
[launcher]: Cleaning temp dir C:\Cygwin\tmp\karma-3247752



Note that I ran the build in a Cygwin Bash shell.  I tried the same in a "cmd" window, and the results were identical.

At the end of this process, I would have expected a "target/karma-coverage" directory to be created, with code coverage data in it.  The "target" directory exists, but not "karma-coverage".

David Karr

unread,
Oct 3, 2013, 4:42:26 PM10/3/13
to karma...@googlegroups.com
 
I managed to get this to work, but inadvertently.  I made what I thought was an irrelevant change that makes this work.

I decided that I didn't like all the "../../../../" paths, so I set my "basepath" to "../../../.." and changed all the path references from "../../../../src" to "src".  When I made this change, my test continued to run, AND it was then generating coverage data.  It didn't occur to me that the paths in the "coverageReporter" element were also relative to basepath.  Once I made the "basepath" change, that then made the "target" reference valid.  You would think the code would at least mention, like, "you do know the directory prefix of your dir property doesn't exist, right?".

Michael Bielski

unread,
Oct 4, 2013, 11:19:01 AM10/4/13
to karma...@googlegroups.com
I'm curious why you didn't use absolute paths instead of the relative paths?

David Karr

unread,
Oct 4, 2013, 3:48:47 PM10/4/13
to karma...@googlegroups.com


On Friday, October 4, 2013 8:19:01 AM UTC-7, Michael Bielski wrote:
I'm curious why you didn't use absolute paths instead of the relative paths?

Seriously?  You mean absolute paths on my windows box that wouldn't work on any other computer?

Michael Bielski

unread,
Oct 4, 2013, 5:46:47 PM10/4/13
to karma...@googlegroups.com
If you are part of a development team (I am) it isn't too much to ask that everyone store their code in the same folder structure (my team does.) Configuring that same folder structure for your own machines (regardless of the OS) is even easier. If you're worried about shipping your tests with your project, it is generally understood that configs are different and will have to be adjusted before tests can be run.

Those things aside, the base path is your friend. Once you get your test config onto another machine you can alter the base path to suit your folder structure. Your config file does not have to be in your source control (but it does help.)

Vojta Jína

unread,
Oct 6, 2013, 8:32:37 PM10/6/13
to karma...@googlegroups.com
David, this sounds like a bug in the coverage plugin.

I tried to make a similar change in the coverage e2e test (https://github.com/karma-runner/karma/tree/master/test/e2e/coverage), but it worked fine. So I'm not sure how to reproduce it. If you can track down how to reproduce it and file an issue at https://github.com/karma-runner/karma-coverage, that'd be great.

As per absolute paths, I'm would never use absolute paths. Your current config is the way to go imho.

V.


On Fri, Oct 4, 2013 at 11:46 PM, Michael Bielski <michael...@yahoo.com> wrote:
If you are part of a development team (I am) it isn't too much to ask that everyone store their code in the same folder structure (my team does.) Configuring that same folder structure for your own machines (regardless of the OS) is even easier. If you're worried about shipping your tests with your project, it is generally understood that configs are different and will have to be adjusted before tests can be run.

Those things aside, the base path is your friend. Once you get your test config onto another machine you can alter the base path to suit your folder structure. Your config file does not have to be in your source control (but it does help.)

--
You received this message because you are subscribed to the Google Groups "karma-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to karma-users...@googlegroups.com.
To post to this group, send email to karma...@googlegroups.com.
Visit this group at http://groups.google.com/group/karma-users.
For more options, visit https://groups.google.com/groups/opt_out.

manunath...@gmail.com

unread,
Nov 7, 2013, 4:49:18 AM11/7/13
to karma...@googlegroups.com
Is this issue fixed, I am not able to get code coverage working with karma. Below is my karma conf

// Karma configuration
// Generated on Wed Nov 06 2013 15:51:57 GMT+0000 (GMT Standard Time)

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

    // base path, that will be used to resolve files and exclude
    basePath: '',


    // frameworks to use
    frameworks: ['jasmine', 'requirejs'],


    // list of files / patterns to load in the browser
    files: [

      
      {pattern: 'src/**/*.js', included: false},
      {pattern: 'spec/*.js', included: false},
 'test-main.js'
    ],


    // list of files to exclude
    exclude: [
      
    ],
//plugins:['karma-jasmine','karma-coverage','karma-requirejs','karma-phantomjs-launcher'],


     // 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_DEBUG,


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


    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera (has to be installed with `npm install karma-opera-launcher`)
    // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
    // - PhantomJS
    // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
    browsers: ['PhantomJS'],


    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,


    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: true,
reporters:['coverage','progress'],
preprocessors:{
   '**/src/model/*.js':'coverage'
},
coverageReporter : {
 type:'html',
 dir:'coverage/kk'
}
  });
};

test-main.js

 var tests = [];
for (var file in window.__karma__.files) {
  if (window.__karma__.files.hasOwnProperty(file)) {
    if (/Spec\.js$/.test(file)) {
//console.log(file)
      tests.push(file);
     }
  }
}

requirejs.config({
    // Karma serves files from '/base'
    baseUrl: '/base',

    paths: {
    },

    shim: {
    },

    // ask Require.js to load these files (all our tests)
    deps: tests,

    // start test run, once Require.js is done
    callback: window.__karma__.start
});

and log

C:\jsapp>karma start manju.conf.js
DEBUG [plugin]: Loading karma-* from C:\Users\manjurn\AppData\Roaming\npm\node_modules
DEBUG [plugin]: Loading plugin C:\Users\manjurn\AppData\Roaming\npm\node_modules/karma-coffee-preprocessor.
DEBUG [plugin]: Loading plugin C:\Users\manjurn\AppData\Roaming\npm\node_modules/karma-coverage.
DEBUG [plugin]: Loading plugin C:\Users\manjurn\AppData\Roaming\npm\node_modules/karma-firefox-launcher.
DEBUG [plugin]: Loading plugin C:\Users\manjurn\AppData\Roaming\npm\node_modules/karma-html2js-preprocessor.
DEBUG [plugin]: Loading plugin C:\Users\manjurn\AppData\Roaming\npm\node_modules/karma-jasmine.
DEBUG [plugin]: Loading plugin C:\Users\manjurn\AppData\Roaming\npm\node_modules/karma-phantomjs-launcher.
DEBUG [plugin]: Loading plugin C:\Users\manjurn\AppData\Roaming\npm\node_modules/karma-requirejs.
DEBUG [plugin]: Loading plugin C:\Users\manjurn\AppData\Roaming\npm\node_modules/karma-script-launcher.
DEBUG [plugin]: Loading inlined plugin (defining ).
INFO [karma]: Karma v0.10.4 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
DEBUG [launcher]: Creating temp dir at C:\Users\manjurn\AppData\Local\Temp\karma-54947652
DEBUG [launcher]: C:\Users\manjurn\AppData\Roaming\npm\node_modules\karma-phantomjs-launcher\node_modules\phantomjs\lib\phantom\phantomjs.exe C:\Users\manjurn\AppData\Local\Temp\karma-5494765
DEBUG [preprocessor.coverage]: Processing "C:/jsapp/src/model/RomanNumeralEncoder.js".
DEBUG [watcher]: Resolved files:
        C:/Users/manjurn/AppData/Roaming/npm/node_modules/karma-requirejs/lib/require.js
        C:/Users/manjurn/AppData/Roaming/npm/node_modules/karma-requirejs/lib/adapter.js
        C:/Users/manjurn/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js
        C:/Users/manjurn/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js
        C:/jsapp/src/model/RomanNumeralEncoder.js
        C:/jsapp/spec/RomanNumeralEncoderSpec.js
        C:/jsapp/test-main.js
DEBUG [web-server]: serving: C:\Users\manjurn\AppData\Roaming\npm\node_modules\karma\static/client.html
DEBUG [web-server]: serving: C:\Users\manjurn\AppData\Roaming\npm\node_modules\karma\static/karma.js
DEBUG [karma]: A browser has connected on socket QCfAPOz9ySy5TLTL8QzJ
INFO [PhantomJS 1.9.2 (Windows 7)]: Connected on socket QCfAPOz9ySy5TLTL8QzJ
DEBUG [karma]: All browsers are ready, executing
DEBUG [web-server]: serving: C:\Users\manjurn\AppData\Roaming\npm\node_modules\karma\static/context.html
DEBUG [web-server]: serving: C:/Users/manjurn/AppData/Roaming/npm/node_modules/karma-requirejs/lib/require.js
DEBUG [web-server]: serving: C:/Users/manjurn/AppData/Roaming/npm/node_modules/karma-requirejs/lib/adapter.js
DEBUG [web-server]: serving: C:/Users/manjurn/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js
DEBUG [web-server]: serving: C:/Users/manjurn/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js
DEBUG [web-server]: serving: C:/jsapp/test-main.js
DEBUG [web-server]: serving: C:/jsapp/spec/RomanNumeralEncoderSpec.js
DEBUG [web-server]: serving: C:\Users\manjurn\AppData\Local\Temp/30f0eb0708fb25d9f32e0a2cede48a7eaae2ed9a.js
PhantomJS 1.9.2 (Windows 7): Executed 2 of 2 SUCCESS (0.092 secs / 0.005 secs)
DEBUG [launcher]: Disconnecting all browsers
DEBUG [launcher]: Killing PhantomJS
DEBUG [launcher]: Process PhantomJS exitted with code 0
DEBUG [launcher]: Cleaning temp dir C:\Users\manjurn\AppData\Local\Temp\karma-54947652

Can someone help me what is that i am making wrong?

Karma version: 0.10.4
node version : v0.10.21

Vojta Jína

unread,
Nov 10, 2013, 10:49:58 AM11/10/13
to karma...@googlegroups.com
I need a help to reproduce this issue. If you can guys post a link to a simple example of this problem (a minimized project that shows the problem), that I can run to see the issue, that would help...

V.


Reply all
Reply to author
Forward
0 new messages