From b0d093a8ae31bd3ae27b2fc57b1e48847c25f13e Mon Sep 17 00:00:00 2001 From: TZ Date: Wed, 28 Mar 2018 14:31:42 +0800 Subject: [PATCH 1/3] fix: should pass env.EGG_TYPESCRIPT at test --- lib/cmd/test.js | 5 ++++- test/fixtures/example-ts/test/index.test.ts | 10 +--------- test/fixtures/ts/{app.js => app.ts} | 0 test/ts.test.js | 8 ++++---- 4 files changed, 9 insertions(+), 14 deletions(-) rename test/fixtures/ts/{app.js => app.ts} (100%) diff --git a/lib/cmd/test.js b/lib/cmd/test.js index c14b1efd..c2ed834d 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -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'); diff --git a/test/fixtures/example-ts/test/index.test.ts b/test/fixtures/example-ts/test/index.test.ts index 25c83639..a5b29892 100644 --- a/test/fixtures/example-ts/test/index.test.ts +++ b/test/fixtures/example-ts/test/index.test.ts @@ -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; - before(() => { - app = mock.app({ typescript: true } as MockOption); - return app.ready(); - }); - after(() => app.close()); it('should work', async () => { await app .httpRequest() diff --git a/test/fixtures/ts/app.js b/test/fixtures/ts/app.ts similarity index 100% rename from test/fixtures/ts/app.js rename to test/fixtures/ts/app.ts diff --git a/test/ts.test.js b/test/ts.test.js index 3993cd02..2038b91c 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -14,7 +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() + // .debug() .expect('stdout', /### egg from ts/) .expect('stdout', /options.typescript=true/) .expect('stdout', /started/) @@ -26,7 +26,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'/) @@ -41,7 +41,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) @@ -55,7 +55,7 @@ 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) From 8e74b1e3c6c5f38aaea9896dcce42ec9beaa759f Mon Sep 17 00:00:00 2001 From: TZ Date: Wed, 28 Mar 2018 15:20:27 +0800 Subject: [PATCH 2/3] feat: cov with typescript --- README.md | 3 ++- lib/cmd/cov.js | 9 +++++++++ test/fixtures/example-ts/app/controller/home.ts | 9 +++++++++ test/fixtures/example-ts/app/router.ts | 6 ++---- .../example-ts/typings/app/controller/index.d.ts | 10 ++++++++++ test/fixtures/example-ts/typings/index.d.ts | 7 +++++++ test/ts.test.js | 15 +++++++++++++++ 7 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/example-ts/app/controller/home.ts create mode 100644 test/fixtures/example-ts/typings/app/controller/index.d.ts create mode 100644 test/fixtures/example-ts/typings/index.d.ts 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(); + }); }); From 5f77aa420bcf6a433276efd3fc2820593b5205c7 Mon Sep 17 00:00:00 2001 From: TZ Date: Wed, 28 Mar 2018 16:17:57 +0800 Subject: [PATCH 3/3] fff --- .autod.conf | 2 +- package.json | 1 - test/fixtures/ts/app.ts | 6 ------ test/fixtures/ts/test.ts | 3 --- test/ts.test.js | 6 ++---- 5 files changed, 3 insertions(+), 15 deletions(-) delete mode 100644 test/fixtures/ts/app.ts delete mode 100644 test/fixtures/ts/test.ts diff --git a/.autod.conf b/.autod.conf index 1ca8ef14..2530613c 100644 --- a/.autod.conf +++ b/.autod.conf @@ -20,7 +20,7 @@ module.exports = { 'egg', 'autod', 'eslint-config-egg', - 'egg-ci', + // 'egg-ci', ], keep: [ ], diff --git a/package.json b/package.json index 2df90b14..de392313 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/fixtures/ts/app.ts b/test/fixtures/ts/app.ts deleted file mode 100644 index e46106f7..00000000 --- a/test/fixtures/ts/app.ts +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; - - -module.exports = app => { - app.logger.info('###', require('./test.ts').default.name); -}; diff --git a/test/fixtures/ts/test.ts b/test/fixtures/ts/test.ts deleted file mode 100644 index 61519692..00000000 --- a/test/fixtures/ts/test.ts +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; - -export default { name: 'egg from ts' }; diff --git a/test/ts.test.js b/test/ts.test.js index 9f6f3ede..eea43a0b 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -15,7 +15,6 @@ describe('test/ts.test.js', () => { mm(process.env, 'NODE_ENV', 'development'); return coffee.fork(eggBin, [ 'dev', '--typescript' ], { cwd }) // .debug() - .expect('stdout', /### egg from ts/) .expect('stdout', /options.typescript=true/) .expect('stdout', /started/) .expect('code', 0) @@ -69,10 +68,9 @@ describe('test/ts.test.js', () => { } cwd = path.join(__dirname, './fixtures/example-ts'); return coffee.fork(eggBin, [ 'cov', '--ts' ], { cwd }) - // .debug() + .debug() .expect('stdout', /hi, egg, 123456/) - .expect('stdout', /should work/) - .expect('stdout', /Statements.*100%/) + .expect('stdout', process.env.NYC_ROOT_ID ? /Coverage summary/ : /Statements.*100%/) .expect('code', 0) .end(); });