diff --git a/lib/cmd/cov.js b/lib/cmd/cov.js index b6c4404d..2dc2b9b1 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -42,7 +42,6 @@ class CovCommand extends Command { * run(context) { const { cwd, argv, execArgv, env } = context; - if (argv.prerequire) { env.EGG_BIN_PREREQUIRE = 'true'; } @@ -73,7 +72,10 @@ class CovCommand extends Command { const opt = { cwd, execArgv, - env: Object.assign({ NODE_ENV: 'test' }, env), + env: Object.assign({ + NODE_ENV: 'test', + EGG_TYPESCRIPT: context.argv.typescript, + }, env), }; // save coverage-xxxx.json to $PWD/coverage diff --git a/lib/command.js b/lib/command.js index 63f2ed1f..544e32b7 100644 --- a/lib/command.js +++ b/lib/command.js @@ -19,6 +19,7 @@ class Command extends BaseCommand { description: 'whether enable typescript support, will load `ts-node/register` etc', type: 'boolean', alias: 'ts', + default: undefined, }, }; } @@ -33,14 +34,16 @@ class Command extends BaseCommand { // remove unuse args argv.$0 = undefined; - // read `egg.typescript` from package.json - let baseDir = argv._[0] || argv.baseDir || cwd; - if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir); - const pkgFile = path.join(baseDir, 'package.json'); - if (fs.existsSync(pkgFile)) { - const pkgInfo = require(pkgFile); - if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript) { - argv.typescript = true; + // read `egg.typescript` from package.json if not pass argv + if (argv.typescript === undefined) { + let baseDir = argv._[0] || argv.baseDir || cwd; + if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir); + const pkgFile = path.join(baseDir, 'package.json'); + if (fs.existsSync(pkgFile)) { + const pkgInfo = require(pkgFile); + if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript === true) { + argv.typescript = true; + } } } diff --git a/test/fixtures/example-ts-pkg/agent.js b/test/fixtures/example-ts-pkg/agent.js new file mode 100644 index 00000000..2fbe626b --- /dev/null +++ b/test/fixtures/example-ts-pkg/agent.js @@ -0,0 +1,6 @@ +'use strict'; + + +module.exports = agent => { + console.log(`agent.options.typescript = ${agent.options.typescript}`); +}; diff --git a/test/fixtures/example-ts-pkg/test/index.test.ts b/test/fixtures/example-ts-pkg/test/index.test.ts index 25c83639..d4599f69 100644 --- a/test/fixtures/example-ts-pkg/test/index.test.ts +++ b/test/fixtures/example-ts-pkg/test/index.test.ts @@ -2,7 +2,6 @@ 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; diff --git a/test/ts.test.js b/test/ts.test.js index 0938b4a3..bff49e3d 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -90,6 +90,15 @@ describe('test/ts.test.js', () => { .end(); }); + it('should fail start app with --no-ts', () => { + return coffee.fork(eggBin, [ 'dev', '--no-ts' ], { cwd }) + // .debug() + .expect('stdout', /agent.options.typescript = false/) + .expect('stdout', /started/) + .expect('code', 0) + .end(); + }); + it('should start app with relative path', () => { return coffee.fork(eggBin, [ 'dev', './example-ts-pkg' ], { cwd: path.dirname(cwd) }) // .debug()