73

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.

Lóránt Pintér
  • 10,152
  • 14
  • 47
  • 53
  • 1
    See also: http://stackoverflow.com/questions/14990335/logging-from-grunt-contrib-jasmine – Jess Sep 18 '13 at 14:25

8 Answers8

62

Try

console.info('foo')

From the test javascripts.

user1338062
  • 11,939
  • 3
  • 73
  • 67
  • 8
    Why .info rather than .log? – Anonymous Feb 02 '16 at 01:24
  • 2
    Oddly, with Jasmine 2.5, I had to do this instead of console.log. I have verbose on, but console.log does not show up (it shows log line + empty string), whereas console.info actually shows the log line + whatever I'm logging. – Ezekiel Victor Dec 06 '16 at 00:34
  • @user1338062 - so therefore your feedback from the downvotes is that you should have included the information that ezekial victor provided, if it had you may not have gotten the downvotes. – shawty Oct 04 '19 at 15:14
42

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.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
BanksySan
  • 27,362
  • 33
  • 117
  • 216
  • 8
    That does not work for me; I don't see the output of `jasmine.log` anywhere in my terminal when running `mvn jasmine:test` (using plugin version 1.3.1.3, htmlunit 2.13) – Pavel Lishin Oct 30 '13 at 18:12
  • 2
    @PavelLishin, it doesn't print to your console window, but it does print to the web runner. – BanksySan Oct 31 '13 at 13:30
  • 12
    This refers to Jasmine 1.x. With Jasmine 2.0 just use console.log directly. – Anonymous Feb 02 '16 at 01:23
13

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

process.stdout.write("this will be send to the console");
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Geoffrey Samper
  • 497
  • 4
  • 14
4

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

alex
  • 479,566
  • 201
  • 878
  • 984
Martin Borthiry
  • 5,256
  • 10
  • 42
  • 59
4

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.

Professor Tom
  • 352
  • 3
  • 16
3

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.

Dave Sag
  • 13,266
  • 14
  • 86
  • 134
  • console.log should work. If you don't see your console.logs in the output, try adding more at earlier places in the call stack. Most likely you have an error earlier that is preventing execution from reaching your console.log. – Anonymous Feb 02 '16 at 01:25
  • 1
    `console.error()` seems to work per my answer [here](http://stackoverflow.com/a/41864171/916586). – Professor Tom Jan 26 '17 at 00:31
3

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.

Community
  • 1
  • 1
Greg Venech
  • 8,062
  • 2
  • 19
  • 29
2

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.