I am not good at writing bug-free code in JavaScript or CoffeeScript. I am equally not good at writing bug-free tests. It would be nice to have a way to quickly build a basic 'safety net' for my code, and be able to describe the tests in a format less verbose than 'describe->it-should->etc'.
It should also be a good learning experience.
- Short and sweet syntax for asserting basic behaviour.
- It should be relatively easy to create new ad-hoc assertions.
# describe how they should work
describe 'asserters.verify', ->
# describe a mock module that 'exports' various things
myModule =
export1: {a:'a', b:'b'}
export2: i:'am', one:'object'
funcWithNoArgs: (callback) -> callback(null, "with", "some", "extra")
funcWithOneArg: (input, callback) ->
assert _.isFunction(callback), "callback must be a function"
callback(new Error("error") if input is "error")
funcWithTwoArgs: (input1, input2, callback) ->
assert _.isFunction(callback), "callback must be a function"
if input1 is "error" or input2 is "error"
callback(new Error("error"))
else
callback(null, "with", "some", "extra")
# describe the assertions that need to be performed
myAsserts =
abc: [ new Asserter(), new Asserter() ] # abc doesn't exist, but the asserter doesnt require it
# abc2: [ a.IsDefined ] # this would fail
# def: [ new AssertIsDefined() ] # def doesn't exist
export1: [ a.IsObject, a.HasFields(['a', 'b']) ]
export2: [ a.IsObject, a.HasFields(['i', 'one']) ]
funcWithNoArgs: [ a.CanCallback() ]
funcWithTwoArgs: [ a.CanCallbackError('success', 'error'), a.CanCallback('success', 'success') ]
funcWithOneArg: [ a.CanCallbackError('error'), a.CanCallback('success') ]
it 'should get no errors when verified', (done) -> a.verify myAsserts, myModule, done
My personal grain-of-salt opinion is that you should test the behaviour, not the specifics of an implementation. If you do want fake types have a look at https://developers.google.com/closure/compiler/. There is also a typed version of CoffeeScript but I can't find a link.