Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context agnostic JavaScript Testing Framework

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.

like image 623
Zach Avatar asked Jan 28 '09 20:01

Zach


3 Answers

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
like image 172
Ates Goral Avatar answered Oct 24 '22 09:10

Ates Goral


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.)

like image 2
ssokolow Avatar answered Oct 24 '22 09:10

ssokolow


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

like image 1
Aaron Avatar answered Oct 24 '22 08:10

Aaron