我将Mocha(和Chai)用于NodeJS模块的单元测试,并希望在Visual Studio代码中对其进行调试。我在test
子文件夹中有一个TypeScript文件,并进行了一些测试。VScode在out dir中生成.js和.map文件(通过tsc watch模式任务)。我的tsconfig.json文件包含以下设置:
{
"compilerOptions": {
"compileOnSave": true,
"module": "commonjs",
"target": "es6",
"outDir": "out",
"removeComments": true,
"noImplicitAny": true,
"sourceMap": true,
"inlineSources": true,
"isolatedModules": false,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true
},
"include": [
"src/**/*", "parser/**/*", "test/**/*"
],
"exclude": [
"node_modules",
".vscode-test"
]
}
并且out dir包含3个include的3个子目录。到目前为止一切都很好。
我可以使用以下命令运行测试:
mocha --compilers ts:ts-node/register,tsx:ts-node/register
在vscode之外。然后,我使用--debug-brk
开关运行此代码,并将vscode附加到该代码。这行得通,但是没有命中断点。在launch.json中的配置是:
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": true,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
}
理想情况下,我希望有一个运行配置,因此不需要手动运行mocha。使用这些设置,我至少可以运行测试:
{
"name": "Mocha",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"preLaunchTask": "tsc",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [ "--no-timeouts", "--colors", "${workspaceRoot}/out/test/**/*.js" ],
"stopOnEntry": true,
"runtimeExecutable": null,
"env": {
"NODE_ENV": "testing"
}
"sourceMaps": true
}
但仍然没有命中断点。
使这两种方案中的至少一种起作用需要什么?
更新:同时我发现偶然的断点开始工作时加debugger;
在测试代码命令的地方,并至少设置一个断点清新它停止后上debugger;
。之后,该单个文件中的所有后续断点将按预期工作。在我看来几乎像个虫子。
使用"protocol": "inspector",
在启动选项帮助我持续一段时间,即使这有恼人的副作用被执行后,所有的测试过程从来没有停止过。每次运行后,我必须终止任务。因此,尽管我再次尝试查找问题,但我成功了。解决方案很简单:将outfiles
选项添加到启动选项中,否则vscode将在TS源文件夹中查找映射。通过添加:
"outFiles": [
"${workspaceRoot}/out/**/*.js"
],
一切开始顺利进行。如果vscode会显示一条警告,指出由于缺少此设置而无法找到源映射,这将非常有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句