TypeScript環境でnycとtapeを使って、カバレッジを取るテスト環境を整える。
初めはavaを使おうと思ってたんです。
下記の説明を見たところコンパイルしてからやってね、みたいな形になっています。 github.com
avaはmagic-assertを使っており、jsに対してassertの表示を見やすくするための処理が入ってるはずです。 そのため、babel等を使う前提で作られています。そのため、TypeScriptへの対応はまだされていない模様です。
というわけで諦めて、tapeとts-nodeとnycでカバレッジを取ってみます。
yarn add tape ts-node nyc typescript @types/tape -D
package.jsonにnpm scriptsを記述します。 今回はコンソールに出力したいのとhtmlでも出力してみたいので reporterを2つ指定してあります。
{ .... "scripts":{ "test": "nyc --reporter=html --reporter=text tape src/test/front/**.ts" } }
同じく、package.jsonにnycの設定を追加します。
{ .... "nyc":{ "include": [ "src/main/front/**/*.ts" ], "extension": [ ".ts" ], "require": [ "ts-node/register" ], "all": true } }
サンプルコードとテストコードです。 tapeのendメソッドを呼ばなくても動きそうだと思ったのですがうまくいかず・・・
// module.ts export default function(){ console.log("test") if(typeof window=="object"){ console.log("not reached") } } // module.spec.ts import * as test from "tape" import module from "../../main/front/module" test('xxx', (t)=>{ module() t.isEqual("a","b") t.end() })
npm test
こんなログが出ます。
PS D:\workspace\spring-sandbox\oauth> npm test > spring-sandbox@1.0.0 test D:\workspace\spring-sandbox\oauth > nyc --reporter=html --reporter=text tape src/test/front/**.ts TAP version 13 # xxx test not ok 1 should be equal --- operator: equal expected: 'b' actual: 'a' at: Test.test (D:\workspace\spring-sandbox\oauth\src\test\front\test.ts:5:5) ... 1..1 # tests 1 # pass 0 # fail 1 ----------|----------|----------|----------|----------|----------------| File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | ----------|----------|----------|----------|----------|----------------| All files | 75 | 50 | 100 | 75 | | test.ts | 75 | 50 | 100 | 75 | 5 | ----------|----------|----------|----------|----------|----------------| npm ERR! Test failed. See above for more details.
htmlのカバレッジレポートもこんな感じで出力されました。
まとめ
avaが使いたかったところから迷走した感があります。
よくよく考えたらTypeScript使うので jasmineでいい気がした。(apiがわからない問題は解決するはず) つまり、カバレッジも取れそうだし jasmineベースのjestでいい気がしました。
また、環境構築し直しです。