1) Why do I have to use an inline function to surround my tested function?
You don't. When you write throws(subfunc(), [...])
, subfunc()
is evaluated first. As subfunc()
throws outside the throws
function, the test fails immediately. In order to fix it, you have to pass throws
a function. function(){subfunc()}
works, but so does subfunc
:
test("This works", function () {
throws(subfunc, "Must throw error to pass.");
});
2) Why does the last test not fail? 'func' does not throw any exception.
For the same reason. func()
is evaluated first. As there is no explicit return
statement, it returns undefined
. Then, throws
tries to call undefined
. As undefined
is not callable, an exception is thrown and the test passes.
test("This doesn't work", function () {
throws(func, "Must throw error to pass.");
});