20

I am writing some tests for Javascript code and I need to dump some messages during the compile process when errors are encountered.

Is there any equivalent to Java's System.out.println() in Javascript?

P.S.: I also need to dump debug statements while implementing tests.

UPDATE

I am using a maven plugin on a file containing all merged tests:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                <phase>test</phase>
                <goals>
                    <goal>java</goal>
                </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>org.mozilla.javascript.tools.shell.Main</mainClass>
                <arguments>
                    <argument>-opt</argument>
                    <argument>-1</argument>
                    <argument>${basedir}/src/main/webapp/html/js/test/test.js</argument>
                </arguments>
            </configuration>
        </plugin>

UPDATE II

I tried console.log("..."), but I get:

js: "src/main/webapp/html/js/concat/tests_all.js", line 147:
uncaught JavaScript runtime exception: ReferenceError: "console" is not defined

The code I am testing is a set of functions (like in a library). I am using QUnit.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453

8 Answers8

30

Essentially console.log("Put a message here.") if the browser has a supporting console.

Another typical debugging method is using alerts, alert("Put a message here.")

RE: Update II

This seems to make sense, you are trying to automate QUnit tests, from what I have read on QUnit this is an in-browser unit testing suite/library. QUnit expects to run in a browser and therefore expects the browser to recognize all of the JavaScript functions you are calling.

Based on your Maven configuration it appears you are using Rhino to execute your Javascript at the command line/terminal. This is not going to work for testing browser specifics, you would likely need to look into Selenium for this. If you do not care about testing your JavaScript in a browser but are only testing JavaScript at a command line level (for reason I would not be familiar with) it appears that Rhino recognizes a print() method for evaluating expressions and printing them out. Checkout this documentation.

These links might be of interest to you.

QUnit and Automated Testing

JavaScript Unit Tests with QUnit

Chris Wagner
  • 20,773
  • 8
  • 74
  • 95
  • 2
    If the browser doesn't have a `console` then it's a crappy browser (for debugging). – gen_Eric Jan 04 '12 at 20:01
  • 3
    @Rocket I would agree, hopefully most of those no longer exist but it was definitely a problem before. Furthermore those crappy browsers were the ones you had to do the most debugging on! – Chris Wagner Jan 04 '12 at 20:03
  • 2
    I know IE wouldn't have a `console` object unless you opened the debugging tools, then it would add it. But that's because IE is stupid. – gen_Eric Jan 04 '12 at 20:04
  • Note only ie < 8 dont have console.log, the developer tools in ie8 have console.log as well. – Andreas Köberle Jan 04 '12 at 20:06
  • @Rocket Which mobile browser has a console and how do I read it? – Voo Jan 04 '12 at 20:19
  • @Voo: Mobile browser? Is this iOS or Android (or something else)? I know on Android, if you use the built-in browser `console.log` messages are sent to the system log, which can be read with [`logcat`](http://developer.android.com/guide/developing/tools/logcat.html) (or [`adb`](http://developer.android.com/guide/developing/tools/adb.html#logcat)) or with a app from the market, such as: [CatLog](https://market.android.com/details?id=com.nolanlawson.logcat&hl=en). – gen_Eric Jan 04 '12 at 20:23
  • I tried the proposed solution, but it did not work. I have updated my question. – Jérôme Verstrynge Jan 04 '12 at 20:23
  • @Voo on iOS... Settings > Safari > Advanced > Debug Console – Chris Wagner Jan 04 '12 at 20:36
  • @JVerstry check out my update, I hope that helps point you in the right direction. – Chris Wagner Jan 04 '12 at 20:52
  • Indeed, I do use Rhino. No worry about browsers specifics, the methods I implement don't rely on browsers' specific, they just convert / tranform data. I am coming to Javascript with my Java JUnit mentality... – Jérôme Verstrynge Jan 04 '12 at 21:19
  • If you mean 'calling without a parameter', then the answer is yes. It prints an empty line. – Jérôme Verstrynge Jan 04 '12 at 21:37
  • @Rock Good to know thanks - had to debug only once some mobile specific problem and found it easier to just pepper the code with some alerts - but good to know that we can log it. – Voo Jan 04 '12 at 22:17
  • @JVerstry Ok, so how about printing what you wanted to print? `print("Hello world")`? Does this answer your question? – Chris Wagner Jan 04 '12 at 22:41
  • Well, yes, that's what I found out by myself yesterday. See the solution I created. – Jérôme Verstrynge Jan 05 '12 at 22:03
  • Hmm, ok I suppose I didn't see you posted a solution, please point that out in the comments next time. – Chris Wagner Jan 05 '12 at 22:46
4

I'm using Chrome and print() literally prints the text on paper. This is what works for me:

document.write("My message");
George
  • 6,006
  • 6
  • 48
  • 68
4

I found a solution:

print("My message here");
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
2

console.log().

Chrome, Safari, and IE 8+ come with built-in consoles (as part of a larger set of development tools). If you're using Firefox, getfirebug.com.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

In java System.out.println() prints something to console. In javascript same can be achieved using console.log().

You need to view browser console by pressing F12 key which opens developer tool and then switch to console tab.

nircraft
  • 8,242
  • 5
  • 30
  • 46
Erikas
  • 1,006
  • 11
  • 22
0

There isn't one, at least, not unless you are using a "developer" tool of some kind in your browser, e.g. Firebug in Firefox or the Developer tools in Safari. Then you can usually use console.log.

If I'm doing something in, say, an iOS device, I might add a <div id="debug" /> and then log to it.

Matt H
  • 6,422
  • 2
  • 28
  • 32
0

You can always simply add an alert() prompt anywhere in a function. Especially useful for knowing if a function was called, if a function completed or where a function fails.

alert('start of function x');
alert('end of function y');
alert('about to call function a');
alert('returned from function b');

You get the idea.

jamesTheProgrammer
  • 1,747
  • 4
  • 22
  • 34
  • Yes, it could work, but when running the compile process multiple times, I would have to close each alert -> A bit impractical for me... – Jérôme Verstrynge Jan 04 '12 at 20:22
  • right - I can see where that would be a little impractical. I used FireBug a bit, seems it has what you are looking for http://getfirebug.com/logging – jamesTheProgrammer Jan 04 '12 at 20:28
0

I'm also about to ask the same question. But from what I've learned from codeacademy.com below code is enough to display the output or text?

print("hello world")  
Iqbal
  • 246
  • 2
  • 10