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

2023-12-14

我正在尝试配置 (Windows) Visual Studio Codelaunch.json推出jest测试当前文件。获取我使用的路径${relativeFile}变量给出一个带有反斜杠的字符串,如下所示"src\services\some-service.spec.ts",虽然在文档斜线看起来很正常。

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

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


[更新]2020-05-01

现在 jest cli 支持选项--按路径运行测试,这样您就可以显式指定文件路径而不是模式,这允许您使用\在窗户上。

所以下面的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}"
    }
  ]
}

以下是原答案:


看来 jest 不打算合作\并且 vscode 不打算提供替换预定义变量中字符的功能。

但有一些解决方法:

  1. use ${fileBasename}代替${relativeFile}

  2. Or use 输入变量这样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}"
    }
  ]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