[英]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}
或一些解决方法)中反转反斜杠?
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}"
}
]
}
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:但是有一些解决方法:
use ${fileBasename}
instead of ${relativeFile}
使用
${fileBasename}
而不是${relativeFile}
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:
总结一下那里的解释:
node ...
, run bash -c node ...
so you can use shell command substitution in the command line.node ...
,而是运行bash -c node ...
以便您可以在命令行中使用 shell 命令替换。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.