最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - How to expect a promise to be resolved in vitest with custom error message - Stack Overflow

programmeradmin0浏览0评论

In vitest you can expect a Promise to be resolved using this code:

await expect(expected, "custom message").resolves.toBeUndefined()

However, when failing to resolve, the custom message is not shown:

promise rejected "undefined" instead of resolving

How to display this custom message when the expect fails?

In vitest you can expect a Promise to be resolved using this code:

await expect(expected, "custom message").resolves.toBeUndefined()

However, when failing to resolve, the custom message is not shown:

promise rejected "undefined" instead of resolving

How to display this custom message when the expect fails?

Share Improve this question edited Feb 6 at 9:51 Blee asked Feb 6 at 8:53 BleeBlee 6477 silver badges13 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

I ended up adding a little util that catches the error and throws the custom message:

export async function expectToResolve(expected: Promise<any>, msg: string) {
  try {
    await expect(expected).resolves.toBeUndefined()
  } catch (cause) {
    throw new Error(msg, {cause})
  }
}

It can be used like this:

await expectToResolve(promise, "custom message");
发布评论

评论列表(0)

  1. 暂无评论