Reputation: 24768
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.
Upvotes: 11
Views: 2223
Reputation: 32530
Is JsUnit any help? It's designed to run in a browser, but it looks relatively abstract.
Upvotes: 0
Reputation: 443
I just got Hudson CI to run JasmineBDD, at least for pure javascript unit testing.
Upvotes: 0
Reputation: 15345
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.)
Upvotes: 2
Reputation: 601
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.
Upvotes: 0
Reputation: 140042
OK, here's something I just brewed based on some earlier work. I hope this would meet your needs.
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.
<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
Upvotes: 4
Reputation: 382
you might want to check out YUI Test. It should work fine without a DOM.
Upvotes: 1