Skip to content

Commit

Permalink
feat: support typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Mar 23, 2018
1 parent 3bf9b72 commit 6bd7aa5
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,20 @@ class TestCommand extends Command {
requireArr.push(require.resolve('intelli-espower-loader'));
}

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' ];
}
if (testArgv.typescript) {
files.push('test/**/*.test.ts');
}
// expand glob and skip node_modules and fixtures
files = globby.sync(files.concat('!test/**/{fixtures, node_modules}/**/*.test.js'));
files.sort();
Expand All @@ -108,6 +115,7 @@ class TestCommand extends Command {
testArgv.r = undefined;
testArgv.t = undefined;
testArgv.g = undefined;
testArgv.typescript = undefined;

return this.helper.unparseArgv(testArgv);
}
Expand Down
36 changes: 34 additions & 2 deletions lib/command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const path = require('path');
const BaseCommand = require('common-bin');

class Command extends BaseCommand {
Expand All @@ -9,16 +10,47 @@ 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 = {
require: {
description: 'will add to execArgv --require',
type: 'array',
alias: 'r',
},
typescript: {
description: 'whether enable typescript support, will load `ts-node/register` etc',
type: 'boolean',
alias: 'ts',
},
};
}

get context() {
const context = super.context;
const { argv, execArgvObj = {}, debugPort } = 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'));
context.execArgvObj = execArgvObj;

const self = this;
Object.defineProperty(context, 'execArgv', {
enumerable: true,
get() {
return self.helper.unparseArgv(execArgvObj, { useEquals: false });
},
});
}

return context;
}
Expand Down
28 changes: 28 additions & 0 deletions lib/ts-helper.js
Original file line number Diff line number Diff line change
@@ -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,
},
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"egg-utils": "^2.3.0",
"eslint": "^4.18.1",
"eslint-plugin-eggache": "^1.0.0",
"espower-typescript": "^8.1.3",
"globby": "^8.0.1",
"inspector-proxy": "^1.2.1",
"intelli-espower-loader": "^1.0.1",
Expand All @@ -26,6 +27,7 @@
"power-assert": "^1.4.4",
"semver": "^5.5.0",
"test-exclude": "^4.2.0",
"ts-node": "^5.0.1",
"ypkgfiles": "^1.5.0"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/ts/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';


module.exports = app => {
app.logger.info('###', require('./test.ts').default.name);
};
3 changes: 3 additions & 0 deletions test/fixtures/ts/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.key = '12345';
3 changes: 3 additions & 0 deletions test/fixtures/ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ts"
}
3 changes: 3 additions & 0 deletions test/fixtures/ts/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

export default { name: 'egg from ts' };
3 changes: 3 additions & 0 deletions test/fixtures/ts/test/sub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

export default { name: 'egg from ts' };
17 changes: 17 additions & 0 deletions test/fixtures/ts/test/ts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const assert = require('assert');

describe('ts.test.js', () => {
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');
});
});
17 changes: 17 additions & 0 deletions test/fixtures/ts/test/ts2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const assert = require('assert');

describe('ts2.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');
});
});
32 changes: 32 additions & 0 deletions test/ts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const path = require('path');
const coffee = require('coffee');
const mm = require('mm');

describe.only('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', /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()
.expect('stdout', /wrong assert/)
.expect('code', 1)
.end();
});
});

0 comments on commit 6bd7aa5

Please sign in to comment.