简体   繁体   English

你如何调试 Jest 测试?

[英]How do you debug Jest Tests?

I can't find any information on debugging my unit tests written with Jest.我找不到任何有关调试用 Jest 编写的单元测试的信息。

How do you debug Jest Tests ?你如何调试 Jest 测试?

You do not need Chrome for Jest tests.您不需要Chrome进行Jest测试。

The simplest solution I found is to use VS Code JavaScript Debug Terminal .我找到的最简单的解决方案是使用VS Code JavaScript Debug Terminal

And it works with Typescript and Nrvl.Nx work-spaces out of the box.它可以与 Typescript 和 Nrvl.Nx 开箱即用的工作空间一起使用。

  1. Open the command palette and start Debug: JavaScript Debug Terminal :打开命令面板并启动Debug: JavaScript Debug Terminal

在此处输入图像描述

  1. Run tests in that terminal in a Watch mode npm test --watch .在该终端中以Watch 模式npm test --watch
  2. Set a break-point in your file.在文件中设置断点。
  3. Make any change in a file you want to debug and save.对要调试和保存的文件进行任何更改。
  4. watch will run Jest tests against modified files. watch将对修改的文件运行 Jest 测试。

When you want to narrow down the number of files run by the --watch, press p in the terminal and enter a pattern, which is just a part of the file name you want to test and hit [Enter]当您想缩小 --watch 运行的文件数量时,在终端中按p并输入一个模式,这只是您要测试的文件名的一部分,然后按 [Enter]

To limit it to a single test in a file - focus it with f, so change it(...) to fit(...)要将其限制为文件中的单个测试 - 用 f 聚焦它,因此将它(...)更改为 fit(...)

You can use Chrome DevTools to debug Jest tests.您可以使用 Chrome DevTools 来调试 Jest 测试。

First , start Node debugger in your project:首先,在您的项目中启动 Node 调试器:

node --inspect-brk <path to jest.js> --runInBand <path to your test file>

Examples:例子:

  • If you install Jest locally (Linux example):如果您在本地安装 Jest(Linux 示例):

    node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand mymodule/test.js

  • If you install Jest globally (Windows example):如果您全局安装 Jest(Windows 示例):

    node --inspect-brk "C:\\Program Files\\nodejs\\node_modules\\jest\\bin\\jest.js" --runInBand mymodule\\test.js

Then , you can open the Google Chrome browser, and type in the address bar:然后,您可以打开 Google Chrome 浏览器,在地址栏中输入:

chrome://inspect

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.请注意,您可能需要将源代码文件夹添加到 chrome-devtools 中的工作区,以便能够设置断点。

Now you can press F8 to start debugging.现在您可以按 F8 开始调试。

[FYI] : [仅供参考]

  • My Node version: v10.11.0我的节点版本:v10.11.0
  • My Jest version: 23.6.0我的笑话版本:23.6.0
  • My Google Chrome version: 71.0.3578.98我的谷歌浏览器版本:71.0.3578.98

[Update] Regarding the step of adding the source code folder to the workspace in chrome-devtools (as asked by Sam), it looks like below: [更新]关于在 chrome-devtools 中将源代码文件夹添加到工作区的步骤(根据 Sam 的要求),如下所示:

在此处输入图像描述

And then you can open your script files and set the breakpoints:然后你可以打开你的脚本文件并设置断点:

在此处输入图像描述

You find the simple demo project on my GitHub repo .您可以在我的 GitHub 存储库中找到简单的演示项目

I wrote a blog post on 7 ways to debug Jest tests in terminal .我写了一篇关于在终端中调试 Jest 测试的 7 种方法的博文。 Here's the fastest way to do it from it.这是最快的方法。

You can install ndb , an improved debugging experience for Node.js, enabled by Chrome DevTools, with您可以安装ndb ,这是一种改进的 Node.js 调试体验,由 Chrome DevTools 启用,使用

npm install -g ndb

Then you can run:然后你可以运行:

ndb npm run test

which will open the DevTools for you and run the tests.这将为您打开 DevTools 并运行测试。

Or you can just put in one command with:或者您可以只输入一个命令:

npx ndb npm run test

