mocha+TypeScript+power-assert
TypeScriptがasync/await対応したのでテストとかでも使いやすくなった。
こないだ試したらちょうどmocha+TypeScript+power-assertが使えるようになってたのでメモる。
package.json
{ "devDependencies": { "babel": "^6.0.15", "babel-polyfill": "^6.0.16", "espower-babel": "^3.3.0", "espower-typescript": "^2.0.0", "mocha": "^2.3.3", "power-assert": "^1.1.0", "watchify": "^3.6.0" }, "scripts": { "test": "mocha --compilers ts:espower-typescript/guess --compilers ts:espower-babel/guess test/**/*-test.ts", "test:watch": "mocha -w --compilers ts:espower-typescript/guess --compilers ts:espower-babel/guess test/**/*-test.ts" } }
test/index-test.ts
import 'babel-polyfill'; import assert from 'power-assert'; describe(`hoge`, () => { it(`huga`, async () => { let res = await new Promise((resolve) => { setTimeout(() => { resolve(2); }); }); assert(res === 1); }); });
でいける。
ポイントはmochaの–compilersが複数個受け取れること。
これで中間ファイルなしに一気にテストをmochaで書ける(エラー行もsourceMapでちゃんと対応付けられる)
espower-typescriptはこないだまで内部のTypeScriptが古くてES6対応できてなかったけど、こないだバージョンアップして対応できた。
power-assertもこないだasync/await対応したので使えるようになった。
これで勝てる。