Search code examples
node.jstestingtapnode.js-tape

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?


Solution

  • 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); });