SuiteJS is a unit testing framework for JavaScript.
First, because it is simple and powerful, utilizing modern JavaScript programming techniques and conventions, made with the JS pro in mind.
Second, because it is not made only for browser testing. If you use JavaScript for server-side code, or any application code, SuiteJS will be happy to help you out!
suiteJS.assert(true)
That was easy enough, eh? Without changing anything, that assumes you’re in a browser and tries to write out the test status. If you’re not in a browser, or want to do something else, you can customize the behavior.
What if I’m not writing a webpage, I’m writing a server-side app? You’ll want to set up your pass/fail functions to do something different.
suiteJS.setPass(function(msg){})
suiteJS.setFail(function(msg) {
log.error(msg)
})
And there you go. These and all SuiteJS options can be set for the global suiteJS object or on individual test suites. Now let’s try something more complicated:
var suite = suiteJS.createSuite("My Test Suite")
suite.setUp = function() {
this.pants = "Pants"
}
suite.tearDown = function() {
delete(this.pants)
}
suite.testPants = function() {
this.assertEquals("Pants", this.pants, "Pants should exist")
}
suite.run()
Not bad, eh?
SuiteJS Self-Test is a page that tests various SuiteJS functionality, using SuiteJS. Layers and layers of testing fun!
Well, you’ll notice the assertion argument orders are different.
JUnit: assertEquals(message, expected, actual)
SuiteJS: assertEquals(expected, actual, message)
Why? First, It’s because I think arguments should always occupy the same ordinal position in the argument list, regardless of if an optional argument exists or not. Since message is optional, in JUnit omitting it turns ‘expected’ into the first argument. I don’t like that. In fact, JS doesn’t like that either, since its style of overloading doesn’t play nice with that argument style. Why don’t I like it? If you’re scanning the code, it’s nice to always know “Oh, the first and second argument is the expected and actual” and not have to guess based on the arguments themselves or the argument count. If you don’t agree, feel free to hack the source and reverse the argument orders.
Why does XUnit / JUnit do it the other way? My guess is they found it convenient to see the message first so the first thing you see is a description of what is being tested. Eh, that’s fine, but I don’t see that as a fair tradeoff.