Output jasmine test results to the console

时光怂恿深爱的人放手 提交于 2019-11-28 17:47:32

Have you tried the ConsoleRepoter

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.

Pawel Miech

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

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());

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!