Search code examples
karma-jasminekarma-coverage

Karma-Coverage always outputs 100%


I'm having really hard time starting to write unit tests for my app. My coverage always reports 100%. I did simple test adn the result should be 50% but karma-coverage doesn't think so. How ever karma progress reports right results but i need to make coverage work.

This is my karma.conf.coffee:

module.exports = (config) ->
config.set
basePath: ""
frameworks: ["jasmine"]
files: [
  #"server/*_spec.js",
  #"server/*.js",
  #"client/app/final/.js"
  #"client/*_spec.js"
  "test.js"
  "test_spec.js"
]
exclude: []
preprocessors: {}
reporters: ["progress", "coverage"]
preprocessors:
  "test.js": ["coverage"]
coverageReporter:
  type : "html",
  dir : "coverage/"
port: 9876
colors: true
logLevel: config.LOG_INFO
autoWatch: false
browsers: [
  "Chrome"
  #"Firefox" add to package.json::devDependencies:["karma-firefox-launcher"]
  #"PhantomJS" add to package.json::dependencies:["phantomjs"] and package.json::devDependencies["karma-phantomjs-launcher"]
]
singleRun: true
concurrency: Infinity

And this is test.js:

function test(a) {
   return a
}

function ivo() {
   return "ivo"
}

And finally test_spec.js

describe("test", function() {
      it("it tests", function() {
    expect(test(3)).toEqual(3);
  });
});

describe("ivo", function() {
  it("it ivos", function() {
    expect(ivo()).toEqual("ovi");
  });
});

Code Coverage

Karma progress result:

karma start
31 12 2015 17:12:48.598:INFO [karma]: Karma v0.13.16 server started at    http://localhost:9876/
31 12 2015 17:12:48.610:INFO [launcher]: Starting browser Chrome
31 12 2015 17:12:50.007:INFO [Chrome 47.0.2526 (Linux 0.0.0)]:  Connected on socket q97o45wS4ctU_z4DAAAA with id 35753464
Chrome 47.0.2526 (Linux 0.0.0) ivo it ivos FAILED
    Expected 'ivo' to equal 'ivo1'.
        at Object.<anonymous> (/home/ivo/ivo/diplomna3/test_spec.js:9:19)
Chrome 47.0.2526 (Linux 0.0.0): Executed 2 of 2 (1 FAILED) (0.004 secs / 0.01 secs)

Solution

  • Test coverage indicates the percentage of the source that is tested, not the percentage of tests that pass.

    Because you have two functions in test.js and there is a test for each function, the coverage is 100%.

    However, only one test actually passes. So your success rate is 50%.