简体   繁体   English

如何在激活断点的 Visual Studio Code 中调试 NodeJS start-server-and-test Jest 测试

[英]How to Debug NodeJS start-server-and-test Jest Tests in Visual Studio Code with Breakpoints Activating

Has anyone using start-server-and-test managed to debug their tests with breakpoints activated from within Visual Studio Code ?是否有人使用start-server-and-test设法使用从Visual Studio Code中激活的断点来调试他们的测试 I am using start-server-and-test to start a docker-compose stack.我正在使用start-server-and-test来启动docker-compose堆栈。 Once the docker-compose stack is successfully started the Jest tests start to run.一旦 docker-compose 堆栈成功启动,Jest 测试就会开始运行。

I can run this successfully within a terminal session.我可以在终端 session 中成功运行它。 However, I would like to trigger it from launch.json within VSCode if possible, with the ability to stop at breakpoints.但是,如果可能的话,我想从VSCode中的 launch.json 触发它,并能够在断点处停止。 I have tried with the following launch.json configuration item:我尝试了以下launch.json配置项:

 {
            "name": "Debug e2e tests",
            "type": "node",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "program": "${workspaceRoot}/node_modules/.bin/start-server-and-test",
            "args": [
                "docker-compose -f ./tests/docker/docker-compose.yml up",
                "http://localhost:8080/ping",
                "${workspaceRoot}/node_modules/.bin/jest -i --config=./tests/e2e/jest.config.js"
            ],
            "runtimeArgs": [
                "--inspect-brk"
            ],
            "protocol": "inspector",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
}

This starts the docker-compose stack and then the tests start running within an integrated terminal session in VSCode.这将启动 docker-compose 堆栈,然后测试开始在 VSCode 中的集成终端 session 中运行。 However, no breakpoints are being hit within VSCode.但是,在 VSCode 中没有遇到断点。

The following command is generated when VSCode runs the debugger: VSCode 运行调试器时会生成以下命令:

/usr/local/bin/node --inspect-brk=44745 node_modules/.bin/start-server-and-test "docker-compose -f ./tests/docker/docker-compose.yml up" http://localhost:8080/ping "/Users/simon/Development/Projects/ObjectDetection_Mqtt_Plugin/node_modules/.bin/jest -i --config=./tests/e2e/jest.config.js"

If I try to run the e2e test script from within a terminal session the tests start running, as expected, after the docker-compose stack has successfully started:如果我尝试从终端 session 中运行 e2e 测试脚本,则测试将在 docker-compose 堆栈成功启动后按预期开始运行:

# package.json extract to include script for running e2e tests
"scripts": {
     "test:e2e": "start-server-and-test 'docker-compose -f ./tests/docker/docker-compose.yml up' http://localhost:8080/ping 'jest --config=./tests/e2e/jest.config.js'",
}

# run the e2e tests 
yarn run test:e2e

Also tried this:也试过这个:

 {
            "name": "Debug node script",
            "type": "node",
            "request": "launch",
            "runtimeExecutable": "yarn",
            "runtimeArgs": [
                "run",
                "vscode"
            ],
            "restart": true,
            "port": 9229,
            "protocol": "inspector",
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "disableOptimisticBPs": true,
            "internalConsoleOptions": "neverOpen"
}

With this script:使用此脚本:

"vscode": "start-server-and-test 'docker-compose -f ./tests/docker/docker-compose.yml up' http://localhost:8080/ping 'node --inspect-brk ./node_modules/.bin/jest --config=./tests/e2e/jest.config.js'"

Same result...结果一样...

Got it working in the end with this setup:最终使用此设置使其正常工作:

 {
            "name": "Debug e2e tests",
            "type": "node",
            "request": "launch",
            "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/start-server-and-test",
            "runtimeArgs": [
                "docker-compose -f ./tests/docker/docker-compose.yml up",
                "http://localhost:8080/ping",
                "node --inspect-brk=9239 --title=Jest-e2e-tests ${workspaceRoot}/node_modules/.bin/jest -i --config=./tests/e2e/jest.config.js"
            ],
            "port": 9239,
            "protocol": "inspector",
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "disableOptimisticBPs": true
}

My approach was to set the runtimeExecutable as start-server-and-test and initialise the runtimeArgs array with the arguments for start-server-and-test.我的方法是将runtimeExecutable设置为 start-server-and-test 并使用 arguments 初始化runtimeArgs数组以进行 start-server-and-test。 The test argument for start-server-and-test spawns a node process with the --inspect-brk=9239 argument to allow the VSCode debugger to attach. start-server-and-test的 test 参数使用--inspect-brk=9239参数生成一个节点进程,以允许附加 VSCode 调试器。 Additionally this required explictly setting the port VSCode debugger option.此外,这需要明确设置port VSCode 调试器选项。

If child processes are spawned by end to end tests then will need to add the autoAttachChildProcesses option and ensure that the code that forks the child process detects if running in inspect mode.如果子进程是由端到端测试产生的,则需要添加autoAttachChildProcesses选项并确保派生子进程的代码检测是否在检查模式下运行。 If it is then it will add inspect argument set with a different port to the parent so no conflict occurs.如果是,那么它将向父级添加具有不同端口的检查参数集,因此不会发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sh:1:启动服务器和测试:权限被拒绝 - sh: 1: start-server-and-test: Permission denied 如何在Visual Studio Code中调试从Grunt运行的Jasmine测试? - How to debug Jasmine tests run from Grunt in Visual Studio Code? 调试mocha测试时,Visual Studio代码中的断点未被命中 - Breakpoints in Visual Studio Code not hit when debugging mocha tests 如何在 Visual Studio 代码中调试 nodeJS (ExpressJS) 应用程序? - How to debug nodeJS (ExpressJS) app in visual studio code? start-server-and-test 启动测试服务器后不启动测试用例 - start-server-and-test not starting test cases after starting the test server 无法在Visual Studio代码中调试Node.js应用程序 - Unable to debug the Nodejs application in Visual studio code 在Visual Studio代码中调试输出nodejs - Debug output nodejs in Visual Studio Code 启动服务器和测试卡在服务器启动时 - (错误:等待超时) - start-server-and-test stuck at server start - ( Error: Timed out waiting ) 如何使用 Visual Studio 代码调试 javascript 中的 mocha 测试? - How to debug with visual studio code a mocha test in javascript? 如何在Visual Studio代码编辑器中确定群集服务器上的调试模式 - How to determine Debug mode on a Clustered Server in Visual Studio Code Editor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM