【发布时间】:2020-01-16 20:10:04
【问题描述】:
如何在当前打开的文件上以调试模式启动 jest,只有这个,无论操作系统(windows、linux、mac)如何。
问题:
windows下使用vscode时,无法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)。
所以要么运行比预期更多的测试,要么根本不运行任何测试。
我找到了一个涉及单独脚本的解决方案,但任何其他解决方案都将不胜感激。
【问题讨论】:
标签: windows visual-studio-code jestjs