Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling errors in a Tape test?

If I have a function that throws an error and I want to test for that error, I'd write something like this:

test('throws at something that is not a string', t => {
  t.plan(1)
  t.err(loadString(9))
})

But this always results in an actual error coming from the function while executing:

And there's the not ok 1 no plan found and not ok 2 no assertions found, which is also weird. How can I make sure it doesn't actually throw?

like image 917
jona Avatar asked Oct 11 '15 13:10

jona


1 Answers

If you want to test if the function will throw, you have to use t.throws and pass it a function (not the error value). Here, I assume loadString is the actual function you are testing, so YOU are actually causing it to throw instead of Tape invoking it and catching the error.

Try this instead:

t.throws(function() { loadString(9); });
like image 199
kmiyashiro Avatar answered Nov 05 '22 21:11

kmiyashiro