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

蓝咒 提交于 2019-12-31 10:06:43

问题


I'm using Jasmine via the jasmine-maven-plugin, and I would like to see console.log() messages in the Maven build output. Is there a way to achieve this?

If console.log() cannot be redirected, is there any other way to log from my tests so that they show up on the Maven build output?

I'm running these tests on Jenkins in a headless fashion, and would like a means to get some debug output from the tests.


回答1:


Try

console.info('foo')

From the test javascripts.




回答2:


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.




回答3:


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

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




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/11049996/redirect-calls-to-console-log-to-standard-output-in-jasmine-tests

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