疑难解答
遇到什么问题了吗? 使用本指南来解决 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. 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. 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). 点击开发者工具中右上方调试菜单栏中的“开始调试” 按钮,让代码继续执行。 点击开发者工具中右上方调试菜单栏中的“开始调试” 按钮,让代码继续执行。 当 Jest 执行到添加了debugger;
语句的单元测试时,执行就会暂停,此时,你可以检查当前的值域和调用栈。
The --runInBand
cli option makes sure Jest runs the test in the same process rather than spawning processes for individual tests. 通常情况下,Jest 并行化测试会跨进程执行,但是很难同时调试多个进程。
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?
尝试使用 --no-cache
选项。 Jest 会缓存转换的模块文件来加速测试的执行。 If you are using your own custom transformer, consider adding a getCacheKey
function to it: getCacheKey in Relay.
未返回的 Promises
如果一个 Promise 并未返回任何东西(no resolve)你会看到类似于下边的报错:
- 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.
If your test is long running, you may want to consider to increase the timeout by calling jest.setTimeout
jest.setTimeout(10_000); // 10 second timeout
看门狗问题
Try running Jest with --no-watchman
or set the watchman
configuration option to false
.
更多详情,查看 看门狗疑难解答.