eggjs / egg

🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
https://eggjs.org
MIT License
18.9k stars 1.82k forks source link

vscode 怎么调试单元测试 #839

Closed shamoq closed 7 years ago

shamoq commented 7 years ago

请问,从单元测试能进到代码里的断点吗?

.launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Egg",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "runtimeExecutable": "npm",
      "windows": {
        "runtimeExecutable": "npm.cmd"
      },
      "runtimeArgs": [
        "run", "test", "--", "--debug"
      ],
      "port": 5858
    },
    {
      "name": "Attach Agent",
      "type": "node",
      "request": "attach",
      "port": 5856
    },
    {
      "name": "Attach Worker",
      "type": "node",
      "request": "attach",
      "restart": true,
      "port": 10086
    }
  ],
  "compounds": [
    {
      "name": "Debug Egg",
      "configurations": ["Launch Egg", "Attach Agent", "Attach Worker"]
    }
  ]
}

自己尝试把runtimeArgs里的dev修改为test,可以从vscode里运行单测,会出现如下提示:

无法通过 "legacy" 协议连接到运行时;请考虑采用 "inspector" 协议(10000 毫秒后超时)。

dev模式,是可以直接从vscode调试进入断点的,就是不太清楚怎么通过单测进入断点。

atian25 commented 7 years ago

单元测试的时候,如果是用 mm.app() 启动的,实际上只是单进程,所以无需 egg-development-proxyworker,直接加一个常规的 configuration 连接默认端口即可。

但这里问题是被 power-assert 转码后的代码,vscode 能断点到首行,但是在源码里面打的点无法断到, WebStorm 我记得是可以的。

image

/cc @shepherdwind @okoala

okoala commented 7 years ago

我看了下,VSCode 调试的时候拿到的文件路径是错误了,无法定位到原文件。需要看看怎么搞~

下面vscode中打印出的日志:

SourceMaps.getMapForGeneratedPath: Using inlined sourcemap in d:\Code\github\egg-example\test\app\controller\home.test.js
SourceMap: creating for d:\Code\github\egg-example\test\app\controller\home.test.js
SourceMap: sourceRoot: undefined
SourceMap: sources: ["test\\app\\controller\\home.test.js"]
SourceMap: no sourceRoot specified, using script dirname: d:\Code\github\egg-example\test\app\controller
Warning: sourceMapPathOverrides entry does not map to an absolute path - "webpack:///./*": "${cwd}/*"
Warning: sourceMapPathOverrides entry does not map to an absolute path - "webpack:///*": "*"
Warning: sourceMapPathOverrides entry does not map to an absolute path - "meteor://💻app/*": "${cwd}/*"
SourceMaps.scriptParsed: d:\Code\github\egg-example\test\app\controller\home.test.js was just loaded and has mapped sources: ["d:\\Code\\github\\egg-example\\test\\app\\controller\\test\\app\\controller\\home.test.js"]
SourceMaps.setBP: Mapped d:\Code\github\egg-example\test\app\controller\test\app\controller\home.test.js to d:\Code\github\egg-example\test\app\controller\home.test.js
SourceMaps.setBP: Mapped d:\Code\github\egg-example\test\app\controller\test\app\controller\home.test.js:7:1 to d:\Code\github\egg-example\test\app\controller\home.test.js:37:9
SourceMaps.setBP: Mapped d:\Code\github\egg-example\test\app\controller\test\app\controller\home.test.js:14:1 to d:\Code\github\egg-example\test\app\controller\home.test.js:54:9
SourceMaps.setBP: SourceMaps are enabled but d:\Code\github\egg-example\test\app\controller\home.test.js is a runtime script
SourceMaps.setBP: Mapped d:\Code\github\egg-example\test\app\controller\home.test.js:37:9 to d:\Code\github\egg-example\test\app\controller\test\app\controller\home.test.js:7
SourceMaps.setBP: Mapped d:\Code\github\egg-example\test\app\controller\home.test.js:54:9 to d:\Code\github\egg-example\test\app\controller\test\app\controller\home.test.js:14

路径中有段是重复的,之前也出现了类似的情况~

看了下 sourceMap 提示要设置 sourceRoot

okoala commented 7 years ago

我本地用 inspector 协议已经正常断点了

image

但是用 旧的协议 会报错

#
# Fatal error in runtime\runtime-debug.cc, line 1424
# Check failed: args[0]->IsJSObject().
#

==== C stack trace ===============================

        v8::internal::operator<< [0x00007FF63913A606+42646]
        v8::internal::AllocationSpaceName [0x00007FF63907BA12+430498]
        (No symbol) [0x0000015EF59843AB]
