I didn't realize until the other day that there's a unit testing framework for Javascript, JSUnit. I'm a big fan of NUnit, NUnitAsp, and CruiseControl.NET, so I thought I'd give JSUnit a try.
Essentially, you just need to include jsUnitCore.js on your test page, and then you can use Assert statements that you should be familiar with from other xUnit testing tools (assertEquals, assertNull, assertTrue, assertFalse). Here's an example:
<html>
<head>
<title>Test Page for multiplyAndAddFive(value1, value2)</title>
<script language="javascript" src="jsUnitCore.js"></script>
<script language="javascript" src="myJsScripts.js"></script>
</head>
<body>
<script language="javascript">
function testWithValidArgs() {
assertEquals("2 times 3 plus 5 is 11", 11, multiplyAndAddFive(2, 3));
assertEquals("Should work with negative numbers", -15, multiplyAndAddFive(-4, 5));
}
function testWithInvalidArgs() {
assertNull("A null argument should result in null", multiplyAndAddFive(2, null));
assertNull("A string argument should result in null", multiplyAndAddFive(2, "a string"));
}
function testStrictReturnType() {
assertNotEquals("Should return a number, not a string", "11", multiplyAndAddFive(2, 3));
}
function testWithUndefinedValue() {
assertNull("An undefined argument should result in null", multiplyAndAddFive(2, JSUNIT_UNDEFINED_VALUE));
}
</script>
</body>
</html>
Then there's a TestRunner (HTML) page that you can use to execute your tests, with a (hopefully!) green progress bar (similar to NUnit) to show the status of your tests. Now, since the test runner needs to load a pure HTML file, your test pages will need to be .html (not .aspx). So that being the case, I can only really see JSUnit being useful to test libraries of Javascript code, rather than Javascript that sits inside of an ASP.NET page, at least that's my initial impression. It's designed for Java folks, so there's a server side implementation that comes with the download for integrating with Ant, so presumably JSUnit could also be integrated with nant or your favorite build tool.