处理磁带测试中的错误?

Handling errors in a Tape test?

如果我有一个抛出错误的函数并且我想测试该错误,我会这样写:

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

但这总是会导致函数在执行时出现实际错误:

还有 not ok 1 no plan foundnot ok 2 no assertions found,这也很奇怪。我怎样才能确保它不会真的抛出?

如果你想测试函数是否会抛出异常,你必须使用 t.throws 并向它传递一个函数(而不是错误值)。在这里,我假设 loadString 是您正在测试的实际函数,所以您实际上是在导致它抛出,而不是 Tape 调用它并捕获错误。

试试这个:

t.throws(function() { loadString(9); });