SuiteJS

Assert yourself!

SuiteJS is a unit testing framework for JavaScript.

Why SuiteJS?

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!

Features:

  • Supports XUnit style. Suites, setUp, tearDown, asserts, etc.
  • But doesn’t require it. Work however you’d like, the XUnit-style suites are simply a convenience if you want to use them.
  • Customizeable. Send the results to a server via AJAX, print them to the screen, whatever. You choose.
  • Flexible. Sprinkle your code with assertions, set up large test suites, or take any approach you’d like. Choose your style and run with it.
  • Browser-independant. Doesn’t even need a browser, much less any particular one.
  • Chaining. Be efficient, chain assertions together.

Examples

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?

Documentation

Here it is

Download

Download SuiteJS

License

MIT License

Self-Test

SuiteJS Self-Test is a page that tests various SuiteJS functionality, using SuiteJS. Layers and layers of testing fun!

Differences from XUnit / JUnit

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.