Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display total number of tests/expectations run by Jasmine

I'm converting a large set of QUnit tests to Jasmine. In QUnit, I'm used to seeing the total number of tests across all test modules, displayed at the top. E.g.:

Tests completed in 157 milliseconds.

528 tests of 528 passed, 0 failed.

I consider the number of tests to be important information. However, Jasmine's example test runners do not display the total number of tests. Rather, you get something like:

Passing 106 specs

Each of those specs may contain any number of individual tests. Is it possible to determine the total number of tests that have run, so that I can display it in my test runner? I've looked for information online and in the Jasmine docs but so far haven't been able to find anything.


Solution

Based on @ggozad's reply, I've come up with the following solution, which prints to the console. Suggestions for how to improve it or how to cleanly add the results to Jasmine's HTML output are welcome.

var jasmineEnv = jasmine.getEnv();
var htmlReporter = new jasmine.HtmlReporter();
var reportRunnerResults = htmlReporter.reportRunnerResults;

htmlReporter.reportRunnerResults = function(runner) {
    reportRunnerResults(runner);

    var specs = runner.specs();
    var specResults;
    var assertionCount = {total: 0, passed: 0, failed: 0};

    for (var i = 0; i < specs.length; ++i) {
        if (this.specFilter(specs[i])) {
            specResults = specs[i].results();
            assertionCount.total += specResults.totalCount;
            assertionCount.passed += specResults.passedCount;
            assertionCount.failed += specResults.failedCount;
        }
    }

    if (console && console.log) {
        console.log('Total: ' + assertionCount.total);
        console.log('Passed: ' + assertionCount.passed);
        console.log('Failed: ' + assertionCount.failed);
    }
};

jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function(spec) {
    return htmlReporter.specFilter(spec);
};

window.onload = function() {
    jasmineEnv.execute();
};

Example console output:

Total: 67
Passed: 67
Failed: 0 
like image 235
slevithan Avatar asked Oct 08 '22 05:10

slevithan


1 Answers

A spec is a test in Jasmine. Inside it you can have expectations similar to assertions in other testing frameworks. So the number of specs you see reported are the total of each it calls:

it('passes some expectations', function () {
  ...
});

Typically you would group several unit-like tests together in an it, which should help you group functionality together and present a more coherent view of how your application is developing.

Now, if you insist on wanting to know about the expectations that failed/succeeded in your spec, you can always get this information from your reporter. For instance if you setup up an instance of an htmlReporter, you could do:

htmlReporter.reportRunnerResults = function (runner) {
...
};

Inside your function you can check all sorts of things, here are some hints:

  • runner.specs() gives you all the specs
  • for each of those say spec, results = spec.results() would give you info about your expectations.
  • results.totalCount, results.failedCount, results.passedCount is what you are seeking ;)
like image 55
ggozad Avatar answered Oct 13 '22 10:10

ggozad