如何在launch.json的Visual Studio Code中反转${relativeFile}中的反斜杠?

Ser*_*gey 3 configuration node.js jestjs visual-studio-code

我正在尝试配置 (Windows) Visual Studio Codelaunch.json来启动jest当前文件的测试。为了获取路径,我使用${relativeFile}了一个变量,它给出了一个带有反斜杠的字符串"src\services\some-service.spec.ts",尽管在文档中斜杠看起来很正常。

jest由于反向斜线,似乎不接受这种路径。当我用普通斜杠手动传递相同的路径时,它工作得很好。

问题是有没有办法在 VSCode 路径预定义变量中反转反斜杠,比如${relativeFile}或者一些解决方法?

Equ*_*ual 5

[更新] 2020-05-01

现在 jest cli 支持选项 --runTestsByPath,以便您可以明确指定文件路径而不是模式,这允许您\在 Windows上使用。

所以以下 launch.json应该工作:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Jest Current File",
      "type": "node",
      "request": "launch",
      "args": [
        "node_modules/jest/bin/jest.js",
        "--runInBand",
        "--runTestsByPath",
        "${relativeFile}"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "cwd": "${workspaceFolder}"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

以下为原答案:


似乎 jest 不打算与 \并且 vscode 不打算提供替换预定义变量中的字符的功能。

但是有一些解决方法:

  1. 使用${fileBasename}代替${relativeFile}

  2. 或者使用输入变量,让vscode在调试时提示你输入自定义测试名称。

以下是launch.json上述两种解决方法的示例。

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Jest Current FileBaseName",
      "type": "node",
      "request": "launch",
      "args": [
        "node_modules/jest/bin/jest.js",
        "--runInBand",
        "${fileBasename}"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "cwd": "${workspaceRoot}"
    },
    {
      "name": "Jest Custom",
      "type": "node",
      "request": "launch",
      "args": [
        "node_modules/jest/bin/jest.js",
        "--runInBand",
        "${input:testName}"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "cwd": "${workspaceRoot}"
    }
  ],
  "inputs": [
    {
      "type": "promptString",
      "id": "testName",
      "description": "The file or name you want to test",
      "default": "${fileBaseName}"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)