Skip to content

Commit

Permalink
feat: cov support typescript (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and popomore committed Mar 29, 2018
1 parent 7952f6c commit 8a05e19
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .autod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
'egg',
'autod',
'eslint-config-egg',
'egg-ci',
// 'egg-ci',
],
keep: [
],
Expand Down
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
5 changes: 4 additions & 1 deletion lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class TestCommand extends Command {

* run(context) {
const opt = {
env: Object.assign({ NODE_ENV: 'test' }, context.env),
env: Object.assign({
NODE_ENV: 'test',
EGG_TYPESCRIPT: context.argv.typescript,
}, context.env),
execArgv: context.execArgv,
};
const mochaFile = require.resolve('mocha/bin/_mocha');
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"coffee": "^4.1.0",
"cross-env": "^3.1.3",
"egg": "^2.5.0",
"egg-ci": "^1.8.0",
"egg-mock": "^3.15.1",
"enzyme": "^2.0.0",
"eslint": "^4.12.1",
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: 1 addition & 9 deletions test/fixtures/example-ts/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
'use strict';

import { Application, Context } from 'egg';
import { default as mock, MockOption, BaseMockApplication } from 'egg-mock';
import * as path from 'path';
import { app } from 'egg-mock/bootstrap';

describe('test/index.test.ts', () => {
let app: BaseMockApplication<Application, Context>;
before(() => {
app = mock.app({ typescript: true } as MockOption);
return app.ready();
});
after(() => app.close());
it('should work', async () => {
await app
.httpRequest()
Expand Down
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 {
}
}
6 changes: 0 additions & 6 deletions test/fixtures/ts/app.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/ts/test.ts

This file was deleted.

23 changes: 18 additions & 5 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ describe('test/ts.test.js', () => {
cwd = path.join(__dirname, './fixtures/ts');
mm(process.env, 'NODE_ENV', 'development');
return coffee.fork(eggBin, [ 'dev', '--typescript' ], { cwd })
.debug()
.expect('stdout', /### egg from ts/)
// .debug()
.expect('stdout', /options.typescript=true/)
.expect('stdout', /started/)
.expect('code', 0)
Expand All @@ -26,7 +25,7 @@ describe('test/ts.test.js', () => {
cwd = path.join(__dirname, './fixtures/ts');
mm(process.env, 'NODE_ENV', 'development');
return coffee.fork(eggBin, [ 'test', '--typescript' ], { cwd })
.debug()
// .debug()
.notExpect('stdout', /false == true/)
.notExpect('stdout', /should not load js files/)
.expect('stdout', /--- \[string\] 'wrong assert ts'/)
Expand All @@ -41,7 +40,7 @@ describe('test/ts.test.js', () => {
}
cwd = path.join(__dirname, './fixtures/example-ts');
return coffee.fork(eggBin, [ 'dev', '--ts' ], { cwd })
.debug()
// .debug()
.expect('stdout', /hi, egg, 12345/)
.expect('stdout', /started/)
.expect('code', 0)
Expand All @@ -55,10 +54,24 @@ describe('test/ts.test.js', () => {
}
cwd = path.join(__dirname, './fixtures/example-ts');
return coffee.fork(eggBin, [ 'test', '--ts' ], { cwd })
.debug()
// .debug()
.expect('stdout', /hi, egg, 123456/)
.expect('stdout', /should work/)
.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', process.env.NYC_ROOT_ID ? /Coverage summary/ : /Statements.*100%/)
.expect('code', 0)
.end();
});
});

0 comments on commit 8a05e19

Please sign in to comment.