0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

vitestでasync関数で投げる例外をテストする

Posted at

たぶん正解

Promiseに対する評価ということで、rejectsを経由して評価する。

await expect(longLongTimeAsyncFunction(AbortSingal.timeout(100)))
  .rejects.toMatchObject({name: 'TimeoutError'})

もしくは、messageの方で引っ掛けてこんな感じかな。

await expect(longLongTimeAsyncFunction(AbortSingal.timeout(100)))
  .rejects.toThrowError(/timeout/);

安直な方法

何も考えずに、こんな風にassert.fail()と組み合わせた方がわかりやすいと思ってしまう。

#assert.fail()忘れてしまったりするのですが

try {
  await longLongTimeAsyncFunction(AbortSingal.timeout(100)));
  assert.fail();
} catch(e) {
  expect(e.name).toBe('TimeoutError');
}
await longLongTimeAsyncFunction(AbortSingal.timeout(100)))
  .then(() => assert.fail())
  .catch((e) => expect(e.name).toBe('TimeoutError'))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?