问题:
在Windows下使用VS Code时,无法对当前(活动)文件进行jest测试,仅限于此文件。
这是从https://github.com/microsoft/vscode-recipes/tree/master/debugging-jest-tests中获取的调试配置:
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"${fileBasenameNoExtension}",
"--config",
"jest.config.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
}
如果你有几个同名测试文件(比如说 index.test.js),那么它不会仅运行当前活动的测试用例。
如果我替换
"args": [
"${fileBasenameNoExtension}",
....
]
通过
"args": [
"${file}",
...
]
作为第一个参数,jest期望的是正则表达式,而
${file}
返回文件的绝对路径,在Windows机器上,它将包含反斜杠\
,这将被解释为模式中后面要转义的字符,因此根本不起作用。仔细阅读Jest文档后,不可能指定单个文件。
而VSCode无法将变量转换为正则表达式。 this question的解决方案几乎相似,但也行不通,因为它在名称相同的文件(情况1)中失败了。
因此,要么运行的测试比预期的更多,要么根本没有运行测试。
我已经找到了一种涉及单独脚本的解决方案,但欢迎任何其他解决方案。