Handling errors in a Tape test?
Asked Answered
B

1

5

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?

Brina answered 11/10, 2015 at 13:26 Comment(0)
C
8

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); });
Carola answered 15/11, 2015 at 23:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.