⚠️ run command --debug-brk got error: D:\Code\github\egg-example\node_modules\_mocha@3.3.0@mocha\bin\_mocha --require=D:\Code\github\egg-example\
node_modules\_co-mocha@1.2.0@co-mocha\lib\co-mocha.js,--require=D:\Code\github\egg-example\node_modules\_intelli-espower-loader@1.0.1@intelli-espower-loader\intelli-espower-loader.js,--no-timeouts,D:\Code\github\egg-example\test\.setup.js,test/app/controller/home.test.js exit with code 3221225477
Error: D:\Code\github\egg-example\node_modules\_mocha@3.3.0@mocha\bin\_mocha --require=D:\Code\github\egg-example\node_modules\_co-mocha@1.2.0@co-mocha\lib\co-mocha.js,--require=D:\Code\github\egg-example\node_modules\_intelli-espower-loader@1.0.1@intelli-espower-loader\intelli-espower-loader.js,--no-timeouts,D:\Code\github\egg-example\test\.setup.js,test/app/controller/home.test.js exit with code 3221225477
    at ChildProcess.proc.once.code (D:\Code\github\egg-example\node_modules\_common-bin@2.3.0@common-bin\lib\helper.js:56:21)
    at Object.onceWrapper (events.js:293:19)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:194:7)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
Rwing commented 7 years ago

关注,typescript 呢?

shamoq commented 7 years ago

@okoala 能看下你的配置吗,是需要修改哪些地方.

okoala commented 7 years ago

@a304885433 你可以看下我引用 espower source 过来的那个链接

shamoq commented 7 years ago

@atian25 @okoala 看了下,还是没看懂不知道改那里,lanch里又是怎么配置。

okoala commented 7 years ago

@a304885433 改了 espower-source 的代码,等OK之后,我会更新使用步骤~

shamoq commented 7 years ago

@okoala 嗯,那就等消息了,期待

popomore commented 7 years ago

已经发布了,试试

@okoala 要不要在 example 里写一个 vscode 的例子

okoala commented 7 years ago

@a304885433 我本地试已经ok了, 你先 rm 掉 node_module 然后重新安装依赖。

启动 test:

npm run test-local -- --inspect-brk

launch.json 配置:

{
      "name": "Attach Test",
      "type": "node",
      "protocol": "inspector",
      "request": "attach",
      "timeout": 10000,
      "restart": true,
      "port": 9229
}

然后就可以进行各种断点调试,app 和 test 下断点都ok.

okoala commented 7 years ago

@popomore 这个可以有~

atian25 commented 7 years ago

插件里面加一个 install.js 用来自动生成吧,如果不存在时

发自我的 iPhone

在 2017年5月5日,11:52,仙森 notifications@github.com 写道:

@popomore 这个可以有~

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

shamoq commented 7 years ago

@okoala 感谢你的帮助,现在是可以调试了。调试能不能直接从vscode里进去呀,现在在终端敲个命令,vscode在去附加进程调试,感觉少许麻烦。我尝试照着原来的配置修改里一下lanch.json,可以运行调试,但是调试结束后,陆续报几个错误出来。以下是我的写法:

   {
      "name": "Launch Test",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "protocol": "inspector",
      "runtimeExecutable": "npm",
      "windows": {
        "runtimeExecutable": "npm.cmd"
      },
      "runtimeArgs": [
        "run",
        "test-local",
        "--",
        "--inspect-brk"
      ],
      "port": 5858
    },
    {
      "name": "Attach Test",
      "type": "node",
      "protocol": "inspector",
      "request": "attach",
      "timeout": 10000,
      "restart": true,
      "port": 9229
    }

    {
      "name": "Test Egg",
      "configurations": [
        "Launch Test",
        "Attach Test"
      ]
    }
okoala commented 7 years ago

@a304885433 调试完进程就退出了啊~

okoala commented 7 years ago

@atian25 好~

shamoq commented 7 years ago

@okoala 嗯,调试完成后是自动退出,不知道vscode是不是还在干什么,会提示如下3个错误。能不能把下面的错误提示干掉。

Cannot read property 'toLowerCase' of undefined

Ensure Node was launched with --inspect. Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:9229).

Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:5858).
tong3jie commented 7 years ago

@a304885433 遇到跟你一样的错误。

已经好几天了,没有调试下去的欲望了。

okoala commented 7 years ago

@tong3jie 你是陆陆续续出现这样的错误,还是一直出现?

AkashiX commented 7 years ago

请问问题解决了没?我的是mac vscode出现问题,配置也是这个。

shen-lan commented 5 years ago

调试如何忽略进入node_module文件夹?

atian25 commented 5 years ago

调试如何忽略进入node_module文件夹?

可以去看看 VSCode 自己的 Debug 文档,配置下 skip 即可