views:

587

answers:

8

I'm looking for a JavaScript Testing Framework that I can easily use in whatever context, be it browser, console, XUL, etc.

Is there such a framework, or a way to easily retrofit an existing framework so its context agnostic?

Edit: The testing framework should not be tied to any other framework such as jQuery or Prototype.js and shouldn't depend on a DOM (or document object) being present. I'm looking for something to test pure JavaScript.

+1  A: 

These run wherever javascript is enabled.

scriptaculous unit-testing

QUnit

geowa4
The problem with these is that they expect a browser environment, with a document, DOM, etc. In addition, those you listed depend on their respective frameworks.
Zach
A: 

Hi,

Not sure if this is relevant but what about Selenium - it does use a browser, although it can work with IE and Firefox.

Chris.

Chris Kimpton
A: 

Is JsUnit any help? It's designed to run in a browser, but it looks relatively abstract.

Ishmael
I did try it, but it nags about window.navigator and other things.
Zach
+1  A: 

you might want to check out YUI Test. It should work fine without a DOM.

Aaron
+3  A: 

OK, here's something I just brewed based on some earlier work. I hope this would meet your needs.

jsUnity

Lightweight Universal JavaScript Testing Framework

jsUnity is a lightweight universal JavaScript testing framework that is context-agnostic. It doesn't rely on any browser capabilities and therefore can be run inside HTML, ASP, WSH or any other context that uses JavaScript/JScript/ECMAScript.

Sample usage inside HTML

<pre>
<script type="text/javascript" src="../jsunity.js"></script>
<script type="text/javascript">
function sampleTestSuite() {
    function setUp() {
     jsUnity.log("set up");
    }

    function tearDown() {
     jsUnity.log("tear down");
    }

    function testLessThan() {
     assertTrue(1 < 2);
    }

    function testPi() {
     assertEquals(Math.PI, 22 / 7);
    }
}

// optionally wire the log function to write to the context
jsUnity.log = function (s) { document.write(s + "</br>"); };
var results = jsUnity.run(sampleTestSuite);
// if result is not false,
// access results.total, results.passed, results.failed
</script>
</pre>

The output of the above:

2 tests found
set up
tear down
[PASSED] testLessThan
set up
tear down
[FAILED] testPi: Actual value does not match what's expected: [expected] 3.141592653589793, [actual] 3.142857142857143
1 tests passed
1 tests failed
Ates Goral
I was already formulating a plan of action to do this, thanks Ates! I may have some commits for you latter.
Zach
my changes on github: http://github.com/zaach/jsunity/tree/master
Zach
A: 

There's also JSpec

JSpec is a extremely small, yet very powerful testing framework. Utilizing its own custom grammar and pre-processor, JSpec can operate in ways that no other JavaScript testing framework can. This includes many helpful shorthand literals, a very intuitive / readable syntax, as well as not polluting core object prototypes.

JSpec can also be run in a variety of ways, such as via the terminal with Rhino support, via browsers using the DOM or Console formatters, or finally by using the Ruby JavaScript testing framework which runs browsers in the background, reporting back to the terminal.

xvga
+2  A: 

Jasmine looks interesting.

According to the developers, it was written because none of the other JS test frameworks met all their needs in a single offering and not requiring things like DOM, jQuery, or the window object is one of the explicit design points.

I'm thinking of using it with env.js and Rhino/SpiderMonkey/V8/etc. to write client-side tests for my web apps which can be easily run in all the same situations as Python unit tests. (setup.py test, BuildBot, etc.)

ssokolow
A: 

I just got Hudson CI to run JasmineBDD, at least for pure javascript unit testing.

Ingvald