From dd4302ec3253118861c902e6c4ec256a2bbba246 Mon Sep 17 00:00:00 2001 From: TZ Date: Sun, 25 Mar 2018 18:39:52 +0800 Subject: [PATCH 01/19] feat: add typescript support --- .autod.conf | 1 + .gitignore | 10 +++++- README.md | 3 ++ lib/cmd/dev.js | 17 ++++++++++ lib/cmd/test.js | 9 ++++- lib/command.js | 22 ++++++++++-- lib/ts-helper.js | 28 +++++++++++++++ package.json | 17 ++++++---- test/fixtures/require-script.js | 3 ++ test/fixtures/ts/app.js | 6 ++++ test/fixtures/ts/config/config.default.js | 3 ++ .../ts/node_modules/aliyun-egg/index.js | 20 +++++++++++ .../ts/node_modules/aliyun-egg/package.json | 6 ++++ test/fixtures/ts/package.json | 6 ++++ test/fixtures/ts/test.ts | 3 ++ test/fixtures/ts/test/a.test.js | 7 ++++ test/fixtures/ts/test/sub.ts | 3 ++ test/fixtures/ts/test/typescript.test.ts | 17 ++++++++++ test/lib/cmd/dev.test.js | 10 ++++++ test/ts.test.js | 34 +++++++++++++++++++ 20 files changed, 214 insertions(+), 11 deletions(-) create mode 100644 lib/ts-helper.js create mode 100644 test/fixtures/require-script.js create mode 100644 test/fixtures/ts/app.js create mode 100644 test/fixtures/ts/config/config.default.js create mode 100644 test/fixtures/ts/node_modules/aliyun-egg/index.js create mode 100644 test/fixtures/ts/node_modules/aliyun-egg/package.json create mode 100644 test/fixtures/ts/package.json create mode 100644 test/fixtures/ts/test.ts create mode 100644 test/fixtures/ts/test/a.test.js create mode 100644 test/fixtures/ts/test/sub.ts create mode 100644 test/fixtures/ts/test/typescript.test.ts create mode 100644 test/ts.test.js diff --git a/.autod.conf b/.autod.conf index d5d0bbd3..6ccc1a3d 100644 --- a/.autod.conf +++ b/.autod.conf @@ -13,6 +13,7 @@ module.exports = { 'co-mocha', 'intelli-espower-loader', 'power-assert', + 'espower-typescript', 'ypkgfiles', ], devdep: [ diff --git a/.gitignore b/.gitignore index 348042ed..f22ee7c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,18 @@ node_modules/ coverage/ -!test/fixtures/custom-framework-app/node_modules/ +test/fixtures/custom-framework-app/node_modules/ + +test/fixtures/demo-app/node_modules/aliyun-egg/ !test/fixtures/demo-app/node_modules/aliyun-egg/node_modules/ + +test/fixtures/ts/node_modules/aliyun-egg/ +!test/fixtures/ts/node_modules/aliyun-egg/node_modules/ + !test/fixtures/test-files-glob/** !test/fixtures/test-files-stack/node_modules/ !test/fixtures/example/node_modules/ + + **/run/*.json .tmp .vscode diff --git a/README.md b/README.md index 3e10ed6f..c7637d8d 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ $ egg-bin dev - `--port` server port, default to `7001`. - `--cluster` worker process number, skip this argvs will start only `1` worker, provide this without value will start `cpu` count worker. - `--sticky` start a sticky cluster server, default to `false`. +- `--typescript` / `--ts` enable typescript support, default to `false`. +- `--require` will add to `execArgv`, support multiple. ### debug @@ -130,6 +132,7 @@ You can pass any mocha argv. - `--grep` only run tests matching - `--timeout` milliseconds, default to 30000 - `--full-trace` display the full stack trace, default to false. +- `--typescript` / `--ts` enable typescript support, default to `false`. - see more at https://mochajs.org/#usage #### environment diff --git a/lib/cmd/dev.js b/lib/cmd/dev.js index 1292475a..42643c09 100644 --- a/lib/cmd/dev.js +++ b/lib/cmd/dev.js @@ -33,6 +33,11 @@ class DevCommand extends Command { description: 'specify framework that can be absolute path or npm package', type: 'string', }, + require: { + description: 'will add to execArgv --require', + type: 'array', + alias: 'r', + }, }; } @@ -40,6 +45,18 @@ class DevCommand extends Command { return 'Start server at local dev mode'; } + get context() { + const context = super.context; + const { argv, execArgvObj } = context; + execArgvObj.require = execArgvObj.require || []; + // add require to execArgv + if (argv.require) { + execArgvObj.require.push(...argv.require); + argv.require = undefined; + } + return context; + } + * run(context) { const devArgs = yield this.formatArgs(context); const options = { diff --git a/lib/cmd/test.js b/lib/cmd/test.js index dd04ef6a..e2c6554a 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -85,13 +85,19 @@ class TestCommand extends Command { requireArr.push(require.resolve('intelli-espower-loader')); } + // for power-assert + if (testArgv.typescript) { + requireArr.push('espower-typescript/guess'); + } + testArgv.require = requireArr; // collect test files let files = testArgv._.slice(); if (!files.length) { - files = [ process.env.TESTS || 'test/**/*.test.js' ]; + files = [ process.env.TESTS || `test/**/*.test.${testArgv.typescript ? 'ts' : 'js'}` ]; } + // expand glob and skip node_modules and fixtures files = globby.sync(files.concat('!test/**/{fixtures, node_modules}/**/*.test.js')); files.sort(); @@ -108,6 +114,7 @@ class TestCommand extends Command { testArgv.r = undefined; testArgv.t = undefined; testArgv.g = undefined; + testArgv.typescript = undefined; return this.helper.unparseArgv(testArgv); } diff --git a/lib/command.js b/lib/command.js index 096a9966..a52b8758 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1,5 +1,6 @@ 'use strict'; +const path = require('path'); const BaseCommand = require('common-bin'); class Command extends BaseCommand { @@ -9,16 +10,33 @@ class Command extends BaseCommand { execArgv: true, removeAlias: true, }; + + // common-bin setter, don't care about override at sub class + // https://github.com/node-modules/common-bin/blob/master/lib/command.js#L158 + this.options = { + typescript: { + description: 'whether enable typescript support, will load `ts-node/register` etc', + type: 'boolean', + alias: 'ts', + }, + }; } get context() { const context = super.context; + const { argv, debugPort, execArgvObj } = context; // compatible - if (context.debugPort) context.debug = context.debugPort; + if (debugPort) context.debug = debugPort; // remove unuse args - context.argv.$0 = undefined; + argv.$0 = undefined; + + // execArgv + if (argv.typescript) { + execArgvObj.require = execArgvObj.require || []; + execArgvObj.require.push(path.join(__dirname, './ts-helper.js')); + } return context; } diff --git a/lib/ts-helper.js b/lib/ts-helper.js new file mode 100644 index 00000000..8121e4cd --- /dev/null +++ b/lib/ts-helper.js @@ -0,0 +1,28 @@ +'use strict'; + +require('ts-node').register({ + typeCheck: true, + compilerOptions: { + target: 'es2017', + module: 'commonjs', + strict: true, + moduleResolution: 'node', + noImplicitAny: false, + experimentalDecorators: true, + emitDecoratorMetadata: true, + charset: 'utf8', + allowJs: false, + pretty: true, + noEmitOnError: false, + noUnusedLocals: true, + noUnusedParameters: true, + allowUnreachableCode: false, + allowUnusedLabels: false, + strictPropertyInitialization: false, + noFallthroughCasesInSwitch: true, + skipLibCheck: true, + skipDefaultLibCheck: true, + inlineSourceMap: true, + importHelpers: true, + }, +}); diff --git a/package.json b/package.json index 60159475..4c0b48c7 100644 --- a/package.json +++ b/package.json @@ -9,24 +9,27 @@ }, "dependencies": { "autod": "^3.0.1", - "chalk": "^2.3.1", - "co-mocha": "^1.2.1", - "common-bin": "^2.7.1", + "chalk": "^2.3.2", + "co-mocha": "^1.2.2", + "common-bin": "^2.7.2", "debug": "^3.1.0", "detect-port": "^1.2.2", "egg-utils": "^2.3.0", "globby": "^8.0.1", "inspector-proxy": "^1.2.1", "intelli-espower-loader": "^1.0.1", - "mocha": "^5.0.1", + "espower-typescript": "^8.1.3", + "mocha": "^5.0.5", "mz-modules": "^2.1.0", - "nyc": "^11.4.1", + "nyc": "^11.6.0", "power-assert": "^1.4.4", "semver": "^5.5.0", - "test-exclude": "^4.2.0", + "test-exclude": "^4.2.1", + "ts-node": "^5.0.1", "ypkgfiles": "^1.5.0" }, "devDependencies": { + "@types/mocha": "^5.0.0", "autod": "^3.0.1", "babel": "^6.3.26", "babel-preset-airbnb": "^1.0.1", @@ -35,7 +38,7 @@ "cross-env": "^3.1.3", "egg": "^1.8.0", "egg-ci": "^1.8.0", - "egg-mock": "^3.14.0", + "egg-mock": "^3.15.1", "enzyme": "^2.0.0", "eslint": "^4.12.1", "eslint-config-egg": "^7.0.0", diff --git a/test/fixtures/require-script.js b/test/fixtures/require-script.js new file mode 100644 index 00000000..00d46285 --- /dev/null +++ b/test/fixtures/require-script.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('hey, you require me by --require'); diff --git a/test/fixtures/ts/app.js b/test/fixtures/ts/app.js new file mode 100644 index 00000000..e46106f7 --- /dev/null +++ b/test/fixtures/ts/app.js @@ -0,0 +1,6 @@ +'use strict'; + + +module.exports = app => { + app.logger.info('###', require('./test.ts').default.name); +}; diff --git a/test/fixtures/ts/config/config.default.js b/test/fixtures/ts/config/config.default.js new file mode 100644 index 00000000..762c2647 --- /dev/null +++ b/test/fixtures/ts/config/config.default.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.key = '12345'; diff --git a/test/fixtures/ts/node_modules/aliyun-egg/index.js b/test/fixtures/ts/node_modules/aliyun-egg/index.js new file mode 100644 index 00000000..c1500965 --- /dev/null +++ b/test/fixtures/ts/node_modules/aliyun-egg/index.js @@ -0,0 +1,20 @@ +'use strict'; + +const egg = require('../../../../../node_modules/egg'); + +module.exports = Object.assign({}, egg); + +module.exports.startCluster = options => { + console.log('options.typescript=%s', options.typescript); + console.log('options: %j', options); + if (process.execArgv.length) { + console.log('process.execArgv:', process.execArgv); + } + + // make sure exit + setTimeout(() => { + console.log('exit by master test end') + process.exit(0); + }, 5000); + return egg.startCluster(options); +}; diff --git a/test/fixtures/ts/node_modules/aliyun-egg/package.json b/test/fixtures/ts/node_modules/aliyun-egg/package.json new file mode 100644 index 00000000..4e36e661 --- /dev/null +++ b/test/fixtures/ts/node_modules/aliyun-egg/package.json @@ -0,0 +1,6 @@ +{ + "name": "aliyun-egg", + "dependencies": { + "egg": "*" + } +} diff --git a/test/fixtures/ts/package.json b/test/fixtures/ts/package.json new file mode 100644 index 00000000..3cb71fd5 --- /dev/null +++ b/test/fixtures/ts/package.json @@ -0,0 +1,6 @@ +{ + "name": "ts", + "egg": { + "framework": "aliyun-egg" + } +} \ No newline at end of file diff --git a/test/fixtures/ts/test.ts b/test/fixtures/ts/test.ts new file mode 100644 index 00000000..61519692 --- /dev/null +++ b/test/fixtures/ts/test.ts @@ -0,0 +1,3 @@ +'use strict'; + +export default { name: 'egg from ts' }; diff --git a/test/fixtures/ts/test/a.test.js b/test/fixtures/ts/test/a.test.js new file mode 100644 index 00000000..32c1eddc --- /dev/null +++ b/test/fixtures/ts/test/a.test.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('a.test.js', () => { + it('should success', () => { + throw 'should not load js files'; + }); +}); diff --git a/test/fixtures/ts/test/sub.ts b/test/fixtures/ts/test/sub.ts new file mode 100644 index 00000000..61519692 --- /dev/null +++ b/test/fixtures/ts/test/sub.ts @@ -0,0 +1,3 @@ +'use strict'; + +export default { name: 'egg from ts' }; diff --git a/test/fixtures/ts/test/typescript.test.ts b/test/fixtures/ts/test/typescript.test.ts new file mode 100644 index 00000000..afe066d7 --- /dev/null +++ b/test/fixtures/ts/test/typescript.test.ts @@ -0,0 +1,17 @@ +'use strict'; + +const assert = require('assert'); + +describe('typescript.test.ts', () => { + it('should success', () => { + const obj = require('./sub'); + console.log('###', obj.default.name); + assert(obj.default.name === 'egg from ts'); + }); + + it('should fail', () => { + const obj = require('./sub'); + console.log('###', obj.default.name); + assert(obj.default.name === 'wrong assert ts'); + }); +}); diff --git a/test/lib/cmd/dev.test.js b/test/lib/cmd/dev.test.js index cdaf7c13..230d5dca 100644 --- a/test/lib/cmd/dev.test.js +++ b/test/lib/cmd/dev.test.js @@ -137,4 +137,14 @@ describe('test/lib/cmd/dev.test.js', () => { .expect('code', 0) .end(done); }); + + it('should support --require', () => { + const script = path.join(__dirname, '../../fixtures/require-script'); + mm(process.env, 'NODE_ENV', 'development'); + return coffee.fork(eggBin, [ 'dev', '--require', script ], { cwd }) + // .debug() + .expect('stdout', /hey, you require me by --require/) + .expect('code', 0) + .end(); + }); }); diff --git a/test/ts.test.js b/test/ts.test.js new file mode 100644 index 00000000..8d2a3460 --- /dev/null +++ b/test/ts.test.js @@ -0,0 +1,34 @@ +'use strict'; + +const path = require('path'); +const coffee = require('coffee'); +const mm = require('mm'); + +describe('test/ts.test.js', () => { + const eggBin = require.resolve('../bin/egg-bin'); + const cwd = path.join(__dirname, './fixtures/ts'); + + afterEach(mm.restore); + + it('should support ts', () => { + 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', /egg started/) + .expect('code', 0) + .end(); + }); + + it('should support ts test', () => { + mm(process.env, 'NODE_ENV', 'development'); + return coffee.fork(eggBin, [ 'test', '--typescript' ], { cwd }) + .debug() + .notExpect('stdout', /false == true/) + .notExpect('stdout', /should not load js files/) + .expect('stdout', /--- \[string\] 'wrong assert ts'/) + .expect('code', 1) + .end(); + }); +}); From ed4453d1c9b7d297b855612f91c79c0b990f6f8b Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 26 Mar 2018 18:21:27 +0800 Subject: [PATCH 02/19] test: fix cov --- test/lib/cmd/cov.test.js | 69 ++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/test/lib/cmd/cov.test.js b/test/lib/cmd/cov.test.js index 727ebe81..16064e51 100644 --- a/test/lib/cmd/cov.test.js +++ b/test/lib/cmd/cov.test.js @@ -14,7 +14,14 @@ describe('test/lib/cmd/cov.test.js', () => { beforeEach(() => rimraf(path.join(cwd, 'coverage'))); afterEach(mm.restore); - it('should success', done => { + function assertCoverage(cwd) { + assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); + assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-summary.json'))); + assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); + assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); + } + + it('should success', function* () { mm(process.env, 'TESTS', 'test/**/*.test.js'); mm(process.env, 'NYC_CWD', cwd); const child = coffee.fork(eggBin, [ 'cov' ], { cwd }) @@ -29,18 +36,12 @@ describe('test/lib/cmd/cov.test.js', () => { child.expect('stdout', /Statements {3}: 80% \( 4[\/|\\]5 \)/); } - child.expect('code', 0) - .end(err => { - assert.ifError(err); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-summary.json'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - done(); - }); + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); }); - it('should hotfixSpawnWrap success on mock windows', done => { + it('should hotfixSpawnWrap success on mock windows', function* () { mm(process.env, 'TESTS', 'test/**/*.test.js'); mm(process.env, 'NYC_CWD', cwd); const child = coffee.fork(eggBin, [ 'cov' ], { cwd }) @@ -55,15 +56,10 @@ describe('test/lib/cmd/cov.test.js', () => { if (!process.env.NYC_ROOT_ID) { child.expect('stdout', /Statements {3}: 80% \( 4[\/|\\]5 \)/); } - child.expect('code', 0) - .end(err => { - assert.ifError(err); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-summary.json'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - done(); - }); + + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); }); it('should success with COV_EXCLUDES', function* () { @@ -82,11 +78,12 @@ describe('test/lib/cmd/cov.test.js', () => { } yield child.expect('code', 0).end(); - assert(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + } }); it('should success with -x to ignore one dirs', function* () { @@ -103,11 +100,12 @@ describe('test/lib/cmd/cov.test.js', () => { } yield child.expect('code', 0).end(); - assert(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + } }); it('should success with -x to ignore multi dirs', function* () { @@ -124,11 +122,12 @@ describe('test/lib/cmd/cov.test.js', () => { } yield child.expect('code', 0).end(); - assert(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + } }); it('should fail when test fail', done => { From 975ba18e6fa49d077d7d49b2adc792b3c22a2908 Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 26 Mar 2018 19:53:25 +0800 Subject: [PATCH 03/19] fix: espower-typescript fullpath --- lib/cmd/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cmd/test.js b/lib/cmd/test.js index e2c6554a..c14b1efd 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -87,7 +87,7 @@ class TestCommand extends Command { // for power-assert if (testArgv.typescript) { - requireArr.push('espower-typescript/guess'); + requireArr.push(require.resolve('espower-typescript/guess')); } testArgv.require = requireArr; From e5a37cb62201c72dfa8b6e844c9dbcdce305070f Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 26 Mar 2018 22:09:14 +0800 Subject: [PATCH 04/19] test: add egg2 test && real cluster test --- .autod.conf | 1 + .travis.yml | 12 +++++++++++- package.json | 4 ++-- test/fixtures/example-ts/app.ts | 7 +++++++ test/fixtures/example-ts/app/router.ts | 9 +++++++++ .../example-ts/config/config.default.ts | 7 +++++++ .../example-ts/node_modules/egg/index.js | 8 ++++++++ .../example-ts/node_modules/egg/package.json | 3 +++ test/fixtures/example-ts/package.json | 3 +++ test/ts.test.js | 18 +++++++++++++++++- 10 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/example-ts/app.ts create mode 100644 test/fixtures/example-ts/app/router.ts create mode 100644 test/fixtures/example-ts/config/config.default.ts create mode 100644 test/fixtures/example-ts/node_modules/egg/index.js create mode 100644 test/fixtures/example-ts/node_modules/egg/package.json create mode 100644 test/fixtures/example-ts/package.json diff --git a/.autod.conf b/.autod.conf index 6ccc1a3d..1ca8ef14 100644 --- a/.autod.conf +++ b/.autod.conf @@ -17,6 +17,7 @@ module.exports = { 'ypkgfiles', ], devdep: [ + 'egg', 'autod', 'eslint-config-egg', 'egg-ci', diff --git a/.travis.yml b/.travis.yml index 47cc542e..7212f2a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,9 +3,19 @@ language: node_js node_js: - '6' - '8' +env: + - EGG_VERSION=1 + - EGG_VERSION=2 +matrix: + exclude: + - node_js: '6' + env: EGG_VERSION=2 install: - - npm i npminstall && npminstall + - npm i npminstall + - sed -i.bak '/"egg":/d' package.json + - npminstall -d script: + - eval "npminstall -d egg@$EGG_VERSION" - npm run ci after_script: - npminstall codecov && codecov diff --git a/package.json b/package.json index 4c0b48c7..fee647df 100644 --- a/package.json +++ b/package.json @@ -15,10 +15,10 @@ "debug": "^3.1.0", "detect-port": "^1.2.2", "egg-utils": "^2.3.0", + "espower-typescript": "^8.1.3", "globby": "^8.0.1", "inspector-proxy": "^1.2.1", "intelli-espower-loader": "^1.0.1", - "espower-typescript": "^8.1.3", "mocha": "^5.0.5", "mz-modules": "^2.1.0", "nyc": "^11.6.0", @@ -36,7 +36,7 @@ "babel-register": "^6.4.3", "coffee": "^4.1.0", "cross-env": "^3.1.3", - "egg": "^1.8.0", + "egg": "^2.5.0", "egg-ci": "^1.8.0", "egg-mock": "^3.15.1", "enzyme": "^2.0.0", diff --git a/test/fixtures/example-ts/app.ts b/test/fixtures/example-ts/app.ts new file mode 100644 index 00000000..a063672b --- /dev/null +++ b/test/fixtures/example-ts/app.ts @@ -0,0 +1,7 @@ +'use strict'; + +import { Application } from 'egg'; + +export default (app: Application) => { + console.log(`hi, egg, ${app.config.keys}`); +}; diff --git a/test/fixtures/example-ts/app/router.ts b/test/fixtures/example-ts/app/router.ts new file mode 100644 index 00000000..46c83b7d --- /dev/null +++ b/test/fixtures/example-ts/app/router.ts @@ -0,0 +1,9 @@ +'use strict'; + +import { Application, Context } from 'egg'; + +export default (app: Application) => { + app.router.get('/', function* (this: Context) { + this.body = `hi, egg`; + }); +}; \ No newline at end of file diff --git a/test/fixtures/example-ts/config/config.default.ts b/test/fixtures/example-ts/config/config.default.ts new file mode 100644 index 00000000..446264a9 --- /dev/null +++ b/test/fixtures/example-ts/config/config.default.ts @@ -0,0 +1,7 @@ +'use strict'; + +export default () => { + const config: any = {}; + config.keys = '123456'; + return config; +}; diff --git a/test/fixtures/example-ts/node_modules/egg/index.js b/test/fixtures/example-ts/node_modules/egg/index.js new file mode 100644 index 00000000..d1757bf0 --- /dev/null +++ b/test/fixtures/example-ts/node_modules/egg/index.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = require('../../../../../node_modules/egg'); + +setTimeout(() => { + console.log('exit by master test end') + process.exit(0); +}, 6000); diff --git a/test/fixtures/example-ts/node_modules/egg/package.json b/test/fixtures/example-ts/node_modules/egg/package.json new file mode 100644 index 00000000..6697ad3f --- /dev/null +++ b/test/fixtures/example-ts/node_modules/egg/package.json @@ -0,0 +1,3 @@ +{ + "name": "egg" +} diff --git a/test/fixtures/example-ts/package.json b/test/fixtures/example-ts/package.json new file mode 100644 index 00000000..1ab2c3f9 --- /dev/null +++ b/test/fixtures/example-ts/package.json @@ -0,0 +1,3 @@ +{ + "name": "example-ts" +} \ No newline at end of file diff --git a/test/ts.test.js b/test/ts.test.js index 8d2a3460..8bc6fddf 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -6,11 +6,12 @@ const mm = require('mm'); describe('test/ts.test.js', () => { const eggBin = require.resolve('../bin/egg-bin'); - const cwd = path.join(__dirname, './fixtures/ts'); + let cwd; afterEach(mm.restore); it('should support ts', () => { + cwd = path.join(__dirname, './fixtures/ts'); mm(process.env, 'NODE_ENV', 'development'); return coffee.fork(eggBin, [ 'dev', '--typescript' ], { cwd }) .debug() @@ -22,6 +23,7 @@ describe('test/ts.test.js', () => { }); it('should support ts test', () => { + cwd = path.join(__dirname, './fixtures/ts'); mm(process.env, 'NODE_ENV', 'development'); return coffee.fork(eggBin, [ 'test', '--typescript' ], { cwd }) .debug() @@ -31,4 +33,18 @@ describe('test/ts.test.js', () => { .expect('code', 1) .end(); }); + + it('should start app', () => { + if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { // eslint-disable-line eqeqeq + console.log('skip egg@1'); + return; + } + cwd = path.join(__dirname, './fixtures/example-ts'); + return coffee.fork(eggBin, [ 'dev', '--ts' ], { cwd }) + .debug() + .expect('stdout', /hi, egg, 12345/) + .expect('stdout', /egg started/) + .expect('code', 0) + .end(); + }); }); From 645a3649bcddbc42e3a545e4f60b5c10125e8706 Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 26 Mar 2018 22:12:12 +0800 Subject: [PATCH 05/19] test: remove appveyor egg@1 nodee@6 --- appveyor.yml | 1 - package.json | 2 +- test/ts.test.js | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 3d15e523..c274b7d3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,5 @@ environment: matrix: - - nodejs_version: '6' - nodejs_version: '8' install: diff --git a/package.json b/package.json index fee647df..17289bf2 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "test-local": "node bin/egg-bin.js test -t 3600000", "cov": "nyc -r lcov -r text-summary npm run test-local", "ci-test-only": "TESTS=test/lib/cmd/cov.test.js npm run test-local", - "ci": "npm run lint && npm run pkgfiles -- --check && npm run autod -- --check && npm run ci-test-only && npm run cov", + "ci": "npm run lint && npm run pkgfiles -- --check && npm run ci-test-only && npm run cov", "autod": "node bin/egg-bin.js autod" }, "engines": { diff --git a/test/ts.test.js b/test/ts.test.js index 8bc6fddf..a73f2b9e 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -35,7 +35,7 @@ describe('test/ts.test.js', () => { }); it('should start app', () => { - if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { // eslint-disable-line eqeqeq + if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { console.log('skip egg@1'); return; } From 4222b7414f0a04bae00d10b0c1f5c73075bd865f Mon Sep 17 00:00:00 2001 From: TZ Date: Tue, 27 Mar 2018 10:20:26 +0800 Subject: [PATCH 06/19] f --- package.json | 3 +- test/fixtures/example-ts/test/index.test.js | 53 +++++++++++++++++++++ test/fixtures/example-ts/test/index.test.ts | 22 +++++++++ test/fixtures/example-ts/tsconfig.json | 25 ++++++++++ test/ts.test.js | 14 ++++++ 5 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/example-ts/test/index.test.js create mode 100644 test/fixtures/example-ts/test/index.test.ts create mode 100644 test/fixtures/example-ts/tsconfig.json diff --git a/package.json b/package.json index 17289bf2..4dbdcad6 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,8 @@ "react": "^0.14.7", "react-addons-test-utils": "^0.14.7", "react-dom": "^0.14.7", - "semver": "^5.4.1" + "semver": "^5.4.1", + "typescript": "^2.7.2" }, "repository": { "type": "git", diff --git a/test/fixtures/example-ts/test/index.test.js b/test/fixtures/example-ts/test/index.test.js new file mode 100644 index 00000000..f1e0a7d9 --- /dev/null +++ b/test/fixtures/example-ts/test/index.test.js @@ -0,0 +1,53 @@ +'use strict'; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [0, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var _this = this; +exports.__esModule = true; +var egg_mock_1 = require("egg-mock"); +describe('test/index.test.ts', function () { + var app; + before(function () { + // tslint:disable-next-line + app = egg_mock_1["default"].app({ typescript: true }); + return app.ready(); + }); + after(function () { return app.close(); }); + it('should work', function () { return __awaiter(_this, void 0, void 0, function () { + return __generator(this, function (_a) { + return [2 /*return*/]; + }); + }); }); +}); diff --git a/test/fixtures/example-ts/test/index.test.ts b/test/fixtures/example-ts/test/index.test.ts new file mode 100644 index 00000000..ba12fa7d --- /dev/null +++ b/test/fixtures/example-ts/test/index.test.ts @@ -0,0 +1,22 @@ +'use strict'; + +import { Application, Context } from 'egg'; +import { default as mock, MockOption, BaseMockApplication } from 'egg-mock'; +import * as path from 'path'; + +describe('test/index.test.ts', () => { + let app: BaseMockApplication; + before(() => { + // tslint:disable-next-line + app = mock.app({ typescript: true } as MockOption); + return app.ready(); + }); + after(() => app.close()); + it('should work', async () => { + // await app + // .httpRequest() + // .get('/') + // .expect('hi, egg') + // .expect(200); + }); +}); diff --git a/test/fixtures/example-ts/tsconfig.json b/test/fixtures/example-ts/tsconfig.json new file mode 100644 index 00000000..092038b3 --- /dev/null +++ b/test/fixtures/example-ts/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "target": "es2017", + "module": "commonjs", + "strict": true, + "moduleResolution": "node", + "noImplicitAny": false, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "charset": "utf8", + "allowJs": false, + "pretty": true, + "noEmitOnError": false, + "noUnusedLocals": true, + "noUnusedParameters": true, + "allowUnreachableCode": false, + "allowUnusedLabels": false, + "strictPropertyInitialization": false, + "noFallthroughCasesInSwitch": true, + "skipLibCheck": true, + "skipDefaultLibCheck": true, + "inlineSourceMap": true, + "importHelpers": true + } +} \ No newline at end of file diff --git a/test/ts.test.js b/test/ts.test.js index a73f2b9e..a76fae6d 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -47,4 +47,18 @@ describe('test/ts.test.js', () => { .expect('code', 0) .end(); }); + + it.only('should test 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, [ 'test', '--ts' ], { cwd }) + .debug() + // .expect('stdout', /hi, egg, 12345/) + // .expect('stdout', /egg started/) + .expect('code', 0) + .end(); + }); }); From a670ca6764966e580699b3a295d0ee697ea08376 Mon Sep 17 00:00:00 2001 From: TZ Date: Sun, 25 Mar 2018 18:39:52 +0800 Subject: [PATCH 07/19] feat: add typescript support --- .autod.conf | 1 + .gitignore | 10 +++++- README.md | 3 ++ lib/cmd/dev.js | 17 ++++++++++ lib/cmd/test.js | 9 ++++- lib/command.js | 22 ++++++++++-- lib/ts-helper.js | 28 +++++++++++++++ package.json | 17 ++++++---- test/fixtures/require-script.js | 3 ++ test/fixtures/ts/app.js | 6 ++++ test/fixtures/ts/config/config.default.js | 3 ++ .../ts/node_modules/aliyun-egg/index.js | 20 +++++++++++ .../ts/node_modules/aliyun-egg/package.json | 6 ++++ test/fixtures/ts/package.json | 6 ++++ test/fixtures/ts/test.ts | 3 ++ test/fixtures/ts/test/a.test.js | 7 ++++ test/fixtures/ts/test/sub.ts | 3 ++ test/fixtures/ts/test/typescript.test.ts | 17 ++++++++++ test/lib/cmd/dev.test.js | 10 ++++++ test/ts.test.js | 34 +++++++++++++++++++ 20 files changed, 214 insertions(+), 11 deletions(-) create mode 100644 lib/ts-helper.js create mode 100644 test/fixtures/require-script.js create mode 100644 test/fixtures/ts/app.js create mode 100644 test/fixtures/ts/config/config.default.js create mode 100644 test/fixtures/ts/node_modules/aliyun-egg/index.js create mode 100644 test/fixtures/ts/node_modules/aliyun-egg/package.json create mode 100644 test/fixtures/ts/package.json create mode 100644 test/fixtures/ts/test.ts create mode 100644 test/fixtures/ts/test/a.test.js create mode 100644 test/fixtures/ts/test/sub.ts create mode 100644 test/fixtures/ts/test/typescript.test.ts create mode 100644 test/ts.test.js diff --git a/.autod.conf b/.autod.conf index d5d0bbd3..6ccc1a3d 100644 --- a/.autod.conf +++ b/.autod.conf @@ -13,6 +13,7 @@ module.exports = { 'co-mocha', 'intelli-espower-loader', 'power-assert', + 'espower-typescript', 'ypkgfiles', ], devdep: [ diff --git a/.gitignore b/.gitignore index 348042ed..f22ee7c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,18 @@ node_modules/ coverage/ -!test/fixtures/custom-framework-app/node_modules/ +test/fixtures/custom-framework-app/node_modules/ + +test/fixtures/demo-app/node_modules/aliyun-egg/ !test/fixtures/demo-app/node_modules/aliyun-egg/node_modules/ + +test/fixtures/ts/node_modules/aliyun-egg/ +!test/fixtures/ts/node_modules/aliyun-egg/node_modules/ + !test/fixtures/test-files-glob/** !test/fixtures/test-files-stack/node_modules/ !test/fixtures/example/node_modules/ + + **/run/*.json .tmp .vscode diff --git a/README.md b/README.md index 3e10ed6f..c7637d8d 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ $ egg-bin dev - `--port` server port, default to `7001`. - `--cluster` worker process number, skip this argvs will start only `1` worker, provide this without value will start `cpu` count worker. - `--sticky` start a sticky cluster server, default to `false`. +- `--typescript` / `--ts` enable typescript support, default to `false`. +- `--require` will add to `execArgv`, support multiple. ### debug @@ -130,6 +132,7 @@ You can pass any mocha argv. - `--grep` only run tests matching - `--timeout` milliseconds, default to 30000 - `--full-trace` display the full stack trace, default to false. +- `--typescript` / `--ts` enable typescript support, default to `false`. - see more at https://mochajs.org/#usage #### environment diff --git a/lib/cmd/dev.js b/lib/cmd/dev.js index a187ee26..3ae25083 100644 --- a/lib/cmd/dev.js +++ b/lib/cmd/dev.js @@ -33,6 +33,11 @@ class DevCommand extends Command { description: 'specify framework that can be absolute path or npm package', type: 'string', }, + require: { + description: 'will add to execArgv --require', + type: 'array', + alias: 'r', + }, }; } @@ -40,6 +45,18 @@ class DevCommand extends Command { return 'Start server at local dev mode'; } + get context() { + const context = super.context; + const { argv, execArgvObj } = context; + execArgvObj.require = execArgvObj.require || []; + // add require to execArgv + if (argv.require) { + execArgvObj.require.push(...argv.require); + argv.require = undefined; + } + return context; + } + * run(context) { const devArgs = yield this.formatArgs(context); const env = { diff --git a/lib/cmd/test.js b/lib/cmd/test.js index dd04ef6a..e2c6554a 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -85,13 +85,19 @@ class TestCommand extends Command { requireArr.push(require.resolve('intelli-espower-loader')); } + // for power-assert + if (testArgv.typescript) { + requireArr.push('espower-typescript/guess'); + } + testArgv.require = requireArr; // collect test files let files = testArgv._.slice(); if (!files.length) { - files = [ process.env.TESTS || 'test/**/*.test.js' ]; + files = [ process.env.TESTS || `test/**/*.test.${testArgv.typescript ? 'ts' : 'js'}` ]; } + // expand glob and skip node_modules and fixtures files = globby.sync(files.concat('!test/**/{fixtures, node_modules}/**/*.test.js')); files.sort(); @@ -108,6 +114,7 @@ class TestCommand extends Command { testArgv.r = undefined; testArgv.t = undefined; testArgv.g = undefined; + testArgv.typescript = undefined; return this.helper.unparseArgv(testArgv); } diff --git a/lib/command.js b/lib/command.js index 096a9966..a52b8758 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1,5 +1,6 @@ 'use strict'; +const path = require('path'); const BaseCommand = require('common-bin'); class Command extends BaseCommand { @@ -9,16 +10,33 @@ class Command extends BaseCommand { execArgv: true, removeAlias: true, }; + + // common-bin setter, don't care about override at sub class + // https://github.com/node-modules/common-bin/blob/master/lib/command.js#L158 + this.options = { + typescript: { + description: 'whether enable typescript support, will load `ts-node/register` etc', + type: 'boolean', + alias: 'ts', + }, + }; } get context() { const context = super.context; + const { argv, debugPort, execArgvObj } = context; // compatible - if (context.debugPort) context.debug = context.debugPort; + if (debugPort) context.debug = debugPort; // remove unuse args - context.argv.$0 = undefined; + argv.$0 = undefined; + + // execArgv + if (argv.typescript) { + execArgvObj.require = execArgvObj.require || []; + execArgvObj.require.push(path.join(__dirname, './ts-helper.js')); + } return context; } diff --git a/lib/ts-helper.js b/lib/ts-helper.js new file mode 100644 index 00000000..8121e4cd --- /dev/null +++ b/lib/ts-helper.js @@ -0,0 +1,28 @@ +'use strict'; + +require('ts-node').register({ + typeCheck: true, + compilerOptions: { + target: 'es2017', + module: 'commonjs', + strict: true, + moduleResolution: 'node', + noImplicitAny: false, + experimentalDecorators: true, + emitDecoratorMetadata: true, + charset: 'utf8', + allowJs: false, + pretty: true, + noEmitOnError: false, + noUnusedLocals: true, + noUnusedParameters: true, + allowUnreachableCode: false, + allowUnusedLabels: false, + strictPropertyInitialization: false, + noFallthroughCasesInSwitch: true, + skipLibCheck: true, + skipDefaultLibCheck: true, + inlineSourceMap: true, + importHelpers: true, + }, +}); diff --git a/package.json b/package.json index 60159475..4c0b48c7 100644 --- a/package.json +++ b/package.json @@ -9,24 +9,27 @@ }, "dependencies": { "autod": "^3.0.1", - "chalk": "^2.3.1", - "co-mocha": "^1.2.1", - "common-bin": "^2.7.1", + "chalk": "^2.3.2", + "co-mocha": "^1.2.2", + "common-bin": "^2.7.2", "debug": "^3.1.0", "detect-port": "^1.2.2", "egg-utils": "^2.3.0", "globby": "^8.0.1", "inspector-proxy": "^1.2.1", "intelli-espower-loader": "^1.0.1", - "mocha": "^5.0.1", + "espower-typescript": "^8.1.3", + "mocha": "^5.0.5", "mz-modules": "^2.1.0", - "nyc": "^11.4.1", + "nyc": "^11.6.0", "power-assert": "^1.4.4", "semver": "^5.5.0", - "test-exclude": "^4.2.0", + "test-exclude": "^4.2.1", + "ts-node": "^5.0.1", "ypkgfiles": "^1.5.0" }, "devDependencies": { + "@types/mocha": "^5.0.0", "autod": "^3.0.1", "babel": "^6.3.26", "babel-preset-airbnb": "^1.0.1", @@ -35,7 +38,7 @@ "cross-env": "^3.1.3", "egg": "^1.8.0", "egg-ci": "^1.8.0", - "egg-mock": "^3.14.0", + "egg-mock": "^3.15.1", "enzyme": "^2.0.0", "eslint": "^4.12.1", "eslint-config-egg": "^7.0.0", diff --git a/test/fixtures/require-script.js b/test/fixtures/require-script.js new file mode 100644 index 00000000..00d46285 --- /dev/null +++ b/test/fixtures/require-script.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('hey, you require me by --require'); diff --git a/test/fixtures/ts/app.js b/test/fixtures/ts/app.js new file mode 100644 index 00000000..e46106f7 --- /dev/null +++ b/test/fixtures/ts/app.js @@ -0,0 +1,6 @@ +'use strict'; + + +module.exports = app => { + app.logger.info('###', require('./test.ts').default.name); +}; diff --git a/test/fixtures/ts/config/config.default.js b/test/fixtures/ts/config/config.default.js new file mode 100644 index 00000000..762c2647 --- /dev/null +++ b/test/fixtures/ts/config/config.default.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.key = '12345'; diff --git a/test/fixtures/ts/node_modules/aliyun-egg/index.js b/test/fixtures/ts/node_modules/aliyun-egg/index.js new file mode 100644 index 00000000..c1500965 --- /dev/null +++ b/test/fixtures/ts/node_modules/aliyun-egg/index.js @@ -0,0 +1,20 @@ +'use strict'; + +const egg = require('../../../../../node_modules/egg'); + +module.exports = Object.assign({}, egg); + +module.exports.startCluster = options => { + console.log('options.typescript=%s', options.typescript); + console.log('options: %j', options); + if (process.execArgv.length) { + console.log('process.execArgv:', process.execArgv); + } + + // make sure exit + setTimeout(() => { + console.log('exit by master test end') + process.exit(0); + }, 5000); + return egg.startCluster(options); +}; diff --git a/test/fixtures/ts/node_modules/aliyun-egg/package.json b/test/fixtures/ts/node_modules/aliyun-egg/package.json new file mode 100644 index 00000000..4e36e661 --- /dev/null +++ b/test/fixtures/ts/node_modules/aliyun-egg/package.json @@ -0,0 +1,6 @@ +{ + "name": "aliyun-egg", + "dependencies": { + "egg": "*" + } +} diff --git a/test/fixtures/ts/package.json b/test/fixtures/ts/package.json new file mode 100644 index 00000000..3cb71fd5 --- /dev/null +++ b/test/fixtures/ts/package.json @@ -0,0 +1,6 @@ +{ + "name": "ts", + "egg": { + "framework": "aliyun-egg" + } +} \ No newline at end of file diff --git a/test/fixtures/ts/test.ts b/test/fixtures/ts/test.ts new file mode 100644 index 00000000..61519692 --- /dev/null +++ b/test/fixtures/ts/test.ts @@ -0,0 +1,3 @@ +'use strict'; + +export default { name: 'egg from ts' }; diff --git a/test/fixtures/ts/test/a.test.js b/test/fixtures/ts/test/a.test.js new file mode 100644 index 00000000..32c1eddc --- /dev/null +++ b/test/fixtures/ts/test/a.test.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('a.test.js', () => { + it('should success', () => { + throw 'should not load js files'; + }); +}); diff --git a/test/fixtures/ts/test/sub.ts b/test/fixtures/ts/test/sub.ts new file mode 100644 index 00000000..61519692 --- /dev/null +++ b/test/fixtures/ts/test/sub.ts @@ -0,0 +1,3 @@ +'use strict'; + +export default { name: 'egg from ts' }; diff --git a/test/fixtures/ts/test/typescript.test.ts b/test/fixtures/ts/test/typescript.test.ts new file mode 100644 index 00000000..afe066d7 --- /dev/null +++ b/test/fixtures/ts/test/typescript.test.ts @@ -0,0 +1,17 @@ +'use strict'; + +const assert = require('assert'); + +describe('typescript.test.ts', () => { + it('should success', () => { + const obj = require('./sub'); + console.log('###', obj.default.name); + assert(obj.default.name === 'egg from ts'); + }); + + it('should fail', () => { + const obj = require('./sub'); + console.log('###', obj.default.name); + assert(obj.default.name === 'wrong assert ts'); + }); +}); diff --git a/test/lib/cmd/dev.test.js b/test/lib/cmd/dev.test.js index cdaf7c13..230d5dca 100644 --- a/test/lib/cmd/dev.test.js +++ b/test/lib/cmd/dev.test.js @@ -137,4 +137,14 @@ describe('test/lib/cmd/dev.test.js', () => { .expect('code', 0) .end(done); }); + + it('should support --require', () => { + const script = path.join(__dirname, '../../fixtures/require-script'); + mm(process.env, 'NODE_ENV', 'development'); + return coffee.fork(eggBin, [ 'dev', '--require', script ], { cwd }) + // .debug() + .expect('stdout', /hey, you require me by --require/) + .expect('code', 0) + .end(); + }); }); diff --git a/test/ts.test.js b/test/ts.test.js new file mode 100644 index 00000000..8d2a3460 --- /dev/null +++ b/test/ts.test.js @@ -0,0 +1,34 @@ +'use strict'; + +const path = require('path'); +const coffee = require('coffee'); +const mm = require('mm'); + +describe('test/ts.test.js', () => { + const eggBin = require.resolve('../bin/egg-bin'); + const cwd = path.join(__dirname, './fixtures/ts'); + + afterEach(mm.restore); + + it('should support ts', () => { + 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', /egg started/) + .expect('code', 0) + .end(); + }); + + it('should support ts test', () => { + mm(process.env, 'NODE_ENV', 'development'); + return coffee.fork(eggBin, [ 'test', '--typescript' ], { cwd }) + .debug() + .notExpect('stdout', /false == true/) + .notExpect('stdout', /should not load js files/) + .expect('stdout', /--- \[string\] 'wrong assert ts'/) + .expect('code', 1) + .end(); + }); +}); From 460664020a76562e172fe433718e5a1cec5b33a4 Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 26 Mar 2018 18:21:27 +0800 Subject: [PATCH 08/19] test: fix cov --- test/lib/cmd/cov.test.js | 69 ++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/test/lib/cmd/cov.test.js b/test/lib/cmd/cov.test.js index 727ebe81..16064e51 100644 --- a/test/lib/cmd/cov.test.js +++ b/test/lib/cmd/cov.test.js @@ -14,7 +14,14 @@ describe('test/lib/cmd/cov.test.js', () => { beforeEach(() => rimraf(path.join(cwd, 'coverage'))); afterEach(mm.restore); - it('should success', done => { + function assertCoverage(cwd) { + assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); + assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-summary.json'))); + assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); + assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); + } + + it('should success', function* () { mm(process.env, 'TESTS', 'test/**/*.test.js'); mm(process.env, 'NYC_CWD', cwd); const child = coffee.fork(eggBin, [ 'cov' ], { cwd }) @@ -29,18 +36,12 @@ describe('test/lib/cmd/cov.test.js', () => { child.expect('stdout', /Statements {3}: 80% \( 4[\/|\\]5 \)/); } - child.expect('code', 0) - .end(err => { - assert.ifError(err); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-summary.json'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - done(); - }); + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); }); - it('should hotfixSpawnWrap success on mock windows', done => { + it('should hotfixSpawnWrap success on mock windows', function* () { mm(process.env, 'TESTS', 'test/**/*.test.js'); mm(process.env, 'NYC_CWD', cwd); const child = coffee.fork(eggBin, [ 'cov' ], { cwd }) @@ -55,15 +56,10 @@ describe('test/lib/cmd/cov.test.js', () => { if (!process.env.NYC_ROOT_ID) { child.expect('stdout', /Statements {3}: 80% \( 4[\/|\\]5 \)/); } - child.expect('code', 0) - .end(err => { - assert.ifError(err); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/coverage-summary.json'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert.ok(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - done(); - }); + + yield child.expect('code', 0).end(); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) assertCoverage(cwd); }); it('should success with COV_EXCLUDES', function* () { @@ -82,11 +78,12 @@ describe('test/lib/cmd/cov.test.js', () => { } yield child.expect('code', 0).end(); - assert(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + } }); it('should success with -x to ignore one dirs', function* () { @@ -103,11 +100,12 @@ describe('test/lib/cmd/cov.test.js', () => { } yield child.expect('code', 0).end(); - assert(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + } }); it('should success with -x to ignore multi dirs', function* () { @@ -124,11 +122,12 @@ describe('test/lib/cmd/cov.test.js', () => { } yield child.expect('code', 0).end(); - assert(fs.existsSync(path.join(cwd, 'coverage/coverage-final.json'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov-report/index.html'))); - assert(fs.existsSync(path.join(cwd, 'coverage/lcov.info'))); - const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); - assert(!/ignore[\/|\\]a.js/.test(lcov)); + // only test on npm run test + if (!process.env.NYC_ROOT_ID) { + assertCoverage(cwd); + const lcov = fs.readFileSync(path.join(cwd, 'coverage/lcov.info'), 'utf8'); + assert(!/ignore[\/|\\]a.js/.test(lcov)); + } }); it('should fail when test fail', done => { From ed00eb0cf14aac0101e3c3c1d2d5ec70bd9be054 Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 26 Mar 2018 19:53:25 +0800 Subject: [PATCH 09/19] fix: espower-typescript fullpath --- lib/cmd/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cmd/test.js b/lib/cmd/test.js index e2c6554a..c14b1efd 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -87,7 +87,7 @@ class TestCommand extends Command { // for power-assert if (testArgv.typescript) { - requireArr.push('espower-typescript/guess'); + requireArr.push(require.resolve('espower-typescript/guess')); } testArgv.require = requireArr; From ef2b479f9927ed7cc283d859540f8a6c43a94cda Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 26 Mar 2018 22:09:14 +0800 Subject: [PATCH 10/19] test: add egg2 test && real cluster test --- .autod.conf | 1 + .travis.yml | 12 +++++++++++- package.json | 4 ++-- test/fixtures/example-ts/app.ts | 7 +++++++ test/fixtures/example-ts/app/router.ts | 9 +++++++++ .../example-ts/config/config.default.ts | 7 +++++++ .../example-ts/node_modules/egg/index.js | 8 ++++++++ .../example-ts/node_modules/egg/package.json | 3 +++ test/fixtures/example-ts/package.json | 3 +++ test/ts.test.js | 18 +++++++++++++++++- 10 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/example-ts/app.ts create mode 100644 test/fixtures/example-ts/app/router.ts create mode 100644 test/fixtures/example-ts/config/config.default.ts create mode 100644 test/fixtures/example-ts/node_modules/egg/index.js create mode 100644 test/fixtures/example-ts/node_modules/egg/package.json create mode 100644 test/fixtures/example-ts/package.json diff --git a/.autod.conf b/.autod.conf index 6ccc1a3d..1ca8ef14 100644 --- a/.autod.conf +++ b/.autod.conf @@ -17,6 +17,7 @@ module.exports = { 'ypkgfiles', ], devdep: [ + 'egg', 'autod', 'eslint-config-egg', 'egg-ci', diff --git a/.travis.yml b/.travis.yml index 47cc542e..7212f2a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,9 +3,19 @@ language: node_js node_js: - '6' - '8' +env: + - EGG_VERSION=1 + - EGG_VERSION=2 +matrix: + exclude: + - node_js: '6' + env: EGG_VERSION=2 install: - - npm i npminstall && npminstall + - npm i npminstall + - sed -i.bak '/"egg":/d' package.json + - npminstall -d script: + - eval "npminstall -d egg@$EGG_VERSION" - npm run ci after_script: - npminstall codecov && codecov diff --git a/package.json b/package.json index 4c0b48c7..fee647df 100644 --- a/package.json +++ b/package.json @@ -15,10 +15,10 @@ "debug": "^3.1.0", "detect-port": "^1.2.2", "egg-utils": "^2.3.0", + "espower-typescript": "^8.1.3", "globby": "^8.0.1", "inspector-proxy": "^1.2.1", "intelli-espower-loader": "^1.0.1", - "espower-typescript": "^8.1.3", "mocha": "^5.0.5", "mz-modules": "^2.1.0", "nyc": "^11.6.0", @@ -36,7 +36,7 @@ "babel-register": "^6.4.3", "coffee": "^4.1.0", "cross-env": "^3.1.3", - "egg": "^1.8.0", + "egg": "^2.5.0", "egg-ci": "^1.8.0", "egg-mock": "^3.15.1", "enzyme": "^2.0.0", diff --git a/test/fixtures/example-ts/app.ts b/test/fixtures/example-ts/app.ts new file mode 100644 index 00000000..a063672b --- /dev/null +++ b/test/fixtures/example-ts/app.ts @@ -0,0 +1,7 @@ +'use strict'; + +import { Application } from 'egg'; + +export default (app: Application) => { + console.log(`hi, egg, ${app.config.keys}`); +}; diff --git a/test/fixtures/example-ts/app/router.ts b/test/fixtures/example-ts/app/router.ts new file mode 100644 index 00000000..46c83b7d --- /dev/null +++ b/test/fixtures/example-ts/app/router.ts @@ -0,0 +1,9 @@ +'use strict'; + +import { Application, Context } from 'egg'; + +export default (app: Application) => { + app.router.get('/', function* (this: Context) { + this.body = `hi, egg`; + }); +}; \ No newline at end of file diff --git a/test/fixtures/example-ts/config/config.default.ts b/test/fixtures/example-ts/config/config.default.ts new file mode 100644 index 00000000..446264a9 --- /dev/null +++ b/test/fixtures/example-ts/config/config.default.ts @@ -0,0 +1,7 @@ +'use strict'; + +export default () => { + const config: any = {}; + config.keys = '123456'; + return config; +}; diff --git a/test/fixtures/example-ts/node_modules/egg/index.js b/test/fixtures/example-ts/node_modules/egg/index.js new file mode 100644 index 00000000..d1757bf0 --- /dev/null +++ b/test/fixtures/example-ts/node_modules/egg/index.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = require('../../../../../node_modules/egg'); + +setTimeout(() => { + console.log('exit by master test end') + process.exit(0); +}, 6000); diff --git a/test/fixtures/example-ts/node_modules/egg/package.json b/test/fixtures/example-ts/node_modules/egg/package.json new file mode 100644 index 00000000..6697ad3f --- /dev/null +++ b/test/fixtures/example-ts/node_modules/egg/package.json @@ -0,0 +1,3 @@ +{ + "name": "egg" +} diff --git a/test/fixtures/example-ts/package.json b/test/fixtures/example-ts/package.json new file mode 100644 index 00000000..1ab2c3f9 --- /dev/null +++ b/test/fixtures/example-ts/package.json @@ -0,0 +1,3 @@ +{ + "name": "example-ts" +} \ No newline at end of file diff --git a/test/ts.test.js b/test/ts.test.js index 8d2a3460..8bc6fddf 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -6,11 +6,12 @@ const mm = require('mm'); describe('test/ts.test.js', () => { const eggBin = require.resolve('../bin/egg-bin'); - const cwd = path.join(__dirname, './fixtures/ts'); + let cwd; afterEach(mm.restore); it('should support ts', () => { + cwd = path.join(__dirname, './fixtures/ts'); mm(process.env, 'NODE_ENV', 'development'); return coffee.fork(eggBin, [ 'dev', '--typescript' ], { cwd }) .debug() @@ -22,6 +23,7 @@ describe('test/ts.test.js', () => { }); it('should support ts test', () => { + cwd = path.join(__dirname, './fixtures/ts'); mm(process.env, 'NODE_ENV', 'development'); return coffee.fork(eggBin, [ 'test', '--typescript' ], { cwd }) .debug() @@ -31,4 +33,18 @@ describe('test/ts.test.js', () => { .expect('code', 1) .end(); }); + + it('should start app', () => { + if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { // eslint-disable-line eqeqeq + console.log('skip egg@1'); + return; + } + cwd = path.join(__dirname, './fixtures/example-ts'); + return coffee.fork(eggBin, [ 'dev', '--ts' ], { cwd }) + .debug() + .expect('stdout', /hi, egg, 12345/) + .expect('stdout', /egg started/) + .expect('code', 0) + .end(); + }); }); From 8f46c160225b437ab42c699cdf35a7345076ad67 Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 26 Mar 2018 22:12:12 +0800 Subject: [PATCH 11/19] test: remove appveyor egg@1 nodee@6 --- appveyor.yml | 1 - package.json | 2 +- test/ts.test.js | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 3d15e523..c274b7d3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,5 @@ environment: matrix: - - nodejs_version: '6' - nodejs_version: '8' install: diff --git a/package.json b/package.json index fee647df..17289bf2 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "test-local": "node bin/egg-bin.js test -t 3600000", "cov": "nyc -r lcov -r text-summary npm run test-local", "ci-test-only": "TESTS=test/lib/cmd/cov.test.js npm run test-local", - "ci": "npm run lint && npm run pkgfiles -- --check && npm run autod -- --check && npm run ci-test-only && npm run cov", + "ci": "npm run lint && npm run pkgfiles -- --check && npm run ci-test-only && npm run cov", "autod": "node bin/egg-bin.js autod" }, "engines": { diff --git a/test/ts.test.js b/test/ts.test.js index 8bc6fddf..a73f2b9e 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -35,7 +35,7 @@ describe('test/ts.test.js', () => { }); it('should start app', () => { - if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { // eslint-disable-line eqeqeq + if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { console.log('skip egg@1'); return; } From 9de39aa1506ea1b093a15a50f26eda927f72af23 Mon Sep 17 00:00:00 2001 From: wanghx Date: Tue, 27 Mar 2018 15:08:47 +0800 Subject: [PATCH 12/19] feat: remove index.test.js --- lib/ts-helper.js | 23 --------- test/fixtures/example-ts/test/index.test.js | 53 --------------------- test/fixtures/example-ts/test/index.test.ts | 10 ++-- test/fixtures/example-ts/tsconfig.json | 2 +- test/ts.test.js | 4 +- 5 files changed, 8 insertions(+), 84 deletions(-) delete mode 100644 test/fixtures/example-ts/test/index.test.js diff --git a/lib/ts-helper.js b/lib/ts-helper.js index 8121e4cd..e43dfa76 100644 --- a/lib/ts-helper.js +++ b/lib/ts-helper.js @@ -2,27 +2,4 @@ require('ts-node').register({ typeCheck: true, - compilerOptions: { - target: 'es2017', - module: 'commonjs', - strict: true, - moduleResolution: 'node', - noImplicitAny: false, - experimentalDecorators: true, - emitDecoratorMetadata: true, - charset: 'utf8', - allowJs: false, - pretty: true, - noEmitOnError: false, - noUnusedLocals: true, - noUnusedParameters: true, - allowUnreachableCode: false, - allowUnusedLabels: false, - strictPropertyInitialization: false, - noFallthroughCasesInSwitch: true, - skipLibCheck: true, - skipDefaultLibCheck: true, - inlineSourceMap: true, - importHelpers: true, - }, }); diff --git a/test/fixtures/example-ts/test/index.test.js b/test/fixtures/example-ts/test/index.test.js deleted file mode 100644 index f1e0a7d9..00000000 --- a/test/fixtures/example-ts/test/index.test.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict'; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (this && this.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (_) try { - if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [0, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -var _this = this; -exports.__esModule = true; -var egg_mock_1 = require("egg-mock"); -describe('test/index.test.ts', function () { - var app; - before(function () { - // tslint:disable-next-line - app = egg_mock_1["default"].app({ typescript: true }); - return app.ready(); - }); - after(function () { return app.close(); }); - it('should work', function () { return __awaiter(_this, void 0, void 0, function () { - return __generator(this, function (_a) { - return [2 /*return*/]; - }); - }); }); -}); diff --git a/test/fixtures/example-ts/test/index.test.ts b/test/fixtures/example-ts/test/index.test.ts index ba12fa7d..791f6f2d 100644 --- a/test/fixtures/example-ts/test/index.test.ts +++ b/test/fixtures/example-ts/test/index.test.ts @@ -13,10 +13,10 @@ describe('test/index.test.ts', () => { }); after(() => app.close()); it('should work', async () => { - // await app - // .httpRequest() - // .get('/') - // .expect('hi, egg') - // .expect(200); + await app + .httpRequest() + .get('/') + .expect('hi, egg') + .expect(200); }); }); diff --git a/test/fixtures/example-ts/tsconfig.json b/test/fixtures/example-ts/tsconfig.json index 092038b3..1d70b6b9 100644 --- a/test/fixtures/example-ts/tsconfig.json +++ b/test/fixtures/example-ts/tsconfig.json @@ -21,5 +21,5 @@ "skipDefaultLibCheck": true, "inlineSourceMap": true, "importHelpers": true - } + }, } \ No newline at end of file diff --git a/test/ts.test.js b/test/ts.test.js index a76fae6d..ef9e1ea9 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -56,8 +56,8 @@ describe('test/ts.test.js', () => { cwd = path.join(__dirname, './fixtures/example-ts'); return coffee.fork(eggBin, [ 'test', '--ts' ], { cwd }) .debug() - // .expect('stdout', /hi, egg, 12345/) - // .expect('stdout', /egg started/) + .expect('stdout', /hi, egg, 12345/) + .expect('stdout', /egg started/) .expect('code', 0) .end(); }); From d4475acd492d877fde7ba161062e014c4badc878 Mon Sep 17 00:00:00 2001 From: wanghx Date: Tue, 27 Mar 2018 15:09:49 +0800 Subject: [PATCH 13/19] feat: add tsconfig.json --- test/fixtures/ts/tsconfig.json | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/fixtures/ts/tsconfig.json diff --git a/test/fixtures/ts/tsconfig.json b/test/fixtures/ts/tsconfig.json new file mode 100644 index 00000000..1d70b6b9 --- /dev/null +++ b/test/fixtures/ts/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "target": "es2017", + "module": "commonjs", + "strict": true, + "moduleResolution": "node", + "noImplicitAny": false, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "charset": "utf8", + "allowJs": false, + "pretty": true, + "noEmitOnError": false, + "noUnusedLocals": true, + "noUnusedParameters": true, + "allowUnreachableCode": false, + "allowUnusedLabels": false, + "strictPropertyInitialization": false, + "noFallthroughCasesInSwitch": true, + "skipLibCheck": true, + "skipDefaultLibCheck": true, + "inlineSourceMap": true, + "importHelpers": true + }, +} \ No newline at end of file From 2f806f3ec9ccf49b42ba6025c1626ba0d7d0ea9a Mon Sep 17 00:00:00 2001 From: wanghx Date: Tue, 27 Mar 2018 16:17:54 +0800 Subject: [PATCH 14/19] fix: fixed test app --- test/fixtures/example-ts/test/index.test.ts | 1 - test/ts.test.js | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/test/fixtures/example-ts/test/index.test.ts b/test/fixtures/example-ts/test/index.test.ts index 791f6f2d..25c83639 100644 --- a/test/fixtures/example-ts/test/index.test.ts +++ b/test/fixtures/example-ts/test/index.test.ts @@ -7,7 +7,6 @@ import * as path from 'path'; describe('test/index.test.ts', () => { let app: BaseMockApplication; before(() => { - // tslint:disable-next-line app = mock.app({ typescript: true } as MockOption); return app.ready(); }); diff --git a/test/ts.test.js b/test/ts.test.js index ef9e1ea9..fb984bf4 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -48,7 +48,7 @@ describe('test/ts.test.js', () => { .end(); }); - it.only('should test app', () => { + it('should test app', () => { if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { console.log('skip egg@1'); return; @@ -56,8 +56,8 @@ describe('test/ts.test.js', () => { cwd = path.join(__dirname, './fixtures/example-ts'); return coffee.fork(eggBin, [ 'test', '--ts' ], { cwd }) .debug() - .expect('stdout', /hi, egg, 12345/) - .expect('stdout', /egg started/) + .expect('stdout', /hi, egg, 123456/) + .expect('stdout', /should work/) .expect('code', 0) .end(); }); From d2cd77ee9a093ff918010f86e7cf7eafac5be2b7 Mon Sep 17 00:00:00 2001 From: wanghx Date: Tue, 27 Mar 2018 16:32:47 +0800 Subject: [PATCH 15/19] test: updated ts test --- test/ts.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ts.test.js b/test/ts.test.js index fb984bf4..3993cd02 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -17,7 +17,7 @@ describe('test/ts.test.js', () => { .debug() .expect('stdout', /### egg from ts/) .expect('stdout', /options.typescript=true/) - .expect('stdout', /egg started/) + .expect('stdout', /started/) .expect('code', 0) .end(); }); @@ -43,7 +43,7 @@ describe('test/ts.test.js', () => { return coffee.fork(eggBin, [ 'dev', '--ts' ], { cwd }) .debug() .expect('stdout', /hi, egg, 12345/) - .expect('stdout', /egg started/) + .expect('stdout', /started/) .expect('code', 0) .end(); }); From 1c88d0c9b72961dc230478bcb438b995bdb81634 Mon Sep 17 00:00:00 2001 From: Haoliang Gao Date: Tue, 27 Mar 2018 12:45:08 +0800 Subject: [PATCH 16/19] feat: set EGG_MASTER_CLOSE_TIMEOUT when run dev (#88) --- lib/cmd/dev.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/cmd/dev.js b/lib/cmd/dev.js index 42643c09..3ae25083 100644 --- a/lib/cmd/dev.js +++ b/lib/cmd/dev.js @@ -59,9 +59,13 @@ class DevCommand extends Command { * run(context) { const devArgs = yield this.formatArgs(context); + const env = { + NODE_ENV: 'development', + EGG_MASTER_CLOSE_TIMEOUT: 1000, + }; const options = { execArgv: context.execArgv, - env: Object.assign({ NODE_ENV: 'development' }, context.env), + env: Object.assign(env, context.env), }; debug('%s %j %j, %j', this.serverBin, devArgs, options.execArgv, options.env.NODE_ENV); yield this.helper.forkNode(this.serverBin, devArgs, options); From 4d9d8828e60e28453e0e7b6c4471a3e215f18dd3 Mon Sep 17 00:00:00 2001 From: wanghx Date: Tue, 27 Mar 2018 18:03:37 +0800 Subject: [PATCH 17/19] chore: optimizated tsconfig --- test/fixtures/example-ts/tsconfig.json | 6 ------ test/fixtures/ts/tsconfig.json | 6 ------ 2 files changed, 12 deletions(-) diff --git a/test/fixtures/example-ts/tsconfig.json b/test/fixtures/example-ts/tsconfig.json index 1d70b6b9..506b51bb 100644 --- a/test/fixtures/example-ts/tsconfig.json +++ b/test/fixtures/example-ts/tsconfig.json @@ -4,18 +4,12 @@ "module": "commonjs", "strict": true, "moduleResolution": "node", - "noImplicitAny": false, "experimentalDecorators": true, "emitDecoratorMetadata": true, "charset": "utf8", - "allowJs": false, "pretty": true, - "noEmitOnError": false, "noUnusedLocals": true, "noUnusedParameters": true, - "allowUnreachableCode": false, - "allowUnusedLabels": false, - "strictPropertyInitialization": false, "noFallthroughCasesInSwitch": true, "skipLibCheck": true, "skipDefaultLibCheck": true, diff --git a/test/fixtures/ts/tsconfig.json b/test/fixtures/ts/tsconfig.json index 1d70b6b9..506b51bb 100644 --- a/test/fixtures/ts/tsconfig.json +++ b/test/fixtures/ts/tsconfig.json @@ -4,18 +4,12 @@ "module": "commonjs", "strict": true, "moduleResolution": "node", - "noImplicitAny": false, "experimentalDecorators": true, "emitDecoratorMetadata": true, "charset": "utf8", - "allowJs": false, "pretty": true, - "noEmitOnError": false, "noUnusedLocals": true, "noUnusedParameters": true, - "allowUnreachableCode": false, - "allowUnusedLabels": false, - "strictPropertyInitialization": false, "noFallthroughCasesInSwitch": true, "skipLibCheck": true, "skipDefaultLibCheck": true, From fff57c328eb7d6fc99dfb55064c10c12e440e6f2 Mon Sep 17 00:00:00 2001 From: wanghx Date: Tue, 27 Mar 2018 18:09:12 +0800 Subject: [PATCH 18/19] chore: remove charset in tsconfig --- test/fixtures/example-ts/tsconfig.json | 1 - test/fixtures/ts/tsconfig.json | 1 - 2 files changed, 2 deletions(-) diff --git a/test/fixtures/example-ts/tsconfig.json b/test/fixtures/example-ts/tsconfig.json index 506b51bb..f0836d64 100644 --- a/test/fixtures/example-ts/tsconfig.json +++ b/test/fixtures/example-ts/tsconfig.json @@ -6,7 +6,6 @@ "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true, - "charset": "utf8", "pretty": true, "noUnusedLocals": true, "noUnusedParameters": true, diff --git a/test/fixtures/ts/tsconfig.json b/test/fixtures/ts/tsconfig.json index 506b51bb..f0836d64 100644 --- a/test/fixtures/ts/tsconfig.json +++ b/test/fixtures/ts/tsconfig.json @@ -6,7 +6,6 @@ "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true, - "charset": "utf8", "pretty": true, "noUnusedLocals": true, "noUnusedParameters": true, From 6505b9687a62d7ef088aba326ececc7cc9fd4cc4 Mon Sep 17 00:00:00 2001 From: wanghx Date: Tue, 27 Mar 2018 18:19:33 +0800 Subject: [PATCH 19/19] chore: change noImplicitAny to false --- test/fixtures/example-ts/tsconfig.json | 1 + test/fixtures/ts/tsconfig.json | 1 + 2 files changed, 2 insertions(+) diff --git a/test/fixtures/example-ts/tsconfig.json b/test/fixtures/example-ts/tsconfig.json index f0836d64..4f10abf7 100644 --- a/test/fixtures/example-ts/tsconfig.json +++ b/test/fixtures/example-ts/tsconfig.json @@ -3,6 +3,7 @@ "target": "es2017", "module": "commonjs", "strict": true, + "noImplicitAny": false, "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true, diff --git a/test/fixtures/ts/tsconfig.json b/test/fixtures/ts/tsconfig.json index f0836d64..4f10abf7 100644 --- a/test/fixtures/ts/tsconfig.json +++ b/test/fixtures/ts/tsconfig.json @@ -3,6 +3,7 @@ "target": "es2017", "module": "commonjs", "strict": true, + "noImplicitAny": false, "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true,