Skip to content

Commit

Permalink
feat: cov with typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Mar 28, 2018
1 parent b0d093a commit 8e74b1e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/cmd/cov.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/example-ts/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

import { Controller } from 'egg';

export default class HomeController extends Controller {
public async index() {
this.ctx.body = 'hi, egg';
}
}
6 changes: 2 additions & 4 deletions test/fixtures/example-ts/app/router.ts
Original file line number Diff line number Diff line change
@@ -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);
};
10 changes: 10 additions & 0 deletions test/fixtures/example-ts/typings/app/controller/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions test/fixtures/example-ts/typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Context } from 'egg';

// extend egg
declare module 'egg' {
interface Context {
}
}
15 changes: 15 additions & 0 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 8e74b1e

Please sign in to comment.