【问题标题】:Unable to attach a debugger while running jest tests运行笑话测试时无法附加调试器
【发布时间】:2021-05-17 18:33:36
【问题描述】:

我无法在运行笑话测试时附加 VS Code 调试器。我尝试使用我找到的不同替代方案配置 launch.json 文件,但它们总是失败并显示以下错误消息,抱怨 node_modules 文件夹中的文件:

Debugger attached.
Waiting for the debugger to disconnect...
local\path\to\node_modules\.bin\jest:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)      
    at internal/main/run_main_module.js:17:47

我的package.json 文件有以下配置:

  "scripts": {
    "compile": "tsc",
    "run": "node ./build/index.js",
    "execute": "tsc && node ./build/index.js",
    "test": "tsc && jest ./build/tests/*"
  },
  "author": "roguib",
  "license": "ISC",
  "dependencies": {
    "@types/jest": "^26.0.20",
    "ts-jest": "^26.5.1",
    "ts-node": "^9.1.1"
  },
  "devDependencies": {
    "@types/node": "^14.14.21",
    "jest": "^26.6.3"
  }

我的launch.json

{
    "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"
        }
    ]
}

source

如果有帮助,执行 npm run test 通常会运行我的 jest 测试,所以我不确定它是否与 jest 本身有关。

【问题讨论】:

    标签: node.js typescript jestjs ts-jest ts-node


    【解决方案1】:

    windows上好像有bug,解决方法是直接将VS Code指向jest.js而不是.bin版本:

    所以尝试替换这个:

    "runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/.bin/jest", "--runInBand"], 类似于:

    "runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/jest/bin/jest.js", "--runInBand", "--coverage", "false"],

    https://github.com/facebook/jest/issues/3750

    另外,如果您收到关于 jest 配置的另一个错误,请添加 -c arg:

      "args": [
                  "--runInBand",
                  "-c", "${workspaceFolder}/package/jest.config.js",
                ],
    

    【讨论】:

    • 你是我的救星!
    【解决方案2】:

    我通过安装 ts-jest 并在launch.json 中使用以下配置解决了这个问题。

    {
      "type": "node",
      "request": "launch",
      "name": "Jest All",
      "program": "${workspaceFolder}/node_modules/.bin/jest",
      "args": [
        "--runInBand"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "disableOptimisticBPs": true,
      "windows": {
        "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      }
    },
    

    Source.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      相关资源
      最近更新 更多