当承诺被拒绝时抛出测试失败

Throws test fails when promise is rejected

我有这个功能

export async function trivialAsyncFail() {
    return new Promise((resolve,reject) => {
        reject("This is supposed to happen");
    });
}

我有以下测试来测试它:

test("Async fail", async (t) => {
    const failedPromise = trivialAsyncFail();
    t.throws(failedPromise);
    await failedPromise;
});

但是我的测试失败并显示消息:

Async fail

Rejected promise returned by test

Rejection reason:

"This is supposed to happen"

我不确定我是否误解了 t.throws 的工作原理。我假设如果您希望承诺被拒绝并且它被拒绝,那么测试应该会成功。

使用 ava 0.19

According to the documentation:

When testing a promise you must wait for the assertion to complete:

test('rejects', async t => {
  await t.throws(promise);
});

所以 await t.throws(failedPromise) 而不是 await failedPromise