[更新]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}"
}
]
}
以下是原答案:
看来 jest 不打算与 \ 一起工作,vscode 也不打算提供替换预定义变量中的字符的功能。
但是有一些解决方法:
使用${fileBasename} 而不是${relativeFile}
或者使用input variables,让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}"
}
]
}