diff --git a/README.md b/README.md index c7637d8d..545f8700 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ TEST_TIMEOUT=2000 egg-bin test ### cov -Using [istanbul] to run code coverage, it support all test params above. +Using [nyc] to run code coverage, it support all test params above. Coverage reporter will output text-summary, json and lcov. @@ -169,6 +169,7 @@ You can pass any mocha argv. - `-x` add dir ignore coverage, support multiple argv - `--prerequire` prerequire files for coverage instrument, you can use this options if load files slowly when call `mm.app` or `mm.cluster` +- `--typescript` / `--ts` enable typescript support, default to `false`, if true, will auto add `.ts` extension and ignore `typings` and `d.ts`. - also support all test params above. #### environment diff --git a/lib/cmd/cov.js b/lib/cmd/cov.js index 94e027e0..45410a02 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -104,8 +104,17 @@ class CovCommand extends Command { '-r', 'json-summary', '-r', 'json', '-r', 'lcov', + '--extension', '.ts', ]; + // typescript support + if (context.typescript) { + covArgs.push('--extension'); + covArgs.push('.ts'); + this.addExclude('typings/'); + this.addExclude('**/*.d.ts'); + } + for (const exclude of this[EXCLUDES]) { covArgs.push('-x'); covArgs.push(exclude); diff --git a/test/fixtures/example-ts/app/controller/home.ts b/test/fixtures/example-ts/app/controller/home.ts new file mode 100644 index 00000000..a860bc79 --- /dev/null +++ b/test/fixtures/example-ts/app/controller/home.ts @@ -0,0 +1,9 @@ +'use strict'; + +import { Controller } from 'egg'; + +export default class HomeController extends Controller { + public async index() { + this.ctx.body = 'hi, egg'; + } +} diff --git a/test/fixtures/example-ts/app/router.ts b/test/fixtures/example-ts/app/router.ts index 46c83b7d..f8cf3f6f 100644 --- a/test/fixtures/example-ts/app/router.ts +++ b/test/fixtures/example-ts/app/router.ts @@ -1,9 +1,7 @@ 'use strict'; -import { Application, Context } from 'egg'; +import { Application } from 'egg'; export default (app: Application) => { - app.router.get('/', function* (this: Context) { - this.body = `hi, egg`; - }); + app.router.get('/', app.controller.home.index); }; \ No newline at end of file diff --git a/test/fixtures/example-ts/typings/app/controller/index.d.ts b/test/fixtures/example-ts/typings/app/controller/index.d.ts new file mode 100644 index 00000000..de6b5d46 --- /dev/null +++ b/test/fixtures/example-ts/typings/app/controller/index.d.ts @@ -0,0 +1,10 @@ +// This file was auto created by egg-ts-helper +// Do not modify this file!!!!!!!!! + +import Home from '../../../app/controller/home'; + +declare module 'egg' { + interface IController { + home: Home; + } +} diff --git a/test/fixtures/example-ts/typings/index.d.ts b/test/fixtures/example-ts/typings/index.d.ts new file mode 100644 index 00000000..f7e4b522 --- /dev/null +++ b/test/fixtures/example-ts/typings/index.d.ts @@ -0,0 +1,7 @@ +import { Context } from 'egg'; + +// extend egg +declare module 'egg' { + interface Context { + } +} \ No newline at end of file diff --git a/test/ts.test.js b/test/ts.test.js index 2038b91c..9f6f3ede 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -61,4 +61,19 @@ describe('test/ts.test.js', () => { .expect('code', 0) .end(); }); + + it('should cov app', () => { + if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { + console.log('skip egg@1'); + return; + } + cwd = path.join(__dirname, './fixtures/example-ts'); + return coffee.fork(eggBin, [ 'cov', '--ts' ], { cwd }) + // .debug() + .expect('stdout', /hi, egg, 123456/) + .expect('stdout', /should work/) + .expect('stdout', /Statements.*100%/) + .expect('code', 0) + .end(); + }); });