【问题标题】:Debugging jest unit tests with create-react-app doesn't hit breakpoint in VSCode使用 create-react-app 调试 jest 单元测试不会在 VSCode 中达到断点
【发布时间】:2019-04-29 13:36:12
【问题描述】:

我试图调试使用 create-react-app/jest/enzyme 创建的测试,但无法在 VScode 中命中断点。当我运行调试“调试 CRA 测试”时,所有工作和测试都通过了,但它没有在 VSCode 中达到断点。

我的配置/设置中是否遗漏了什么?

import React from "react";
import { configure, shallow } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import Input from "./Input";

configure({ adapter: new Adapter() });

describe("<Input />", () => {

    it("should render <Input label=.../> with label", () => {
        const wrapper = shallow(<Input elementType="input" />);
        wrapper.setProps({ label: "test" });
        expect(wrapper.find("label")).toHaveLength(1);
    });

});

还有我的launch.json:

   {
        "name": "Debug CRA Tests",
        "type": "node",
        "request": "launch",
        "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
        "args": [
            "test",
            "--runInBand",
            "--no-cache"
        ],
        "cwd": "${workspaceRoot}",
        "protocol": "inspector",
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen"
    }

还有我的 package.json:

"dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.6",
    "@fortawesome/free-solid-svg-icons": "^5.4.1",
    "@fortawesome/react-fontawesome": "^0.1.3",
    "axios": "^0.18.0",
    "moment": "^2.22.2",
    "react": "^16.6.0",
    "react-dom": "^16.6.0",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.0.5",
    "react-swipeable": "^4.3.0",
    "redux": "^4.0.1",
    "redux-saga": "^0.16.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "enzyme": "^3.7.0",
    "enzyme-adapter-react-16": "^1.6.0",
    "node-sass": "^4.9.4"
  }

....

【问题讨论】:

  • 我认为这是一个持续存在的问题;检查#5846
  • 尝试仅在您要调试的文件中运行测试。在监视模式下,它是“p”,然后是文件名。

标签: reactjs debugging visual-studio-code jestjs create-react-app


【解决方案1】:

我在使用 create-react-apps 文档中指示的 vscode 启动配置时遇到了类似的问题。

它不起作用,因为 --no-watch 参数不起作用。 解决方案:使用另一种方式他们必须不使用手表模式:

"env": {
    "CI": "true"
}

完整配置工作(Node v8.12.0,react-scripts v2.1.3,Jest 23.6.0)

 {
  "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", "--no-watch"],
      "cwd": "${workspaceRoot}",
      "protocol": "inspector",
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "env": {
        "CI": "true"
      }
    }
  ]
}

这是他们在撰写本文时文档中所述的内容

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 2017-07-04
    • 2019-01-30
    • 2020-08-09
    • 2017-07-31
    • 2020-09-16
    • 2020-07-22
    • 2023-03-12
    • 2023-02-07
    相关资源
    最近更新 更多