简体   繁体   English

如何在Jest中使用node debug cli?

[英]How do I use node debug cli with Jest?

How can I just use the simple node cli/repl debugger with Jest? 我如何才能在Jest中使用简单的节点cli / repl调试器?

The Jest documentation uses node-inspector , but it is outdated/deprecated as of Node 6.3. Jest文档使用node-inspector ,但是从Node 6.3开始已经过时/过时。 I tried the recommended command anyway on Node 7.7.4: 无论如何,我在节点7.7.4上尝试了推荐的命令:

node --debug-brk ./node_modules/.bin/jest --runInBand --no-cache [your_test_file]

But this simply hangs on the following (assumedly waiting on node-inspector): 但这只是挂在以下内容上(假定正在等待node-inspector):

(node:13452) DeprecationWarning: node --debug is deprecated. Please use node --inspect instead. Debugger listening on 127.0.0.1:5858

I added --inspect as specified by the warning, but even then execution doesn't stop on my debugger statement in Chrome DevTools. --inspect按照警告所指定的方式添加了--inspect ,但即使如此,执行仍不会在Chrome DevTools中的debugger语句上停止。

This seems overly complicated for a very simple use case. 对于非常简单的用例而言,这似乎过于复杂。

I found the following command works: 我发现以下命令有效:

node debug ./node_modules/.bin/jest --runInBand --no-cache [your_test_file]

...but with some quirky behavior. ...但行为有些古怪。 When the debugger first stops you will see: 调试器首次停止时,您将看到:

break in node_modules/jest/bin/jest.js:10
  8  */
  9
>10 'use strict';
 11
 12 require('jest-cli/bin/jest');
debug>

Apparently Jest always injects this breakpoint so that you have time to open Chrome DevTools (irrelevant in our case since we're only going to use cli/repl). 显然,Jest总是插入此断点,以便您有时间打开Chrome DevTools(在我们的案例中无关紧要,因为我们仅使用cli / repl)。

Continue past this breakpoint with c , and after a short time (without any indication of course that things are progressing along) you should see your breakpoint: 继续使用c结束此断点,然后过一会儿(当然没有任何进展的迹象),您应该看到断点:

break in webpack/assets/react/components/presentation/Feed/Comments/Comment/commentSpec.jsx:12
 10     var wrapper = (0, _enzyme.shallow)(_react2.default.createElement(_comment2.default, { loading: true }));
 11
>12     debugger;
 13     expect(wrapper.find(_button2.default)).to.have.length(1);
 14   });
 debug>

The last weird thing is you need to type repl to inspect objects as described in Node Debugger and Inspecting variables using node's built-in debugger? 最后一件奇怪的事情是,您需要键入repl来检查节点 ,如Node Debugger中所述,并使用Node的内置 调试器来 检查变量?

The combination of all these steps was not immediately obvious to me while reading the documentation, so I hope this answer helps someone get over the hurdle faster. 在阅读文档时,所有这些步骤的组合对我来说并不是立即显而易见的,因此我希望这个答案可以帮助某人更快地克服障碍。

From node v8.4 the debugger keyword within the code is fixed for VM context. 从节点v8.4开始,代码中的debugger关键字针对VM上下文是固定的。 Refer this git comment . 请参阅此git注释

1. Type debugger keyword in your Jest code : 1. 在您的Jest代码中输入debugger关键字

describe('Testcase definition', () => {
   it('should be defined with subobjects', () => {
     debugger; // <-- This keyword breaks on Chrome inspect
     expect(true).toBe(true);
   });
});
  1. Command to Run: 运行命令:

    node --inspect-brk --inspect ./node_modules/.bin/jest -i tests/mytest.test.js 节点--inspect-brk-检查./node_modules/.bin/jest -i tests / mytest.test.js

  2. Now open chrome://inspect/#devices on Chrome. 现在,在Chrome上打开 chrome:// inspect /#devices Voila! 瞧!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM