LoginSignup
16
16

More than 5 years have passed since last update.

Node.js Debug and Test Tips

Posted at

概要

Node.jsの開発で、デバッグやテストに役に立つTipsをまとめました。

デバッグ

console.xxx

標準出力にログを出力する。

  • console.log()
  • console.info()
  • console.warn()
  • console.error()
  • console.dir()
  • console.trace() => スタックとレースを出力
  • console.time() => 始めから終わりまでの時間を出力

assert.xxx

値が期待した値と異なる場合は、例外を発生させる。

node debug

コマンドライン上でデバッグする。

  • c: continue 実行
  • n: next 次の行を実行
  • s: step 次の関数内を実行
  • repl: プログラム内の変数や関数にアクセスする
  • watch(): 変数の値をウォッチする。引数はテキストで変数名を指定する
  • unwatch(): 変数の値をウォッチを解除する
  • setBreakPoint / sb: ブレークポイントを設定する
  • clearBreakPoint / cb: ブレークポイントを解除する

node-inspector

GUI(Coogle Chromeのデバッガー)でデバッグする。

# node-inspectorをインストール
npm install -g node-inspector

# node-inspectorでデバッグするプロセスを起動する
node --debug-brk app.js

# node-inspectorを起動
node-inspector

テスト

mocha

ユニットテストのフレームワーク

npm install mocha

supertest

expressのwebテストフレームワーク
mochaと合わせて使う、httpレベルのでテストが可能。

npm install supertest
node_modules/mocha/bin/mocha --reporter spec

supertest-session

supertestにsession管理機能を追加したもの。
ログイン機能などがある場合はこちらを使うと良い。

npm install supertest-session
node_modules/mocha/bin/mocha --reporter spec
16
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
16