scramble cadenza

技術ネタのガラクタ置き場

node-inspector を使わず mocha で debug する

イントロ

mocha (>3.1.0)V8 inspector integration がサポートされたとかで、デバッグが楽になった。

今までは node-inspector を入れたり(しかもコイツが中々使いづらい…)、console.log 連打したりで、非常に効率が悪かった。

が、これを使うと chromeconsole 上で色々できるので、少しマシになる。

環境

  • node (6.9.4)
  • mocha (3.4.2)

やり方

spec の中で debugger を仕込む

--- a/spec/imageResizer_spec.js
+++ b/spec/imageResizer_spec.js
@@ -4,6 +4,10 @@ const endPoint = new Aws.Endpoint('http://localhost:4572');
 const s3 = new Aws.S3({ endpoint: endPoint });

 describe('imageResizer', () => {
+  beforeEach(() => {
+    debugger;
+  });
+
   it('desc', () => {
     console.log("test");
   });

mocha--debug-brk--inspect option をつけて spec を実行する

% mocha --debug-brk --inspect --compilers js:babel-core/register spec/imageResizer_spec.js
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/bdb841cb-6a37-45e7-ae6a-c922b628bae9
Debugger attached.

すると devtools://xxxx という url が発行されるので chrome で開く。

f:id:mgi:20170723095300p:plain

F8 (右上の右矢印ボタンでも良い)を押すと、以下のように debugger のところで止まる。
後はお好きなように、という感じですね。

f:id:mgi:20170723095249p:plain

未解決なこと

node 8.x.x 系だとうまくいかない。。
というか nodejs の更新早すぎィ!