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.
Also, the Equivalent of Println in JavaScript is document. writeln() for the closest, but it is not advisable in all cases due to its varied limitations.
Description. The println() function writes to the console area, the black rectangle at the bottom of the Processing environment. This function is often helpful for looking at the data a program is producing. Each call to this function creates a new line of output.
The console. log() method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects. Note: This feature is available in Web Workers.
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
I found a solution:
print("My message here");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With