Monday, March 30th, 2009
Improved Readability for JavaScript Unit Tests
Christian Johansen is tired of:
mentally decoding test names like "testNewUserWithClownShoesShouldSqueak", so I've written some code to improve readability for JavaScript unit tests written using JsUnitTest (from prototypejs). It's heavily inspired by Ruby frameworks Shoulda and Context.
Tests with his new framework--called jscontext--look like this:
"A new User": JsContext({
"with clown shoes": JsContext({
"should squeak": function() { with(this) {
// ...
}},
// ...more tests and/or contexts
}),
"without clown shoes": JsContext({
"should not squeak": function() { with(this) {
// ...
}},
// ...more tests and/or contexts
}),
// ...more tests and/or contexts
}),
// ...more tests and/or contexts
}, { testLog: "testlog" });
Christian maintains that:
Using this, the test runner will label tests a lot more readable; where you may have been used to "testNewUserWithClownShoesShouldSqueak" you now get "A new user with clown shoes should squeak".
Christian's introduction/announcement blog post has a lot of additional details. Looks very cool!







Looks similar to Choan Galvez’s projects:
http://jshoulda.scriptia.net/
http://sugartest.scriptia.net/
Indeed they do, hadn’t seen them before. The only difference is the syntax. I liked the JShoulda syntax, but the Sugartest syntax did not appeal to me. Way too jQuery-ish (ie long chains).
It all boils down to taste though, the important part is that you test in a environment your comfortable with :)