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?

【Jest】全てのテストのタイムアウト時間を変更する

1
Last updated at Posted at 2026-01-07

はじめに

Jestのデフォルトのタイムアウト時間は5秒です。短かったのでこの時間を変更する方法を調べました。いくつか方法はあるのですが、今回は全てのテストのタイムアウト時間を変更する方法です。

問題

タイムアウトのデフォルト時間は5秒です。5秒経過するとそのテストは失敗(failed)扱いになるのですが、本番ではDBからのデータ取得で5秒以上かかるケースもあります。

thrown: "Exceeded timeout of 5000 ms for a test.

解決方法

testTimeoutプロパティをjest.config.mjsに設定すればOKです。

jest.config.mjs
export default {
  testEnvironment: "jsdom",
  moduleNameMapper: {
    "\\.(css|less)$": "identity-obj-proxy",
  },
  setupFilesAfterEnv: ["./jest.setup.js"],
+  testTimeout: 10000,  // タイムアウト時間を10秒へ変更
};

おわりに

補足ですがjest.config.mjstestTimeoutプロパティに設定したタイムアウト時間よりも、test()の第三引数で指定したタイムアウト時間が優先されます。

sample.spec.js
test("サンプルテスト", () => {
  expect(true).toBe(true);
}, 3000); // testTimeoutプロパティの値に関係なくタイムアウト時間が3秒になる

jest.config.mjsで標準のタイムアウト時間を5秒から変更する方法でした。

参考

JISOUのメンバー募集中!

プログラミングコーチングJISOUでは、新たなメンバーを募集しています。
日本一のアウトプットコミュニティでキャリアアップしませんか?
興味のある方は、ぜひホームページをのぞいてみてください!
▼▼▼
https://projisou.jp

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?