您如何调试Jest测试? [英] How do you debug Jest Tests?
问题描述
我找不到有关调试用Jest编写的单元测试的任何信息。
I can't find any information on debugging my unit tests written with Jest.
如何调试Jest测试?
How do you debug Jest Tests ?
推荐答案
您可以使用Chrome DevTools调试Jest测试。
You can use Chrome DevTools to debug Jest tests.
首先,在项目中启动Node调试器:
First, start Node debugger in your project:
node --inspect-brk <path to jest.js> --runInBand <path to your test file>
示例:
-
如果在本地安装Jest(Linux示例):
If you install Jest locally (Linux example):
node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand mymodule / test.js
如果全局安装Jest(Windows示例):
If you install Jest globally (Windows example):
node --inspect-brk C:\\Program Files\\nodejs\\node_modules\\jest\\bin\\jest.js --runInBand mymodule\\test.js
然后,您可以打开Google Chrome浏览器,然后在地址栏中输入:
Then, you can open the Google Chrome browser, and type in the address bar:
chrome://inspect
现在,点击远程目标下的检查链接,打开Chrome DevTools。
Now click the inspect link under "Remote Target" to open Chrome DevTools.
请注意,您可能需要将源代码文件夹添加到chrome-devtools中的工作区中,以便能够设置断点。
Note that you probably need to add the source code folder to the workspace in chrome-devtools, so as to be able to set breakpoints.
现在您可以按F8键开始调试。
Now you can press F8 to start debugging.
[FYI] :
- 我的节点版本:v10.11.0
- 我的Jest版本:23.6.0
- 我的Google Chrome版本:71.0.3578.98
[更新]
关于将源代码文件夹添加到chrome-devtools中的工作区(按Sam的要求),如下所示:
[Update] Regarding the step of adding the source code folder to the workspace in chrome-devtools (as asked by Sam), it looks like below:
然后您可以打开脚本文件并设置断点:
And then you can open your script files and set the breakpoints:
您会找到简单的我的GitHub存储库上的演示项目。
这篇关于您如何调试Jest测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!