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

JavascriptJasmineJasmine Maven-Plugin

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

Try

console.info('foo')

From the test javascripts.

Solution 2 - Javascript

Jasmine 1.x

You can use:

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

Jasmine 2+

Use console.log directly, as per douglas-treadwell's comment below.

Solution 3 - Javascript

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

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

Solution 4 - Javascript

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

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.

Solution 7 - Javascript

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.

Solution 8 - Javascript

  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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionL&#243;r&#225;nt Pint&#233;rView Question on Stackoverflow
Solution 1 - Javascriptuser1338062View Answer on Stackoverflow
Solution 2 - JavascriptBanksySanView Answer on Stackoverflow
Solution 3 - JavascriptGeoffrey SamperView Answer on Stackoverflow
Solution 4 - JavascriptMartin BorthiryView Answer on Stackoverflow
Solution 5 - JavascriptProfessor TomView Answer on Stackoverflow
Solution 6 - JavascriptDave SagView Answer on Stackoverflow
Solution 7 - JavascriptGreg VenechView Answer on Stackoverflow
Solution 8 - JavascriptJegan GopalakrishnanView Answer on Stackoverflow