41


I am using Jasmine (BDD Testing Framework for JavaScript) in my firefox add-on to test the functionality of my code.

The problem is that jasmine is outputing the test results to an HTML file,what I need is to Firebug Console or other solution to output the results.

Yosi
  • 2,936
  • 7
  • 39
  • 64

5 Answers5

29

Have you tried the ConsoleReporter?

jasmine.getEnv().addReporter(new jasmine.ConsoleReporter(console.log));

According to the code Jasmine has the ConsoleReporter class that executes a print function (in this case console.log) that should do what you need.

If all else fails you could just use this as a starting point to implement your own console.log reporter.

UPDATE In newer versions of jasmine, ConsoleReporter was removed. You can either use the built-in jsApiReporter, or write your own (console) reporter, as shown in the following link: https://jasmine.github.io/tutorials/custom_reporter

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Tigraine
  • 23,358
  • 11
  • 65
  • 110
  • Have you implemented Jasmine for iPhone??? if yes, does the ConsoleReporter works for getting output in Console of iPhone?? – DShah Jan 10 '12 at 12:34
  • 2
    It works, but watch out which version of Jasmine you're using. Provided ConsoleReporter github link points to master branch version of ConsoleReporter.js, which use different reporter-API as current (1.3.1) official version of Jasmine (e.g. jasmineStarted or jasmineDone instead of reportRunnerStarting etc). If it does not work for you, you probably need to select v1.3.1 tag (or use [this](https://github.com/pivotal/jasmine/blob/v1.3.1/src/console/ConsoleReporter.js) link). – Mateusz Szulc Aug 21 '13 at 18:09
15

In newest version of Jasmine (2.0) if you want to get test output to console you need to add following lines.

var ConsoleReporter = jasmineRequire.ConsoleReporter();
var options = {
   timer: new jasmine.Timer, 
   print: function () {
      console.log.apply(console,arguments)
}};
consoleReporter = new ConsoleReporter(options); // initialize ConsoleReporter
jasmine.getEnv().addReporter(consoleReporter); //add reporter to execution environment

Output to html is included by default however so if you don't want html output at all you have to edit your boot.js file and remove relevant lines from there. If you want to customize how output is displayed in console edit file console.js. Source

Carl Emmoth
  • 473
  • 4
  • 11
Pawel Miech
  • 7,742
  • 4
  • 36
  • 57
6

jasmineRequire.ConsoleReporter did not exist in 2.3.0 so I used the following code:

//create a console.log reporter
var MyReporter = function(){jasmineRequire.JsApiReporter.apply(this,arguments);};
MyReporter.prototype = jasmineRequire.JsApiReporter.prototype;
MyReporter.prototype.constructor = MyReporter;
MyReporter.prototype.specDone=function(o){
    o=o||{};
    if(o.status!=="passed"){
      console.warn("Failed:" + o.fullName + o.failedExpectations[0].message);
    }
};
var env = jasmine.getEnv();
env.addReporter(new MyReporter());
HMR
  • 37,593
  • 24
  • 91
  • 160
  • Worked for me under Jasmine 2.8.0 :) I added this to `boot.js` just after creation of the `jsApiReporter`. I removed the `var env=...` line since `env` already existed. Would you be willing to add a bit of explanation to help me better understand the interactions between `JsApiReporter` and `MyReporter`? – cxw Oct 07 '17 at 14:14
6

For the sake of completeness here's the full configuration:

First of all run the npm install command:

npm install jasmine-console-reporter --save-dev

Then check your Jasmine configuration to make sure you got the helpers setting there:

spec/support/jasmine.json

{
    "spec_dir": "spec",
    "spec_files": [
        "**/*[sS]pec.js"
    ],
    "helpers": [
        "helpers/**/*.js"
    ],
    "stopSpecOnExpectationFailure": false,
    "random": false
}

Since helpers are executed before specs the only thing you have to do is to create a console reporter helper.

spec/helpers/reporter/consoleReporter.js

const JasmineConsoleReporter = require('jasmine-console-reporter');

let consoleReporter = new JasmineConsoleReporter({
    colors: 1,           // (0|false)|(1|true)|2
    cleanStack: 1,       // (0|false)|(1|true)|2|3
    verbosity: 4,        // (0|false)|1|2|(3|true)|4
    listStyle: 'indent', // "flat"|"indent"
    activity: false
});

jasmine.getEnv().addReporter(consoleReporter);
Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
  • 1
    That is ridiculous how complicated it is with `jasmine`. Why not just to add `"color": "true"` option in `jasmine.json`? Like in `mocha` just type `--color` in config file – Green Jun 20 '17 at 16:28
0

I create summary solution of above answers, tested on different jasmine versions. Add this to your boot.js (e.g. to boot1.js):

      const env = jasmine.getEnv();
      const jasmineRequire = window.jasmineRequire || require('./jasmine.js');
    
      var ConsoleReporter = window.ConsoleReporter;
      var options = null;
      if (!jasmine.ConsoleReporter || !!jasmineRequire.ConsoleReporter) {  
          if (!jasmineRequire.ConsoleReporter) {
                ConsoleReporter = function(){jasmineRequire.JsApiReporter.apply(this,arguments);};
                ConsoleReporter.prototype = jasmineRequire.JsApiReporter.prototype;
                ConsoleReporter.prototype.constructor = ConsoleReporter;
                ConsoleReporter.prototype.specDone = function (o) {
                    o = o || {};
                    if (o.status !== "passed") {
                      console.warn("Failed: " + o.fullName + o.failedExpectations[0].message);
                    } else {
                      console.debug("Passed: " + o.fullName);
                    }
                };
          } else {  
              ConsoleReporter = jasmineRequire.ConsoleReporter();
              options = {
                  timer: new jasmine.Timer, 
                  print: function () {
                      console.log.apply(console,arguments)
                  }
              };
          }
      } else {
          ConsoleReporter = jasmine.ConsoleReporter;
          options = console.log;
      }
      window.ConsoleReporter = ConsoleReporter;
      
      consoleReporter = new ConsoleReporter(options); // initialize ConsoleReporter
      env.addReporter(consoleReporter); // add reporter to execution environment

Monday
  • 41
  • 5