26

I want to obtain a report of all successful jasmine specs runned with karma, something like you obtain when using jasmine alone.

Is there anyway to achieve this?

laurent
  • 88,262
  • 77
  • 290
  • 428
Tomas Romero
  • 8,418
  • 11
  • 50
  • 72

4 Answers4

22

Try this quick plugin I wrote:

https://github.com/dtabuenc/karma-html-reporter

Daniel Tabuenca
  • 13,147
  • 3
  • 35
  • 38
8

tl;dr

Yes, but it's non-trivial, and even more so if you want --auto-watch. So, basically, no :-( Blame it on lib/reporters/Progress.js, which swallows successful test results and spits out a summary line for each browser.

If you're determined, though, you have (at least) two non-trivial ways (in v0.9.2) of getting a "good enough" result (one of which, if you do it, you should polish up and submit as a pull request :-))

Option 1: jUnit scraping

As with most test frameworks, you can get Karma to output results in the jUnit XML format, which you can then post-process...

By default, karma start --reporters=junit will write a jUnit report to ${basePath}/test-results.xml, but you can override this with the junitReporter.outputFile config item.

Keep in mind that you can combine the jUnit output with other reporters (growl, etc.) on the command-line: e.g., karma start --reporters=junit,growl

Post-processing with ant

Post-processing with XSLT

Post-processing with xmlstarlet

I've always ended up rolling my own stupid grep/sed/perl/etc. pipeline in cases like this, but xmlstarlet is perfect for this job. E.g.,

$ cat test-runner.xml \
    | xml sel -t -m "//testcase" -v @classname -o " " -v @name -nl

yields (for one of my projects):

Chrome 27.0 (Linux).Globalization API: _g11n exists
Chrome 27.0 (Linux).Globalization API: _g11n has been initialized
Chrome 27.0 (Linux).Globalization API: _g11n _locales exists
Chrome 27.0 (Linux).Globalization API: _g11n _locales has been initialized
Chrome 27.0 (Linux).Globalization API: _g11n _locales has a current locale (matching the default)
Chrome 27.0 (Linux).Globalization API: _g11n _locales registry allows lookup by full code
Chrome 27.0 (Linux).Globalization API: _g11n _locales registry allows lookup by locale object
Chrome 27.0 (Linux).Globalization API: _g11n _locales registry fails
Chrome 27.0 (Linux).Globalization controllers hkmLocaleCtrl should have the locales database

Option 2: Write a Custom Reporter

If you're up for it, subclass an existing reporter (e.g., lib/reporters/Progress.js, lib/reporters/Base.js), overriding the .specSuccess and .onBrowserComplete methods to report more details from each test. Getting karma to use the reporter is left as an exercise for the reader :-)

BTW: If you choose option 2, be sure to open a pull-request to get it into karma :-)

Community
  • 1
  • 1
Tripp Lilley
  • 1,653
  • 16
  • 21
  • 1
    Thanks for the great answer! I think I will try to go for option 2. I have karma installed with node at './.npm/karma/0.8.5/package/lib', I tried to modify that file (by deleting all basically to see if it fails), but no change is reflected in karma. Is there another place I should look for? – Tomas Romero May 31 '13 at 22:18
  • That's the "pristine" cached copy of the package. Use `npm prefix` and `npm prefix -g` to find the local and global package installs, respectively. Alternatively, `mkdir foo ; cd foo ; npm install karma` then edit the copy in `foo/node_modules/karma`. This is a hackish way of doing it, but I haven't done enough NPM development to know the "right" way, yet. I know it starts with `git clone https://github.com/karma-runner/karma` :-), and, for karma, continues with notes here: https://github.com/karma-runner/karma/blob/master/CONTRIBUTING.md – Tripp Lilley Jun 03 '13 at 12:36
  • 1
    I vote for option 2, with Karma 0.9+ (currently in canary), you can easily add custom plugins (eg. reporters/launchers/adapters/etc). So write a new reporter, you can get inspiration in https://github.com/karma-runner/karma-junit-reporter – Vojta Jun 12 '13 at 23:36
  • Would love this also, but writing a plug in is above my ability, so sorry I can't take it on. – Ross R Jul 16 '13 at 16:44
2

To show the individual test case I use the following (starting from scratch for a basic unit test project):

npm install karma karma-jasmine karma-phantomjs-launcher karma-spec-reporter

touch main.js main.spec.js

karma init

then to the following questions select:

Which testing framework do you want to use ?
> jasmine

Do you want to capture any browsers automatically ?
> PhantomJS
> 

What is the location of your source and test files ?
> *.js

Do you want Karma to watch all the files and run the tests on change ?
> yes

edit the karma.conf.js in your project folder and

replace:

reporters: ['progress']

with:

reporters: ['spec']

run

karma start

and you are ready to write your unit test in the main.spec.js

describe('suite', function () {
    it('expectation', function () {
        expect(true).toBeTruthy();
    });
});

save... and in the terminal you should see something like:

INFO [watcher]: Changed file"/main.spec.js".
true
  ✓ should be true

PhantomJS 1.9.8 (Mac OS X): Executed 1 of 1 SUCCESS (0.001 secs / 0 secs)
Daniele Urania
  • 2,658
  • 1
  • 15
  • 11
  • this is the perfect answer to my https://stackoverflow.com/questions/44462961/display-jasmine-spec-results-in-console question – Igor L. Jun 09 '17 at 17:15
0

There's karma-coverage which creates .html code coverage reports. It's straight-forward to integrate it into your karma config.

https://github.com/karma-runner/karma-coverage

npm install karma-coverage --save-dev

add to karma.conf.js:

// karma.conf.js
module.exports = function(config) {
  config.set({
    files: [
      'src/**/*.js',
      'test/**/*.js'
    ],

    // coverage reporter generates the coverage
    reporters: ['progress', 'coverage'],

    preprocessors: {
      // source files, that you wanna generate coverage for
      // do not include tests or libraries
      // (these files will be instrumented by Istanbul)
      'src/*.js': ['coverage']
    },

    // optionally, configure the reporter
    coverageReporter: {
      type : 'html',
      dir : 'coverage/'
    }
  });
};
grant
  • 4,325
  • 2
  • 24
  • 20
  • I am not the one who downvoted you, but I think you misunderstood the question. Coverage reports only show code coverage for each file, not each individual test cases. – janetsmith Jun 26 '14 at 00:47