From 48573566a7725f425388955b0b06ee33b925381f Mon Sep 17 00:00:00 2001 From: TZ Date: Sat, 24 Mar 2018 10:42:04 +0800 Subject: [PATCH] feat: add typescript support --- .gitignore | 8 ++++++- lib/cmd/test.js | 2 ++ lib/command.js | 24 ++++++++++++------- package.json | 1 + test/fixtures/require-script.js | 3 +++ .../ts/node_modules/aliyun-egg/index.js | 20 ++++++++++++++++ .../ts/node_modules/aliyun-egg/package.json | 6 +++++ test/fixtures/ts/package.json | 5 +++- test/fixtures/ts/test/ts.test.js | 2 +- test/fixtures/ts/test/ts2.test.ts | 2 +- test/lib/cmd/dev.test.js | 10 ++++++++ test/ts.test.js | 10 ++++---- 12 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/require-script.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 diff --git a/.gitignore b/.gitignore index 348042ed..83e5648a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,13 @@ 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/ diff --git a/lib/cmd/test.js b/lib/cmd/test.js index 33a3e7c0..c15d5e31 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -85,6 +85,7 @@ class TestCommand extends Command { requireArr.push(require.resolve('intelli-espower-loader')); } + // for power-assert if (testArgv.typescript) { requireArr.push('espower-typescript/guess'); } @@ -96,6 +97,7 @@ class TestCommand extends Command { if (!files.length) { files = [ process.env.TESTS || 'test/**/*.test.js' ]; } + // support loading ts test files if (testArgv.typescript) { files.push('test/**/*.test.ts'); } diff --git a/lib/command.js b/lib/command.js index a31c379b..b3512ee8 100644 --- a/lib/command.js +++ b/lib/command.js @@ -30,28 +30,34 @@ class Command extends BaseCommand { get context() { const context = super.context; const { argv, execArgvObj = {}, debugPort } = context; + execArgvObj.require = execArgvObj.require || []; // compatible if (debugPort) context.debug = debugPort; + // add require to execArgv + if (argv.require) { + execArgvObj.require.push(...argv.require); + argv.require = undefined; + } + // remove unuse args argv.$0 = undefined; // execArgv if (argv.typescript) { - execArgvObj.require = execArgvObj.require || []; execArgvObj.require.push(path.join(__dirname, './ts-helper.js')); context.execArgvObj = execArgvObj; - - const self = this; - Object.defineProperty(context, 'execArgv', { - enumerable: true, - get() { - return self.helper.unparseArgv(execArgvObj, { useEquals: false }); - }, - }); } + const self = this; + Object.defineProperty(context, 'execArgv', { + enumerable: true, + get() { + return self.helper.unparseArgv(execArgvObj, { useEquals: false }); + }, + }); + return context; } } diff --git a/package.json b/package.json index 5425dfd6..a6df7f37 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "ypkgfiles": "^1.5.0" }, "devDependencies": { + "@types/mocha": "^5.0.0", "autod": "^3.0.1", "babel": "^6.3.26", "babel-preset-airbnb": "^1.0.1", 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/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 index 6ab5f480..3cb71fd5 100644 --- a/test/fixtures/ts/package.json +++ b/test/fixtures/ts/package.json @@ -1,3 +1,6 @@ { - "name": "ts" + "name": "ts", + "egg": { + "framework": "aliyun-egg" + } } \ No newline at end of file diff --git a/test/fixtures/ts/test/ts.test.js b/test/fixtures/ts/test/ts.test.js index 7352de05..83a27d4c 100644 --- a/test/fixtures/ts/test/ts.test.js +++ b/test/fixtures/ts/test/ts.test.js @@ -12,6 +12,6 @@ describe('ts.test.js', () => { it('should fail', () => { const obj = require('./sub'); console.log('###', obj.default.name); - assert(obj.default.name === 'wrong assert'); + assert(obj.default.name === 'wrong assert ts1'); }); }); diff --git a/test/fixtures/ts/test/ts2.test.ts b/test/fixtures/ts/test/ts2.test.ts index df0e96a7..90a0f402 100644 --- a/test/fixtures/ts/test/ts2.test.ts +++ b/test/fixtures/ts/test/ts2.test.ts @@ -12,6 +12,6 @@ describe('ts2.test.ts', () => { it('should fail', () => { const obj = require('./sub'); console.log('###', obj.default.name); - assert(obj.default.name === 'wrong assert'); + assert(obj.default.name === 'wrong assert ts2'); }); }); 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 index ba63f9c5..5c15c02b 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -4,18 +4,18 @@ const path = require('path'); const coffee = require('coffee'); const mm = require('mm'); -describe.only('test/ts.test.js', () => { +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', () => { - // TODO: should load test/**/*.test.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(); @@ -24,8 +24,10 @@ describe.only('test/ts.test.js', () => { it('should support ts test', () => { mm(process.env, 'NODE_ENV', 'development'); return coffee.fork(eggBin, [ 'test', '--typescript' ], { cwd }) - .debug() - .expect('stdout', /wrong assert/) + // .debug() + .notExpect('stdout', /false == true/) + .expect('stdout', /--- \[string\] 'wrong assert ts1'/) + .expect('stdout', /--- \[string\] 'wrong assert ts2'/) .expect('code', 1) .end(); });