调试mocha测试时,Visual Studio代码中的断点未被命中

时间:2016-12-30 15:59:42

标签: node.js mocha visual-studio-code

我正在使用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目录包含3个包含的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;命令时断点开始工作,并在停止后设置至少一个新断点 / strong> on debugger;。之后,此单个文件中的所有后续断点都按预期工作。看起来几乎像是我的错误。

1 个答案:

答案 0 :(得分:2)

在启动选项中使用"protocol": "inspector",帮助我继续了一段时间,尽管这有一个恼人的副作用,测试过程在执行完所有后从未停止过。我不得不在每次运行后杀死任务。所以我虽然试着再试一次,但我成功了。解决方案很简单:在启动选项中添加outfiles选项,否则vscode将在TS源文件夹中查找地图。添加:

        "outFiles": [
            "${workspaceRoot}/out/**/*.js"
        ],
一切都开始好起来了。如果vscode会因为缺少设置而打印出无法找到源地图的警告,那将非常有帮助。