<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>qunit-cli &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/qunit-cli/</link>
	<description>Feed of posts on WordPress.com tagged "qunit-cli"</description>
	<pubDate>Fri, 24 May 2013 16:04:30 +0000</pubDate>

	<generator>http://en.wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[Java 6 Scripting is no js.jar]]></title>
<link>http://twoguysarguing.wordpress.com/2010/11/28/java-6-scripting-is-no-js-jar/</link>
<pubDate>Mon, 29 Nov 2010 01:34:38 +0000</pubDate>
<dc:creator>benjaminplee</dc:creator>
<guid>http://twoguysarguing.wordpress.com/2010/11/28/java-6-scripting-is-no-js-jar/</guid>
<description><![CDATA[&nbsp; QUnit-CLI riding a Dog Today I wanted to put together a QUnit-CLI example that leveraged Java]]></description>
<content:encoded><![CDATA[<p style="text-align:left;">&#160;</p>
<div id="attachment_1232" class="wp-caption aligncenter" style="width: 190px"><a href="http://www.flickr.com/photos/joeltelling/69492277/"><img class="size-medium wp-image-1232  " title="Monkey Riding Dog" src="http://twoguysarguing.files.wordpress.com/2010/11/69492277_ec392e62fc_d.jpg?w=180&#038;h=155" alt="Monkey Riding Dog" width="180" height="155" /></a><p class="wp-caption-text">QUnit-CLI riding a Dog</p></div>
<p>Today I wanted to put together a <em><strong><a href="https://github.com/benjaminplee/QUnit-CLI">QUnit-CLI</a></strong></em> example that leveraged Java 6&#8242;s included scripting features.  Seeing as the Java 6 JDK includes a recent version of Rhino as its primary JavaScript engine, I thought this would be a piece of cake.  Wrong.</p>
<p>&#160;</p>
<p style="text-align:left;">To the javax.script package&#8217;s credit, Creating a new scripting engine and evaluating some script code is dead simple.  Example below from <a href="http://download.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html#evalfile">Oracle&#8217;s own pages</a>&#8230;</p>
<pre class="brush: java; title: ; notranslate" title="">
import javax.script.*;

public class EvalFile {
  public static void main(String[] args) throws Exception {
    // create a script engine manager
    ScriptEngineManager factory = new ScriptEngineManager();

    // create JavaScript engine
    ScriptEngine engine = factory.getEngineByName(&#34;JavaScript&#34;);

    // evaluate JavaScript code from given file - specified by first argument
    engine.eval(new java.io.FileReader(args[0]));
  }
}
</pre>
<p>The trouble came into play when I ran QUnit-CLI &#8216;s Rhino based suite.js file.   Ka-blew-ey!</p>
<pre>Exception in thread "main" javax.script.ScriptException:
    sun.org.mozilla.javascript.internal.EcmaError: 
    ReferenceError: "load" is not defined. (#1) in  at line number 1
  at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
  at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
  at Java6RhinoRunner.load(Java6RhinoRunner.java:29)
  at Java6RhinoRunner.main(Java6RhinoRunner.java:14)</pre>
<p>It turns out that scripts running from in a <a href="https://developer.mozilla.org/en/Rhino_Shell">Rhino Shell</a> environment have access to extra functions that are not directly provided when executing embedded from Java.  The top-level &#8220;load&#8221; function which is used to load additional JavaScript files from the Rhino Shell is not directly available when running from the scripting engine.</p>
<p>It turns out, I am not the <a href="http://groups.google.com/group/envjs/browse_thread/thread/a96e79f264bd196c/6e453224d2a4b08a?#6e453224d2a4b08a">only</a> <a href="http://stackoverflow.com/questions/3539624">one</a> with this problem.  My solution was to bind an additional object with a Java-based &#8220;load&#8221; function and add a new top-level &#8220;load&#8221; function to the JavaScript scope that invokes the Java code.</p>
<pre class="brush: java; title: ; notranslate" title="">
import javax.script.*;
import java.io.*;

public class Java6RhinoRunner {
  public static void main(String[] args) throws ScriptException {
    new Java6RhinoRunner().load(args[0]);
  }

  private final ScriptEngine engine;

  public Java6RhinoRunner() throws ScriptException {
    ScriptEngineManager factory = new ScriptEngineManager();
    this.engine = factory.getEngineByName(&#34;JavaScript&#34;);

    this.engine.put(&#34;Java6RhinoRunner&#34;, this);
    this.engine.eval(&#34;function load(filename) { Java6RhinoRunner.load(filename); }&#34;);
  }

  public void load(String filename) throws ScriptException {
    try {
      this.engine.eval(new FileReader(filename));
    }
    catch(FileNotFoundException e) {
      throw new RuntimeException(&#34;Error loading javascript file: &#34; + filename, e);
    }
  }
}
</pre>
<p>To make matters worse, from the Rhino Shell, the &#8220;print&#8221; function will output the given text to standard output with a newline character.  As far as I can tell there isn&#8217;t a non-newline print command available on the Shell.  Annoyingly, when running from Java both print and println are available and tied to their common java behaviors.  This means that my suite.js code which uses &#8220;print&#8221; needs to use &#8220;println&#8221; when running from Java.  My first thought was to override print to execute println from my Java runner, but it looks like these basic top-level functions can&#8217; be redefined from JavaScript.</p>
<pre class="brush: java; gutter: false; title: ; notranslate" title="">
this.engine.eval(&#34;function print(message) { println(message); }&#34;);
</pre>
<p><em><span style="color:#ff0000;">(no errors at runtime, but didn&#8217;t work)</span></em></p>
<p>The solution was to use add a level of indirection to my suite.js</p>
<pre class="brush: jscript; gutter: false; title: ; notranslate" title="">
var out = (typeof println !== &#34;undefined&#34;) ? println : print;
out(&#34;FAIL - &#34; + name); // used to be a call to print directly
</pre>
<p>Now we have suit.js running from Rhino embedded within the Java 6 JDK &#8230; but things are perfect.  The current code throws &#8220;Inappropriate array length&#8221; errors for a few QUnit tests.  I will be looking into these next.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[QUnit-CLI: Running QUnit with Rhino]]></title>
<link>http://twoguysarguing.wordpress.com/2010/11/26/qunit-cli-running-qunit-with-rhino/</link>
<pubDate>Sat, 27 Nov 2010 04:52:59 +0000</pubDate>
<dc:creator>benjaminplee</dc:creator>
<guid>http://twoguysarguing.wordpress.com/2010/11/26/qunit-cli-running-qunit-with-rhino/</guid>
<description><![CDATA[Previously I talked about wanting to run QUnit outside the browser and about some issues I ran into.]]></description>
<content:encoded><![CDATA[<p><span style="color:#0000ff;"><a href="http://commons.wikimedia.org/wiki/File:Indian_Rhino_001.jpg"><img class="alignright" title="Rhino" src="http://upload.wikimedia.org/wikipedia/commons/6/61/Indian_Rhino_001.jpg" alt="Rhino - wikimedia" width="300" height="213" /></a>Previously I talked about wanting to run QUnit <a href="http://twoguysarguing.wordpress.com/2010/11/02/make-javascript-tests-part-of-your-build-qunit-rhino/">outside the browser</a> and about some <a href="http://twoguysarguing.wordpress.com/2010/11/06/qunit-and-the-command-line-one-step-closer/">issues</a> I ran into.  Finally, I have QUnit running from the command line: <em><strong><a href="https://github.com/benjaminplee/QUnit-CLI">QUnit-CLI</a></strong></em></span></p>
<p>After a good deal of hacking and a <a href="http://twitter.com/#!/bassistance/status/4594097226514432">push from jzaefferer</a>, I gotten the example code in QUnit-CLI to run using Rhino and no browser in sight.  This isn&#8217;t a complete substitute for in-browser testing, but makes integration with build servers and faster feedback possible.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate" title="">
/projects/qunit-cli/js suite.js
PASS - Adding numbers
FAIL - Subtracting numbers
PASS&#124;OK&#124;subtraction function exists&#124;
FAIL&#124;EQ&#124;Intended bug!!!&#124;Expected: 0, Actual: -2
PASS - module without setup/teardown (default)
PASS - expect in testPASS - expect in test
PASS - module with setup
PASS - module with setup/teardown
PASS - module without setup/teardown
PASS - scope check
PASS - scope check
PASS - modify testEnvironment
PASS - testEnvironment reset for next test
PASS - scope check
PASS - modify testEnvironment
PASS - testEnvironment reset for next test
PASS - makeurl working
PASS - makeurl working with settings from testEnvironment
PASS - each test can extend the module testEnvironment
PASS - jsDump output
PASS - raises
PASS - mod2
PASS - reset runs assertions
PASS - reset runs assertions2
----------------------------------------
PASS: 22  FAIL: 1  TOTAL: 23
Finished in 0.161 seconds.
----------------------------------------
</pre>
<p>The first hurdle was adding guards around all QUnit.js&#8217;s references to setTimeout, setInterval, and other browser/document specific objects.  In addition I extended the test.js browser checks to include all of the asynchronous tests and fixture tests.  Finally I cleaned up a bit of the jsDump code to work better with varying object call chains.  My alterations can be found on my <a href="https://github.com/asynchrony/qunit/tree/consistent-checks">fork here</a>.</p>
<p>The second hurdle was getting QUnit-CLI using my modified version of QUnit.js and adjusting how Rhino errors are handled.  Adding a QUnit submodule to the QUnit-CLI git repository easily fixed the first (<span style="color:#808080;"><em>I previously posted my notes on <a href="http://twoguysarguing.wordpress.com/2010/11/14/tie-git-submodules-to-a-particular-commit-or-branch/">git submodules and fixed branches</a></em></span>).  QUnit.js&#8217;s borrowed jsDump code is used to &#8220;pretty-print&#8221; objects in test messages.  jzaefferer ran into an <strong><a href="https://github.com/jquery/qunit/issues/#issue/56">issue</a></strong> when running QUnit&#8217;s own tests through QUnit-CLI resulting in the cryptic error:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate" title="">
js: &#34;../qunit/qunit.js&#34;, line 1021: Java class &#34;[B&#34; has no public instance field or method named &#34;setInterval&#34;.
at ../qunit/qunit.js:1021
at ../qunit/qunit.js:1002
at ../qunit/qunit.js:1085
at ../qunit/qunit.js:1085
at ../qunit/qunit.js:1085
at ../qunit/qunit.js:110
at ../qunit/qunit.js:712 (process)
at ../qunit/qunit.js:304
at suite.js:84
</pre>
<p>It turns out that errors objects (e.g. ReferenceError) throw in Rhino include an additional property of rhinoException which points to the underlying Java exception that was actually thrown.  The error we saw is generated when the jsDump code walks the error object tree down to a byte array off of the exception.  Property requests executed against this byte array throw the Java error above, even if they are done part of a typeof check, e.g.</p>
<pre class="brush: jscript; light: true; title: ; notranslate" title="">
var property_exists = (typeof obj.property !== 'undefined');
</pre>
<p>Once I figured this out, I wrapped the object parser inside QUnit.jsDump to properly pretty-print error objects and delegate to the original code for any other type of object.</p>
<pre class="brush: jscript; gutter: false; title: ; notranslate" title="">
...
var current_object_parser = QUnit.jsDump.parsers.object;
QUnit.jsDump.setParser('object', function(obj) {
  if(typeof obj.rhinoException !== 'undefined') {
    return obj.name + &#34; { message: '&#34; + obj.message + &#34;', fileName: '&#34; + obj.fileName + &#34;', lineNumber: &#34; + obj.lineNumber + &#34; }&#34;;
  }
  else {
    return current_object_parser(obj);
  }
});
...
</pre>
<p>With these changes we have a decent command line executable test suite runner for QUnit.  With a bit more work QUnit-CLI will hopefully be able to print Ant/JUnit style XML output and/or include stack traces when errors bubble out of test code.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[QUnit and the Command Line: One Step Closer,]]></title>
<link>http://twoguysarguing.wordpress.com/2010/11/06/qunit-and-the-command-line-one-step-closer/</link>
<pubDate>Sun, 07 Nov 2010 04:57:07 +0000</pubDate>
<dc:creator>benjaminplee</dc:creator>
<guid>http://twoguysarguing.wordpress.com/2010/11/06/qunit-and-the-command-line-one-step-closer/</guid>
<description><![CDATA[Previously I talked about getting QUnit JavaScript tests running on the command line using a simple]]></description>
<content:encoded><![CDATA[<p><a href="http://twoguysarguing.files.wordpress.com/2010/11/2206974242_fa5aa8e08e_m.jpg"><img class="alignleft size-full wp-image-999" style="margin-left:10px;margin-right:10px;" title="One Step Closer" src="http://twoguysarguing.files.wordpress.com/2010/11/2206974242_fa5aa8e08e_m.jpg?w=144&#038;h=96" alt="" width="144" height="96" /></a><strong><a href="http://twoguysarguing.wordpress.com/2010/11/02/make-javascript-tests-part-of-your-build-qunit-rhino/">Previously</a></strong> I talked about getting QUnit JavaScript tests running on the command line using a simple Rhino setup.  Hacking a few lines together meant that we could run our tests outside the constraints of a browser and potentionally as part of an automated build (e.g. <a href="http://hudson-ci.org/">Hudson</a>).  We had just one big problem: QUnit can run outside of a browser, but it still makes LOTS of assumptions based on being in a browser.</p>
<p>Foremost was the fact that the QUnit.log callback passed back HTML in the message parameter.  I wasn&#8217;t the only one to catch onto this issue (<em><a href="https://github.com/jquery/qunit/issues/closed#issue/32">GitHub issue 32</a></em>).  While I was still formulating a plan of attack to refactor out all of the browser oriented code, <a href="https://github.com/jzaefferer">Jörn Zaefferer</a> was putting <a href="https://github.com/jquery/qunit/commit/c2cde34d3e53f5438a984dfdacdc992c8e417919">a much simpler fix into place</a>.  His commit added an additional details object to all QUnit.log calls which will contains the original assertion message w/o HTML and possibly the raw expected and actual values (not yet documented on the main page).  Problem solved!</p>
<p><em>Or so it seemed. </em></p>
<p>As I tried to hack my example CLI test runner to use the quick fix I ran into several issues.</p>
<h3>Core QUnit logic still includes browser assumptions</h3>
<p style="padding-left:30px;">Even with changes to wrap browser calls and guard against assuming certain browser objects existing, qunit.js is full of browser code.  It would be great if the core unit testing code could be separated by the code necessary to execute properly within a browser page and separate from how test results should be displayed on an HTML page.  If these three responsibilities were found in 3 different objects, it would be simple to replace one or more with ones that fit the scenario much more closely without needing to resort to hacks or breaking backward compatibility.</p>
<p style="padding-left:30px;text-align:center;">&#160;</p>
<div id="attachment_1009" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.lostechies.com/blogs/derickbailey/archive/2009/02/11/solid-development-principles-in-motivational-pictures.aspx"><img class="size-medium wp-image-1009" title="SingleResponsibilityPrinciple2_71060858" src="http://twoguysarguing.files.wordpress.com/2010/11/singleresponsibilityprinciple2_71060858.jpg?w=300&#038;h=240" alt="Single Responsibility Principle" width="300" height="240" /></a><p class="wp-caption-text">Single Responsibility Principle</p></div>
<h3>Lifecycle callbacks break down outside of a browser</h3>
<p style="padding-left:30px;">QUnit has some really nice <a href="http://docs.jquery.com/QUnit#Integration_into_Browser_Automation_Tools">lifecycle callbacks</a> which anyone needing to integrate a testing tool can use.  They include callbacks for starting and finishing each test, individual assertions, and the whole test suite.  The first thing I wanted to add was reporting of the total number of passing and failing tests along with execution time when the tests were all done.  This looked like a simple job for QUnit.begin and QUnit.done.</p>
<p style="padding-left:30px;">It turns out that QUnit.begin won&#8217;t get called unless there is a window &#8220;load&#8221; event &#8230;. which doesn&#8217;t happen in Rhino &#8230;. so that is out.  To make matters worse, QUnit.done is getting called twice! For each test!  This means that my &#8220;final&#8221; stats are spammed with each test run.  With help of Rhino&#8217;s debugger app, I saw that the culprit were the successive &#8220;done&#8221; calls near the end of the &#8220;test&#8221; function.  Not sure how to fix that yet.</p>
<h3>&#8220;<a href="http://www.mapbender.org/JavaScript_pitfalls:_null,_false,_undefined,_NaN">Truthiness</a>&#8221; in JavaScript is a double edged sword</h3>
<p style="padding-left:30px;">Most of the time it is great not having to worry about if a value is really &#8220;False&#8221; or if it is undefined or some other &#8220;falsey&#8221; value.  Only one problem, zero (0) is falsey too.  Going back to my C days this isn&#8217;t too big of a deal (and can be used in clever ways).  However, if you are checking for the existence of properties of an object by doing a boolean expression &#8230; don&#8217;t.  Sure, undefined is falsey and if an object doesn&#8217;t have a property it will return undefined &#8230; but what if the value of that proeperty really IS undefined or in my case zero.  No good.</p>
<pre class="brush: jscript; title: ; notranslate" title="">
var foo = {cat: &#34;dog&#34;, count: 0 }
!!foo.cat   // true
!!foo.bar   // false (doesn't exist, returns undefined)
!!foo.count // false (0 is falsey)
</pre>
<p style="padding-left:30px;"><em>** The double bang (!!boolean) convention is a convienent way for converting between a falsey or truthy value to the explicit values TRUE and FALSE</em></p>
<h3>Unit test frameworks should standardize the order of arguments!</h3>
<p style="padding-left:30px;">This is a personal pet peeve.  I wish all unit testing framework would standardize whether the EXPECTED value or the ACTUAL value should go first on equals assertions.  JUnit puts expected first.  TestNG puts it second.  QUnit puts it second.  This is damn confusing if you have to switch between these tools frequently.</p>
<p>I moved my current code to a new GitHub repo -&#62; <a href="http://bit.ly/9dIeEv" target="_blank"><em><strong>QUnit-CLI</strong></em></a>.  As I learn more and find a better solution, I will keep this repo updated.  Currently the code outputs one line for each passing test and a more detailed set of lines for each failing test (including individual assertion results).  Because of the QUnit.done problem above, the &#8220;final&#8221; test suite results are shown twice for each test (making them not very &#8220;final&#8221;) which shows the total test results and execution time. <em>[Edited to have correct link]</em></p>
<p style="text-align:center;">~~~~~~~~~~</p>
<p style="text-align:center;"><span style="color:#000080;"><em>Side Note:</em> As an end goal, I would like to build a CLI for QUnit that will output <a href="http://ant.apache.org/manual/Tasks/junit.html">Ant+JUnit style XML</a> output which would make integrating these test results with other tools a piece of cake.  I CAN&#8217;T FIND THE XML DOCUMENTED ANYWHERE!  Lots of people have anecdotal evidence of how the XML will be produced but no one seems to have a DTD or XSD that is &#8220;official&#8221;.  If anyone knows of good documentation of the XML reports Ant generates for JUnit runs please let me know.  Thanks.</span></p>
<p style="text-align:center;">~~~~~~~~~~</p>
<h5 style="text-align:center;"><span style="color:#999999;"><em>λ This post has <span style="text-decoration:underline;">nothing</span> to do with Lisp or Clojure λ</em></span></h5>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Make JavaScript tests part of your build: QUnit &amp; Rhino]]></title>
<link>http://twoguysarguing.wordpress.com/2010/11/02/make-javascript-tests-part-of-your-build-qunit-rhino/</link>
<pubDate>Tue, 02 Nov 2010 05:01:19 +0000</pubDate>
<dc:creator>benjaminplee</dc:creator>
<guid>http://twoguysarguing.wordpress.com/2010/11/02/make-javascript-tests-part-of-your-build-qunit-rhino/</guid>
<description><![CDATA[I know some zealous Asynchronites who don&#8217;t believe code exists unless: it has tests code and]]></description>
<content:encoded><![CDATA[<p><span style="color:#800000;">I know some zealous <em><a href="http://www.asolutions.com/" target="_blank">Asynchronites</a></em> who don&#8217;t believe code exists unless:</span></p>
<ul>
<li><span style="color:#800000;">it has tests</span></li>
<li><span style="color:#800000;">code and tests are in source control</span></li>
<li><span style="color:#800000;">tests are running on the continuous integration (<a href="http://martinfowler.com/articles/continuousIntegration.html" target="_blank">CI</a>) build</span></li>
</ul>
<p><span style="color:#800000;"><em>Sound crazy?  If so, that is my kind of crazy.</em></span></p>
<p><span style="color:#800000;"><em><br />
</em></span></p>
<p>Lately I have been hacking around a lot with <strong>JavaScript</strong> (<a href="http://benjaminplee.github.com/HTLM5-GoL/" target="_blank">HTML5</a>, <a href="http://nodejs.org/" target="_blank">NodeJS</a>, etc&#8230;) and was asked by another team if I had any suggestions on testing JavaScript and how they could integrate it into their build.  Most of my experience testing JavaScript has been done by executing user acceptance tests written with tools like <a href="http://seleniumhq.org/" target="_blank">Selenium</a> and <a href="http://cukes.info/" target="_blank">Cucumber</a> (load a real browser and click/act like a real user).  Unfortunately these tests are slow and brittle compared to our unit tests.  More and more of today&#8217;s dynamic web apps have large amounts of business logic client side that doesn&#8217;t have a direct dependency on the browser.  What I want are FAST tests that I can run with every single change, before every single commit, and headless on our build server.  They might not catch everything, but they will catch a lot long before the UATs are finished.</p>
<h2>Goal : Run JavaScript unit tests as part of our automated build</h2>
<p>The first hurdle was finding a way to run the code headless outside of a browser.  Several of today&#8217;s embedded JavaScript interpreters are available as separate projects but for simplicity I like <em><strong><a href="http://www.mozilla.org/rhino/" target="_blank">Rhino</a></strong></em>.  Rhino is an interpreter written entirely in Java and maintained by the Mozilla Foundation.  Running out of a single .jar file and with a nice GUI debugger, Rhino is a good place to start running JavaScript from the command line.  While not fully <a href="http://www.commonjs.org/" target="_blank">CommonJS</a> compliant yet, Rhino offers a lot of nice native functions for loading other files, interacting with standard io, etc.  Also, you can easily embed Rhino in Java apps (it is now included in Java 6) and extended with Java code.</p>
<p>My JS testing framework of choice lately has been <a title="JSpec Github page" href="http://visionmedia.github.com/jspec/" target="_blank">JSpec</a> which I really like: nice GUI output, tons of assertions/matchers, async support, Rhino integration, and really nice r-spec-esque grammar.  Unfortunately JSpec works best on a Mac and the team in question needs to support Windows/*nix mainly.</p>
<p>Enter <em><strong><a href="http://docs.jquery.com/Qunit" target="_blank">QUnit</a></strong></em>: simple, fast, and used to test JQuery.  Only one problem, QUnit is designed to run in a browser and gives all of its output in the form of DOM manipulations.  Hope was almost lost when I found a tweet by <a href="http://twitter.com/jeresig/status/4477641447" target="_blank">John Resig</a> himself that suggested that QUnit could be made to work with a command line JavaScript interpreter.  Sadly despite many <a href="http://ajaxian.com/archives/qunit-a-javascript-unit-testing-framework" target="_blank">claims</a> of this functionality, I couldn&#8217;t find a single good tutorial which showed me how nor a developer leveraging this approach.  A bit of hacking, a lucky catch while reading the <a href="http://www.envjs.com/doc/guides#crawling-testing" target="_blank">Env.js</a> tutorial, and twada&#8217;s <a href="http://github.com/twada/qunit-tap/blob/master/lib/qunit-tap.js" target="_blank">qunit-tap</a> project came together in this simple solution:</p>
<h2>Step 1 : Write a test <em>(myLibTest.js)</em></h2>
<p><em>Create a simple test file for a even simpler library function.</em></p>
<pre class="brush: jscript; title: ; notranslate" title="">
test(&#34;Adding numbers works&#34;, function() {
    expect(3);
    ok(newAddition, &#34;function exists&#34;);
    equals(4, newAddition(2, 2), &#34;2 + 2 = 4&#34;);
    equals(100, newAddition(100, 0), &#34;zero is zero&#34;);}
);
</pre>
<h2>Step 2 : Create a test suite <em>(suite.js)</em></h2>
<p><em>This code could be included in the test file, but I like to keep them separate so that myLibTest.js could be included into a HTML page for running QUnit in normal browser mode without making any changes.</em></p>
<p><em>This file contains all of the Rhino specific commands as well as some formatting changes for QUnit.  After loading QUnit we need to a do a bit of house work to setup QUnit to run on the command line and override the log callback to print our test results out to standard out.  QUnit offers a number of callbacks which can be overridden to integrate w/ other testing tools (find them on the QUnit home page under &#8220;</em>Integration into Browser Automation Tools&#8221;)<em> Finally load our library and our tests. <span style="color:#ffcc00;">[EDIT: ADD]</span><br />
</em></p>
<pre class="brush: jscript; title: ; notranslate" title="">
load(&#34;../qunit/qunit/qunit.js&#34;);

QUnit.init();
QUnit.config.blocking = false;
QUnit.config.autorun = true;
QUnit.config.updateRate = 0;
QUnit.log = function(result, message) {
    print(result ? 'PASS' : 'FAIL', message);
};

load(&#34;myLib.js&#34;);
load(&#34;myLibTest.js&#34;);
</pre>
<h2>Step 3 : Write the code <em>(myLib.js)</em></h2>
<pre class="brush: jscript; title: ; notranslate" title="">
function newAddition(x, y) {
    return x + y;
}
</pre>
<h2>Step 4 : Run it</h2>
<p><em>Running Rhino is piece of cake but I am VERY lazy so I created a couple of aliases to make things dead simple. (taken from the Env.js tutorial, jsd runs the debugger)</em></p>
<pre>export RHINO_HOME="~/development/rhino1_7R2"
alias js="java -cp $RHINO_HOME/js.jar org.mozilla.javascript.tools.shell.Main -opt -1"
alias jsd="java -cp $RHINO_HOME/js.jar org.mozilla.javascript.tools.debugger.Main"</pre>
<p><em>Once the alias are created, simple run the suite</em></p>
<pre><strong>&#62; js suite.js
</strong>&#62; PASS function exists
&#62; PASS &#60;span&#62;2 + 2 = 4&#60;/span&#62;, expected: &#60;span&#62;4&#60;/span&#62;
&#62; PASS &#60;span&#62;zero is zero&#60;/span&#62;, expected: &#60;span&#62;100&#60;/span&#62;
&#62;<strong> </strong></pre>
<h2>Step 5 : Profit!</h2>
<p><em>There you go, command line output of your JavaScript unit tests.  Now we can test our &#8220;pure&#8221; JavaScript which doesn&#8217;t rely on the DOM.  Using a tool like Env.js this is also possible and will be discussed in a future post. <span style="color:#ffcc00;">[EDIT: ADD]</span></em></p>
<h2>Step 5+ : Notice the problem -&#62; HTML output on the command line</h2>
<p><em>You may have noticed that my output messages include HTML markup.  Sadly QUnit still has assumptions that it is running in a browser and/or reporting to an HTML file.  Over the next couple weeks I am going to work on refactoring out the output formatting code from the core unit testing logic and hopefully build out separate HTML, human readable command line, and Ant/JUnit XML output formatters so that integrating QUnit into your build process and Ant tasks is a piece of cake.</em></p>
<p><em>Track my progress on my GitHub <a href="http://github.com/asynchrony/qunit" target="_blank">fork</a> and on the main QUnit project <a href="http://github.com/jquery/qunit/issues#issue/32/comment/510737" target="_blank">issue</a> report.</em></p>
<p style="text-align:center;"><span style="color:#008000;"><strong><em>[EDIT] I have posted about some of my progress and setbacks <a href="http://twoguysarguing.wordpress.com/2010/11/06/qunit-and-the-command-line-one-step-closer/">here</a>.</em></strong></span></p>
]]></content:encoded>
</item>

</channel>
</rss>
