Skip to content

Commit

Permalink
feat: add typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Mar 24, 2018
1 parent 6bd7aa5 commit 4857356
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 17 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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/
Expand Down
2 changes: 2 additions & 0 deletions lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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');
}
Expand Down
24 changes: 15 additions & 9 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/require-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

console.log('hey, you require me by --require');
20 changes: 20 additions & 0 deletions test/fixtures/ts/node_modules/aliyun-egg/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/fixtures/ts/node_modules/aliyun-egg/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion test/fixtures/ts/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"name": "ts"
"name": "ts",
"egg": {
"framework": "aliyun-egg"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/ts/test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
2 changes: 1 addition & 1 deletion test/fixtures/ts/test/ts2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
10 changes: 10 additions & 0 deletions test/lib/cmd/dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
10 changes: 6 additions & 4 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});
Expand Down

0 comments on commit 4857356

Please sign in to comment.