メインコンテンツへスキップ
Version: 29.4

トラブルシューティング

おやおや、うまくいきませんか? Jest についての問題を解決するのにこのガイドをご利用下さい。

理由は分からないがテストが失敗する

Try using the debugging support built into Node. debugger;宣言をテストの任意の箇所に記述し、プロジェクトのディレクトリで以下のコマンドを実行して下さい。

node --inspect-brk node_modules/.bin/jest --runInBand [any other arguments here]
or on Windows
node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand [any other arguments here]

このコマンドでJestは外部からのデバッガと接続できる状態のNodeプロセス上で実行されます。 プロセスはデバッガが接続されるまでポーズすることに注意して下さい。

Google Chrome (もしくは Chromium ベースのブラウザ)でデバッグするには、ブラウザを開いて、chrome://inspect にアクセスし、 "Open Dedicated DevTools for Node" を開きます。 そうすれば接続できるアクティブな node インスタンスの一覧が表示されます。 上記のコマンドを実行した後、端末に表示されるアドレス(通常は localhost:9229のようなもの)をクリックします。 ChromeのDevToolsを使ってJestをデバッグできます。

ChromeDeveloperToolsが表示され、Jest の CLIスクリプトの1行目にブレークポイントが設定されます(これは開発者ツールを開くための時間を設けて、その前に Jest が実行されてしまうのを防ぐためのものです)。 次の実行に進むには画面の上部右側にある再生ボタンのようなボタンをクリックします。 debugger 宣言を含むテストをJestが実行した場合、実行はポーズされ現在いのスコープとコールスタックを調べることができます。

note

The --runInBand cli option makes sure Jest runs the test in the same process rather than spawning processes for individual tests. 通常Jestはプロセスをまたいで並列にテストを実行しますが、同時に複数のプロセスをデバッグするのは困難だからです。

VS Codeでデバッグする

There are multiple ways to debug Jest tests with Visual Studio Code's built-in debugger.

組み込みのデバッガを追加するには、前述した形でテストを実行して下さい。

node --inspect-brk node_modules/.bin/jest --runInBand [any other arguments here]
or on Windows
node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand [any other arguments here]

そして以下の launch.jsonの設定によりVS Codeのデバッガを追加して下さい:

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229
}
]
}

自動的に起動してテストを実行するプロセスに追加するには、以下の構成を使用して下さい:

{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}

もしくは Windows の場合は以下を使用してください:

{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/jest/bin/jest.js",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}

Facebookのcreate-react-appを使用しているなら、Jestのテストを以下の設定でデバッグできます:

{
"version": "0.2.0",
"configurations": [
{
"name": "Debug CRA Tests",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
"args": [
"test",
"--runInBand",
"--no-cache",
"--env=jsdom",
"--watchAll=false"
],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}

Nodeによるデバッグについての詳細はここで確認できます。

WebStormでのデバッグ

WebStorm has built-in support for Jest. Read Testing With Jest in WebStorm to learn more.

キャッシュ関連の問題

Jestがコードを変換するスクリプトの変更や、Babelのアップデートを認識できていませんか?

--no-cacheオプションを付けて再実行してみて下さい。 Jestはテスト実行の高速化のために変換されたモジュールファイルをキャッシュします。 独自のトランスパイラを使用している場合は、getCacheKey関数を追加することを検討して下さい:getCacheKey in Relay

Promiseが解決されない

promiseが全く解決されない場合、このエラーが投げられるでしょう:

- Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.`

最も多い原因はPromiseの実装が競合していることです。 Consider replacing the global promise implementation with your own, for example globalThis.Promise = jest.requireActual('promise'); and/or consolidate the used Promise libraries to a single one.

テストの実行が長時間かかる場合は、jest.setTimeout を呼び出してタイムアウト時間を伸ばすことも検討したくなるでしょう。

jest.setTimeout(10_000); // 10 second timeout

Watchman関連の問題

Jestを--no-watchman オプション付きで実行するか 構成オプションでwatchmanfalseに設定してみてください。

watchman troubleshootingも参照して下さい。

Dockerおよび/またはCIサーバ上でテストが極端に遅くなる

While Jest is most of the time extremely fast on modern multi-core computers with fast SSDs, it may be slow on certain setups as our users have discovered.

Based on the findings, one way to mitigate this issue and improve the speed by up to 50% is to run tests sequentially.

これを実施するには--runInBandオプションを指定してテストを同じスレッドで実行して下さい。

# Using Jest CLI
jest --runInBand

# Using your package manager's `test` script (e.g. with create-react-app)
npm test -- --runInBand

テスト実行時間を高速化するもう1つの案としてはTravis-CIのようなCIサーバでワーカーのプール数を最大4までに設定することです。 Travis-CIに特定すると、テストの実行時間を半分にできます。 注意: オープンソースプロジェクトで利用できるTravis-CIの フリープランは2CPUコアまでしか割り当てられません。

# Using Jest CLI
jest --maxWorkers=4

# Using your package manager's `test` script (e.g. with create-react-app)
npm test -- --maxWorkers=4

If you use GitHub Actions, you can use github-actions-cpu-cores to detect number of CPUs, and pass that to Jest.

- name: Get number of CPU cores
id: cpu-cores
uses: SimenB/github-actions-cpu-cores@v2
- name: run tests
run: yarn jest --max-workers ${{ steps.cpu-cores.outputs.count }}

Another thing you can do is use the shard flag to parallelize the test run across multiple machines.

coveragePathIgnorePatterns が動作していない

babel-plugin-istanbul プラグインを使用していないことを確認してください。 Jest は Istanbul をラップするため、カバレッジを収集するのにどのファイルを使用するのかを Istanbul に伝えます。 babel-plugin-istanbul を使用した場合、Babel により処理される全てのファイルはカバレッジ収集用コードを備えているので、coveragePathIgnorePatterns で無視されなくなります。

Defining Tests

Tests must be defined synchronously for Jest to be able to collect your tests.

As an example to show why this is the case, imagine we wrote a test like so:

// Don't do this it will not work
setTimeout(() => {
it('passes', () => expect(1).toBe(1));
}, 0);

When Jest runs your test to collect the tests it will not find any because we have set the definition to happen asynchronously on the next tick of the event loop. This means when you are using test.each you cannot set the table asynchronously within a beforeEach / beforeAll.

まだ解決しませんか?

Help を参照してください。