【问题标题】:How do I use node debug cli with Jest?如何在 Jest 中使用节点调试 cli?
【发布时间】:2017-08-16 07:32:02
【问题描述】:

我如何才能将简单的节点 cli/repl 调试器与 Jest 一起使用?

Jest documentation 使用 node-inspector,但从 Node 6.3 开始它已过时/已弃用。我还是在 Node 7.7.4 上尝试了推荐的命令:

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

但这只是挂在下面(假设等待节点检查器):

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

我添加了警告指定的--inspect,但即使这样,Chrome DevTools 中我的debugger 语句的执行也不会停止。

对于一个非常简单的用例来说,这似乎过于复杂。

【问题讨论】:

    标签: node.js jestjs node-debugger babel-jest


    【解决方案1】:

    从节点 v8.4 开始,代码中的 debugger 关键字已针对 VM 上下文进行了修复。参考this git comment

    1.在您的 Jest 代码中输入 debugger 关键字

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

      node --inspect-brk --inspect ./node_modules/.bin/jest -i tests/mytest.test.js

    2. 现在在 Chrome 上打开chrome://inspect/#devices。瞧!

    【讨论】:

      【解决方案2】:

      我发现以下命令有效:

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

      ...但有一些古怪的行为。当调试器第一次停止时,您会看到:

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

      显然,Jest 总是注入此断点,以便您有时间打开 Chrome DevTools(在我们的例子中无关,因为我们只会使用 cli/repl)。

      使用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>
      

      最后一件奇怪的事情是你需要输入 repl 来检查对象,如 Node DebuggerInspecting variables using node's built-in debugger? 中所述

      在阅读文档时,所有这些步骤的组合对我来说并不是很明显,所以我希望这个答案可以帮助某人更快地克服障碍。

      【讨论】:

        猜你喜欢
        • 2014-09-28
        • 2020-12-19
        • 2022-01-16
        • 2023-01-19
        • 1970-01-01
        • 2015-10-05
        • 1970-01-01
        • 2019-05-16
        • 1970-01-01
        相关资源
        最近更新 更多