LoginSignup
21
19

More than 5 years have passed since last update.

nycでbabel6のカバレッジを取る

Last updated at Posted at 2016-02-03

nycは普段npm testで実行しているテストに、追従するようにして簡単にコードカバレッジを取ることが出来ます。

package.json片
{
  "scripts": {
    "test": "mocha --compilers js:espower-babel/guess",
    "cover": "nyc --reporter=lcov --reporter=text npm run test"
  }
}

上記のようなcoverを実行すると、lcovレポータがcoverage/lcov.infoの生成を、textレポータがコンソール上の集計画面を出力します。

スクリーンショット 2016-02-03 20.05.23.png

あなたが isparta の代替手段を探している場合、必要なのはnpm install nyc --save-devcoverの追記だけではないでしょうか。

power-assertを使用しない場合はtestmocha --require babel-registerとしてみてください。

nycの動作確認用の環境設定

前述のスクリーンショットでは、src/index.jsにFooクラスのbazメソッドを定義し、test/index.jsからasync/awaitでテストを書くと言う内容で、コードカバレッジを取っています。

具体的には下記のような3ファイルです。

ディレクトリ構造の確認
tree .
# .
# ├── package.json
# ├── src
# │   └── index.js
# └── test
#     └── index.js
package.json
{
  "name": "nyc-example",
  "scripts": {
    "test": "mocha --compilers js:espower-babel/guess",
    "cover": "nyc --reporter=lcov --reporter=text npm run test"
  },
  "devDependencies": {
    "babel-polyfill": "^6.3.14",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-stage-2": "^6.3.13",
    "espower-babel": "^4.0.1",
    "mocha": "^2.4.5",
    "nyc": "^5.5.0",
    "power-assert": "^1.2.0"
  },
  "babel": {
    "presets": [
      "es2015",
      "stage-2"
    ]
  }
}
src/index.js
export default class Foo {
  bar() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve('baz');
      }, 0);
    });
  }
}
test/index.js
import Foo from '../src';
import { equal } from 'power-assert';
import 'babel-polyfill';

describe('Fooクラス', () => {
  it('barメソッドは非同期にbazを返す', async () => {
    const foo = new Foo;
    equal(await foo.bar(), 'baz');
  });
});

テストでasync/awaitを使わないのであれば、babel-polyfillは不要です。お好みで依存モジュールを付け替えしてみましょう。

avaを使う

power-assertbabel-polyfillを内蔵しているテストランナーavaであれば、package.jsonがもう少しシンプルになります。

ディレクトリ構造の確認
tree .
# .
# ├── package.json
# ├── src
# │   └── index.js
# └── test
#     └── index.js
package.json
{
  "name": "nyc-example",
  "scripts": {
    "test": "ava --require babel-register",
    "cover": "nyc --reporter=lcov --reporter=text npm run test"
  },
  "devDependencies": {
    "ava": "^0.11.0",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-stage-2": "^6.3.13",
    "babel-register": "^6.4.3",
    "nyc": "^5.5.0"
  },
  "babel": {
    "presets": [
      "es2015",
      "stage-2"
    ]
  }
}
src/index.js
export default class Foo {
  bar() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve('baz');
      }, 0);
    });
  }
}
test/index.js
import Foo from '../src';
import test from 'ava';

test('barメソッドは非同期にbazを返す', async t => {
  const foo = new Foo;
  t.is(await foo.bar(), 'baz');
});

スクリーンショット 2016-02-03 20.30.12.png

成果物

ava+nyc+travisCIを使ってnpmjsへライブラリを公開したレポジトリがあるので、よければ参考にしてください。

21
19
7

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
21
19