LoginSignup
16
12

More than 5 years have passed since last update.

nyc + ts-node + mochaでtsファイルをテストしてカバレッジを出す

Posted at

node.js環境下で、いちいちtscするのでなく*.tsファイルをそのままテストしたい。
ということで環境を作った。

目指す環境

  • mocha + chaiでテスト
  • nycでカバレッジも出す
  • いちいちtscしない

当初はnycでなくistanbulremap-istanbulを使っていたが、こちらのほうがよさげだったのを変更

インストール

npm i --save-dev nyc ts-node mocha chai @types/mocha @types/chai

設定

package.jsonに以下のような感じでnycの設定を記述する。
引数に列挙していってもいいけど流石に多かったので。

  "nyc": {
    "include": [
      "app/*.ts",
      "app/**/*.ts"
    ],
    "extension": [
      ".ts"
    ],
    "require": [
      "ts-node/register"
    ],
    "reporter": [
      "json",
      "html",
      "text"
    ],
    "sourceMap":true,
    "all": true
  }

スクリプトを記述

package.jsonに記述する。

    "test": "mocha --recursive --require ts-node/register \"test/**/*.ts\"",
    "cover": "nyc npm t"

走らせてみた

text.PNG

npm run coverでこんな具合にカバレッジが表示される。

coverage.PNG

こんな具合にhtmlも出力された。

16
12
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
12