and you're good to go.你可以走了。 But do check out the blog post where I go into details of different ways to debug Jest tests.但是请查看博客文章,其中详细介绍了调试 Jest 测试的不同方法。

This is my basic config for debugging Jest in VSCode, add to your launch configurations within settings.json这是我在 VSCode 中调试 Jest 的基本配置,添加到 settings.json 中的启动配置

"launch" : {
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Jest",
      "program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
      "args": [
          "-i"
      ],
       "skipFiles": [
        "<node_internals>/**/*.js", "node_modules",
      ]
    }
  ],
},

As of 2021, check Debugging in VS Code from Jest docs.截至 2021 年,请查看 Jest 文档中的 VS Code 中的调试 Add the configurations below to your .vscode/launch.json file.将以下configurations添加到您的.vscode/launch.json文件中。

{
  "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",
      "port": 9229
    }
  ]
}

For anyone seeking an answer, you debug jest with node-inspector.对于任何寻求答案的人,您可以使用 node-inspector 调试 jest。 However this is a very slow process (2-5 minutes to debug a single test), and wasn't very useful in actual usage.然而,这是一个非常缓慢的过程(调试单个测试需要 2-5 分钟),并且在实际使用中并不是很有用。

I haven't found a better way to debug Jest, however I have seen a lot of people doing the extra work to make jasmine or mocha work with react components.我还没有找到更好的调试 Jest 的方法,但是我看到很多人做额外的工作来让 jasmine 或 mocha 与 react 组件一起工作。 So that may be a more viable option until node-inspector is actually usable.因此,在节点检查器实际可用之前,这可能是一个更可行的选择。

Run

node --debug-brk node_modules/.bin/jest

in your project directory.在您的项目目录中。 It should start the node process paused an listening (usually at port 5858) for a debugger.它应该启动node进程暂停调试器的侦听(通常在端口 5858)。 Visual Studio Code is an example of a nice graphical debugger for node.js that can be used to connect to the listening node process. Visual Studio Code是一个很好的 node.js 图形调试器示例,可用于连接到侦听节点进程。

@Yuci answer was perfect for me. @Yuci 的回答对我来说是完美的。

I'm just precising it in case of you're using jest inside local Docker container.如果您在本地 Docker 容器中使用 jest,我只是对其进行精确化。 You need to precise --inspect-brk=0.0.0.0:9229您需要精确--inspect-brk=0.0.0.0:9229

node --inspect-brk=0.0.0.0:9229 <path to jest.js> --runInBand <path to your test file>

Adding jest configuration would work only if there is only one project.仅当只有一个项目时,添加 jest 配置才有效。 However, if there are multiple projects within its own folder (ie rootfolder/project1, rootfolder/project2), running "jest --watchAll --runInBand --coverage" with breakpoint either in the test file or the file under test would be good option但是,如果它自己的文件夹中有多个项目(即 rootfolder/project1、rootfolder/project2),则在测试文件或被测文件中运行带有断点的“jest --watchAll --runInBand --coverage”会很好选项

Here's a one-liner you can add to your projects to use debugger;这是您可以添加到项目中以使用debugger; in your Jest test files:在您的 Jest 测试文件中:

  1. add this script to package.json :将此脚本添加到package.json
  "test:debug": "open -a \"Google Chrome\" chrome://inspect && node --nolazy --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --colors --verbose",
  1. run the script within your project:在您的项目中运行脚本:
yarn test:debug insert/path/to/file
  1. Now you can use debugger;现在你可以使用debugger; within your test files to trigger line-of-code breakpoints in Google Chrome!在您的测试文件中触发 Google Chrome 中的代码行断点 🐛 🔫 🐛 🔫

The script has two parts.该脚本有两个部分。 First, it opens Chrome — to: chrome://inspect — and then it will start a Node process (where we can use debugger) that runs a jest file (from node modules) on a specific test file.首先,它打开 Chrome - 到: chrome://inspect - 然后它会启动一个 Node 进程(我们可以在其中使用调试器),在特定的测试文件上运行一个 jest 文件(来自节点模块)。 As a result, you must have Jest installed in your project dependencies!因此,您必须在项目依赖项中安装 Jest!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM