Node.js nnpm测试在命令行上运行。这个让我有点困惑。注意这里,以防其他人也感到困惑
编辑:VS Code 1.9.0在调试配置下拉列表中添加了“添加配置”选项,您可以选择“Node.js Mocha Tests”,这有助于简化上述大部分内容。您仍然需要确保mocha位于Node.js nnpm测试在命令行上运行。这个让我有点困惑。注意这里,以防其他人也感到困惑,node.js,visual-studio-code,mocha.js,tdd,Node.js,Visual Studio Code,Mocha.js,Tdd,编辑:VS Code 1.9.0在调试配置下拉列表中添加了“添加配置”选项,您可以选择“Node.js Mocha Tests”,这有助于简化上述大部分内容。您仍然需要确保mocha位于节点\u模块中,并且可能需要更新cwd和最后一个运行时参数(这是查找测试的模式)以指向适当的路径。但是一旦你设置了这两个属性,它就可以从那里开始工作了。这对我来说是在Windows7机器上工作的。我确实在全球安装了mocha,但此配置指向项目安装,以避免需要用户配置文件路径(顺便说一句,我尝试使用%USERPRO
节点\u模块中,并且可能需要更新cwd
和最后一个运行时参数(这是查找测试的模式)以指向适当的路径。但是一旦你设置了这两个属性,它就可以从那里开始工作了。这对我来说是在Windows7机器上工作的。我确实在全球安装了mocha,但此配置指向项目安装,以避免需要用户配置文件路径(顺便说一句,我尝试使用%USERPROFILE%变量,但没有成功)。我现在可以在我的摩卡测试中设置断点了。耶
{
"name": "Mocha Tests",
"type": "node",
"request": "launch",
"stopOnEntry": false,
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"cwd": "${workspaceRoot}",
"args": ["./test/**/*.js"],
"runtimeExecutable": null,
"envFile": "${workspaceRoot}/.env"
}
对于那些使用grunt或gulp的用户,配置非常简单
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Run mocha by grunt",
"type": "node",
"program": "${workspaceRoot}/node_modules/grunt/bin/grunt",
"stopOnEntry": false,
"args": ["mochaTest"],
"cwd": "${workspaceRoot}",
"runtimeExecutable": null
}
]}
Grunfile.js
module.exports = function (grunt) {
grunt.initConfig({
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: ['test/**/*test.js']
}
}
});
grunt.loadNpmTasks('grunt-mocha-test');
grunt.registerTask('default', 'mochaTest');};
在VSCode版本1.13.0(macOS)中,他们在配置->Mocha测试中内置了它 您知道吗,您只需进入启动配置,将光标放在其他配置之后或之间,然后按ctrl空格键即可自动生成当前有效的摩卡配置?
这对我来说非常好。包括在断点处停止。
(我还有一个以前的,现在已经过时的,由于各种与设置相关的原因,它不再存在。)
截至VSCode 1.21.1(2018年3月),这将产生:
{
"version": "0.2.0",
"configurations": [
{
"name": "Mocha (Test single file)",
"type": "node",
"request": "launch",
"runtimeArgs": [
"${workspaceRoot}/node_modules/.bin/mocha",
"--inspect-brk",
"${relativeFile}",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
}
另请注意:debug brk
(至少适用于节点>=版本8的用户)。在launch.json中,在下面添加1个配置
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"10000",
"${workspaceRoot}/services/*.spec.js",
"${workspaceRoot}/*.spec.js"
],
"internalConsoleOptions": "openOnSessionStart"
},
如果需要配置节点版本,只需像下面这样添加runtimeExecutable
字段
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"10000",
"${workspaceRoot}/services/*.spec.js",
"${workspaceRoot}/*.spec.js"
],
"internalConsoleOptions": "openOnSessionStart",
"runtimeExecutable": "${env:HOME}/.nvm/versions/node/v8.2.1/bin/node"
},
如果在args列表末尾添加${file}变量,则可以直接从打开的文件开始调试:
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${file}"
],
"internalConsoleOptions": "openOnSessionStart"
}
进入Debug>addconfiguration…
菜单
选择Node.js
environment
从出现的下拉列表中选择Mocha测试
选项
键入测试文件的路径作为args
属性的最后一项
添加一个断点
单击Debug
图标
选择摩卡咖啡测试作为配置
按开始调试
按钮
:-)
使用Babel或生成javascript文件但在源代码中放置断点时,必须确保启用sourceMaps
并定义outFiles
。下面是一个对我有用的配置示例
{
"name": "Mocha Test",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/packages/api/node_modules/mocha/bin/_mocha",
"cwd": "${workspaceRoot}/packages/api",
"args": ["--colors", "--no-timeouts", "out/test"],
"outFiles": ["${workspaceRoot}/packages/api/out/*"],
"sourceMaps": true,
},
注意-您需要修改outFiles
,以包括可能要添加断点的所有内容。在单一回购和多个相关项目中,这可能会更加繁琐。1)转到
.vscode
然后
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Run mocha by grunt",
"type": "node",
"program": "${workspaceRoot}/node_modules/grunt/bin/grunt",
"stopOnEntry": false,
"args": ["mochaTest"],
"cwd": "${workspaceRoot}",
"runtimeExecutable": null
}
]}
文件
2) 在launch.json中添加以下配置-
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Test",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/*folder_path_containing_test*/node_modules/.bin/mocha",
"windows": {
"runtimeExecutable": "${workspaceRoot}/*folder_path_containing_test*/node_modules/.bin/mocha.cmd"
},
"runtimeArgs": [
"--colors",
"--recursive",
"${workspaceRoot}/*folder_path_till_test*/tests"
],
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/*folder_path_to_test*/app.js"
}
]
}
3) 在测试文件中设置断点,然后按F5键。如果您在测试中有一些依赖项,那么附加它也很容易
例如,我正在使用mongo unit helper
将单元测试与数据库集成
package.json
脚本是:mocha--recursive--require./test/mongo-unit-helper.js--exit“
我的launch.json
看起来像:
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"--recursive",
"--require",
"${workspaceFolder}/test/mongo-unit-helper.js",
"${workspaceFolder}/test/**/*.js",
],
"internalConsoleOptions": "openOnSessionStart"
}
]
解决方案是将--require
分别放在args
的launch.json
最简单的解决方案中
将以下代码添加到.vscode文件夹内的launch.json中:
{
"name": "Unit tests",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
],
}
但是,您可能还需要添加一个超时参数:
{
"name": "Unit tests",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999"
],
}
这种方法简单得多,实际上没有任何配置,您必须添加“请求”:“附加”“
发送到launch.json,如果它不存在-否则它会抱怨您必须指定一个程序或其他错误。这似乎是VS code
特定的。不适用于普通VS 2015great建议谢谢:)注意,--debug brk
是,这就是为什么我建议,是的,也是专门针对摩卡的。不适用于我(在windows上)-但如果它有效,似乎是一个很好的解决方案:)是的。抱歉OpenDebug进程意外终止
能否尝试将“runtimeExecutable”
设置为“C:/Program Files/nodejs/node.exe”
或安装节点的任何位置?当然可以-但没有更改。我不使用Windows,因此无法进一步帮助。但是,请注意-他们正在讨论这个OpenDebug问题。对我不起作用,它找不到测试文件,路径是正确的,我也尝试了完整路径。这对我也适用vscode 0.10.6。对于.ts文件中的断点,对于sourcemaps,我确实在我的启动配置中添加了“sourcemaps”:true,“outDir”:“./build”
。对于其他有问题的人。注意摩卡咖啡,而不是摩卡咖啡。仅使用mocha,它将在VS代码中运行测试,但对于那些使用TypeScript的人来说,断点不会被命中,只要您设置sourceMaps:true
,这是一个合适的答案。非常感谢!要添加与npm兼容的自定义测试参数,请将类似于npm_config_myparam
的内容添加到env块。在CLI上,它可能看起来像npm--myparam=myvalue test
。这让我陷入了困境。我错误地使用了“which mocha”的路径,而不是node_模块。谢谢在Windows中也是一样。我需要一些自定义代码来初始化文档和禁用热模块替换。在“args”
块中传递此参数:“--require”、“${workspaceFolder}/tools/testSetup.js”
VS code 1.29.1:Ctrl+Space
自动生成的摩卡测试配置没有调试brk
。尽管使用断点进行调试工作正常。@Antony是的,长期以来,debug brk
不再需要使用、支持或自动插入。我的旁注只是澄清了这一点,
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"10000",
"${workspaceRoot}/services/*.spec.js",
"${workspaceRoot}/*.spec.js"
],
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"10000",
"${workspaceRoot}/services/*.spec.js",
"${workspaceRoot}/*.spec.js"
],
"internalConsoleOptions": "openOnSessionStart",
"runtimeExecutable": "${env:HOME}/.nvm/versions/node/v8.2.1/bin/node"
},
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${file}"
],
"internalConsoleOptions": "openOnSessionStart"
}
{
"name": "Mocha Test",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/packages/api/node_modules/mocha/bin/_mocha",
"cwd": "${workspaceRoot}/packages/api",
"args": ["--colors", "--no-timeouts", "out/test"],
"outFiles": ["${workspaceRoot}/packages/api/out/*"],
"sourceMaps": true,
},
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Test",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/*folder_path_containing_test*/node_modules/.bin/mocha",
"windows": {
"runtimeExecutable": "${workspaceRoot}/*folder_path_containing_test*/node_modules/.bin/mocha.cmd"
},
"runtimeArgs": [
"--colors",
"--recursive",
"${workspaceRoot}/*folder_path_till_test*/tests"
],
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/*folder_path_to_test*/app.js"
}
]
}
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"--recursive",
"--require",
"${workspaceFolder}/test/mongo-unit-helper.js",
"${workspaceFolder}/test/**/*.js",
],
"internalConsoleOptions": "openOnSessionStart"
}
]
{
"name": "Unit tests",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
],
}
{
"name": "Unit tests",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999"
],
}