トラブルシューティング
おやおや、うまくいきませんか? 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が実行した場合、実行はポーズされ現在いのスコープとコールスタックを調べることができます。
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.