Redirect calls to console.log() to standard output in Jasmine tests

有些话、适合烂在心里 提交于 2019-12-02 19:55:19

Try

console.info('foo')

From the test javascripts.

You can use:

jasmine.log("I've got a big log.");

NB: See douglas-treadwell's comment below.

This refers to Jasmine 1.x. With Jasmine 2.0 just use console.log directly.

If you are running in a node env. You could use

process.stdout.write("this will be send to the console");

I'm using jasmine 2 via guard and phantom js and have found that standard console.log messages within tests are output to the jasmine spec runner's console just fine.

I've also found that console.log messages within the javascript code elements that I am testing do get written out to stdout but not console.log messages within the tests themselves.

Martin Borthiry

I think it is not possible.

I had to overwrite the console.log implementation in the spec loader. i.e (using jQuery):

var console = {
    panel: $('body').append('<div>').css({position:'fixed', top:0, right:0,background:'transparent'}),
    log: function(m){
        this.panel.prepend('<div>'+m+'</div>');
    }       

};
        console.log('message 1');
        console.log('message 2');

here your have a functional example

If you're desperate for any output in a Terminal window so you can have data to review after the test has completed and the browser has closed, console.error() seems to do the trick.

Skip Jack

Ran into the same issue using grunt/karma/jasmine (karma-jasmine 0.2.2) -

To go along with what Dave Sag said, I've found that all my console.log messages from the code I'm testing run fine, however nothing from my describe() {} and it() {} blocks log anything.

I did find that you can log from a beforeEach() {} block. At least it worked for me :

beforeEach(function() {
  this.model = new blahModel();
  console.log('this.model = ', this.model);
});

Note that this only logs in the browser console and for some reason won't log in command line. Somewhat strange when the console.log statements from the tested code block do log in the command line. I've also found what seems to be a better approach for consistent logging here.

UPDATE : I'm actually seeing the logging working for it blocks as well, I believe I had other errors that were preventing this.

1) Go to your project directory where you have your pom.xml. Run the following command in cmd. mvn jasmine:bdd

2) You will get the localhost URL : localhost:8234 (just an example).

3) Run this URL in the browser. Now all your test cases gets executed.

4) Do the Inspect element of this page. In the browser console you will be able to see all the console.log() or console.error() traces.

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