简体   繁体   English

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

[英]How to reverse backslashes in ${relativeFile} in Visual Studio Code in launch.json?

I'm trying to configure (Windows) Visual Studio Code launch.json to launch jest tests for the current file.我正在尝试配置 (Windows) Visual Studio Code launch.json以启动当前文件的jest测试。 To get the path I use ${relativeFile} variable which gives a string with backslashes like this "src\\services\\some-service.spec.ts" , although in the documentation slashes look just normal.为了获取路径,我使用了${relativeFile}变量,它给出了一个带有反斜杠的字符串,例如"src\\services\\some-service.spec.ts" ,尽管在 文档中斜杠看起来很正常。

It seems that jest doesn't accept this kind of path because of reversed slashed.似乎jest不接受这种路径,因为反向斜线。 When I manually pass the same path with normal slashes it works just fine.当我用普通斜杠手动传递相同的路径时,它工作得很好。

The question is is there any way to reverse backslashes in VSCode path predefined variables like ${relativeFile} or maybe some workarounds?问题是有没有办法在 VSCode 路径预定义变量(如${relativeFile}或一些解决方法)中反转反斜杠?

[Update] 2020-05-01 [更新] 2020-05-01

Now jest cli supports option --runTestsByPath , so that you can explicitly specify a file path rather than a pattern, which allows you to use \\ on windows.现在 jest cli 支持选项--runTestsByPath ,以便您可以明确指定文件路径而不是模式,这允许您在 Windows 上使用\\

So the following launch.json should work:所以下面的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}"
    }
  ]
}

The following is the original answer:以下为原答案:


It seems that jest doesn't plan to work with \\ and vscode doesn't plan to provide features to replace chars in Predefined variables.似乎 jest 不打算使用\\并且 vscode 不打算提供替换预定义变量中的字符的功能。

But there are some workarounds:但是有一些解决方法:

  1. use ${fileBasename} instead of ${relativeFile}使用${fileBasename}而不是${relativeFile}

  2. Or use input variables so that vscode prompt you to input custom test name when you debug.或者使用输入变量,让vscode在调试时提示你输入自定义测试名称。

Here is an example launch.json for the above two workarounds.以下是上述两种解决方法的示例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}"
    }
  ]
}

Use variable extension.commandvariable.file.relativeFilePosix from extension command-variable where you need ${relativeFile} with forward slashes.使用扩展命令变量中的变量extension.commandvariable.file.relativeFilePosix ,您需要带正斜杠的${relativeFile} There are also other useful substitutions in this extension.此扩展中还有其他有用的替换。

This problem is very similar to one I gave an answer to elsewhere .这个问题与我在别处回答过的问题非常相似。 To summarize the explanation there:总结一下那里的解释:

  1. Instead of directly running node ... , run bash -c node ... so you can use shell command substitution in the command line.不是直接运行node ... ,而是运行bash -c node ...以便您可以在命令行中使用 shell 命令替换。
  2. Pass the path through the cygpath -m program to get forward slashes, something like $(cygpath -m ${relativeFile}) .通过cygpath -m程序传递路径以获取正斜杠,例如$(cygpath -m ${relativeFile})

See the linked answer for more details and examples.有关更多详细信息和示例,请参阅链接的答案。

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

相关问题 在 Visual Studio Code 中,如何在 launch.json 中传递 arguments - In Visual Studio Code, how to pass arguments in launch.json Visual Studio Code - launch.json 和调试器卡在 Python 上 - Visual Studio Code - launch.json and debugger is stuck on Python 运行工头从视觉工作室代码launch.json开始 - Running foreman start through visual studio code launch.json 在 Visual Studio Code 中启用详细调试以启动.json - Enable verbose debugging to launch.json in Visual Studio Code “。”(点)在launch.json的Visual Studio代码“ runtimeArgs”属性中做什么 - What does “.” (Dot) do in Visual Studio Code “runtimeArgs” property of launch.json 无法获取Visual Studio代码在launch.json中运行我的构建命令 - Can't get Visual Studio Code to run my build command in launch.json VS 代码调试器,如何在 launch.json 中使用带有命令行选项的节点来启动脚本 - VS Code-debugger, How to use node with command line option in launch.json for starting a script 在 VS Code-debugger 中,如何在 launch.json 中为 nodejs 使用 envFile? - In VS Code-debugger, how do I use envFile in launch.json for nodejs? 如何在 VSCode 中向 launch.json 添加环境变量 - How do I add environment variables to launch.json in VSCode 运行启动时找不到模块“/Applications/Visual”。json 配置 - Cannot find module '"/Applications/Visual' when running launch.json config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM