Устранение неполадок
Упс, что-то пошло не так? Используйте данное руководство для решения проблем с 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]
This will run Jest in a Node process that an external debugger can connect to. Note that the process will pause until the debugger has connected to it.
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.
The Chrome Developer Tools will be displayed, and a breakpoint will be set at the first line of the Jest CLI script (this is done to give you time to open the developer tools and to prevent Jest from executing before you have time to do so). Click the button that looks like a "play" button in the upper right hand side of the screen to continue execution. 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. Normally Jest parallelizes test runs across processes but it is hard to debug many processes at the same time.
Debugging in VS Code
There are multiple ways to debug Jest tests with Visual Studio Code's built-in debugger.
To attach the built-in debugger, run your tests as aforementioned:
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
}
]
}
To automatically launch and attach to a process running your tests, use the following configuration:
{
"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"
}
]
}
or the following for 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"
}
]
}
Более подробную информацию об отладке Node можно найти здесь.
Debugging in WebStorm
WebStorm has built-in support for Jest. Read Testing With Jest in WebStorm to learn more.
Проблемы кеширования
The transform script was changed or Babel was updated and the changes aren't being recognized by Jest?
Retry with --no-cache
. Jest caches transformed module files to speed up test execution. If you are using your own custom transformer, consider adding a getCacheKey
function to it: getCacheKey in Relay.
Unresolved Promises
If a promise doesn't resolve at all, this error might be thrown:
- Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.`