1
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?

More than 5 years have passed since last update.

e2eテストでタイムアウトさせたい

Posted at

e2eテストを書いていて外部サーバーでタイムアウトを発生させる
テストケースを確認したいことがあり
すこしハマったので書きとめておきたい

プラグインはmockttpを使用

//外部サーバーを実行しているラッパーメソッド
function method() {
  await api.post(path, request).catch(err => {
    if (err.code === 'ECONNABORTED') {
      console.log('タイムアウト');
    }  
  });
}

function main() {
  method();
}

上のような感じで(例がうまくかけないですが)タイムアウトのときになにか処理させるということをしたく
E2Eテストで外部サーバーでの処理をタイムアウトさせたい、ということがあった

外部サーバーの部分を以下のようにモックしてテストを実行

const endPoint = await mock.post(path).thenTimeout();

こうすると外部サーバーはタイムアウトを返してくれるようになった

ただ、外部サーバーへのリクエストパラメータや呼び出し回数を確認するため
endPoint.getSeenRequests()
をしてリクエストの情報を取得しようとしたが、タイムアウトに・・・
endPointをログにだしてみたがrequestに要素は入っている、Countも1でカウントされている

リクエストの情報も確認したいのでthenCallback()を使うようにした

const endPoint = await mock.post(path).thenCallback(async req => {
  await sleep(ms);
  return {
    statusCode: 200
  }
});

sleepは引数の時間分スリープするメソッド
テストのタイムアウト時間より長く設定することで、タイムアウトエラーが発生
endPoint.getSeenRequests()でリクエスト情報を取得できるようになった

thenTimeout()がどういう感じで動いているか
getSeenRequests()でどうしてタイムアウトになってしまうのか
まだ理解しきれていないのでまた確認しておきたい

1
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
1
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?