トラブルシューティング
おやおや、うまくいきませんか? Jest についての問題を解決するのにこのガイドをご利用下さい。
理由は分からないがテストが失敗する
Try using the debugging support built into Node. Place a debugger; statement in any of your tests, and then, in your project's directory, run:
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プロセス上で実行されます。 プロセスはデバッガが接続されるまでポーズすることに注意して下さい。
To debug in Google Chrome (or any Chromium-based browser), open your browser and go to chrome://inspect and click on "Open Dedicated DevTools for Node", which will give you a list of available node instances you can connect to. Click on the address displayed in the terminal (usually something like localhost:9229) after running the above command, and you will be able to debug Jest using Chrome's DevTools.
ChromeDeveloperToolsが表示され、Jest の CLIスクリプトの1行目にブレークポイントが設定されます(これは開発者ツールを開くための時間を設けて、その前に Jest が実行されてしまうのを防ぐためのものです)。 次の実行に進むには画面の上部右側にある再生ボタンのようなボタンをクリックします。 When Jest executes the test that contains the debugger statement, execution will pause and you can examine the current scope and call stack.
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]
Then attach VS Code's debugger using the following launch.json config:
{
"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"
}
]
}
If you are using Facebook's create-react-app, you can debug your Jest tests with the following configuration:
{
"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"
}
]
}
More information on Node debugging can be found here.
WebStormでのデバッグ
WebStorm has built-in support for Jest. Read Testing With Jest in WebStorm to learn more.