From 3b370ef998b65e1a5f0390a7aa5f0fb4d2e96a7d Mon Sep 17 00:00:00 2001 From: dxd Date: Fri, 27 Sep 2019 16:33:13 +0800 Subject: [PATCH] fix: nyc shim (#138) --- .gitignore | 1 + .travis.yml | 13 +++++----- lib/cmd/cov.js | 5 ++++ test/fixtures/example-ts-cluster/app.ts | 8 ++++++ .../example-ts-cluster/app/controller/home.ts | 11 ++++++++ .../fixtures/example-ts-cluster/app/router.ts | 7 ++++++ .../config/config.default.ts | 7 ++++++ .../node_modules/egg/index.js | 8 ++++++ .../node_modules/egg/package.json | 3 +++ test/fixtures/example-ts-cluster/package.json | 6 +++++ .../example-ts-cluster/test/index.test.ts | 25 +++++++++++++++++++ .../fixtures/example-ts-cluster/tsconfig.json | 19 ++++++++++++++ .../typings/app/controller/index.d.ts | 10 ++++++++ .../example-ts-cluster/typings/global.d.ts | 3 +++ .../example-ts-cluster/typings/index.d.ts | 7 ++++++ test/ts.test.js | 9 +++++++ 16 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/example-ts-cluster/app.ts create mode 100644 test/fixtures/example-ts-cluster/app/controller/home.ts create mode 100644 test/fixtures/example-ts-cluster/app/router.ts create mode 100644 test/fixtures/example-ts-cluster/config/config.default.ts create mode 100644 test/fixtures/example-ts-cluster/node_modules/egg/index.js create mode 100644 test/fixtures/example-ts-cluster/node_modules/egg/package.json create mode 100644 test/fixtures/example-ts-cluster/package.json create mode 100644 test/fixtures/example-ts-cluster/test/index.test.ts create mode 100644 test/fixtures/example-ts-cluster/tsconfig.json create mode 100644 test/fixtures/example-ts-cluster/typings/app/controller/index.d.ts create mode 100644 test/fixtures/example-ts-cluster/typings/global.d.ts create mode 100644 test/fixtures/example-ts-cluster/typings/index.d.ts diff --git a/.gitignore b/.gitignore index 2d4a0d45..dc9d2e32 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ test/fixtures/ts/node_modules/aliyun-egg/ !test/fixtures/test-files-glob/** !test/fixtures/test-files-stack/node_modules/ !test/fixtures/example/node_modules/ +!test/fixtures/example-ts-cluster/node_modules/ !test/fixtures/example-ts-error-stack/node_modules/ !test/fixtures/egg-require/node_modules/ test/fixtures/example-ts-ets/typings/ diff --git a/.travis.yml b/.travis.yml index 924dff05..559b54f1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,19 @@ sudo: false language: node_js node_js: - - '6' - - '8' - - '10' + - "6" + - "8" + - "10" env: - EGG_VERSION=1 - EGG_VERSION=2 matrix: exclude: - - node_js: '6' - env: EGG_VERSION=2 + - node_js: "6" + env: EGG_VERSION=2 +before_install: + - npm install npminstall -g install: - - npm i npminstall - sed -i.bak '/"egg":/d' package.json - npminstall -d script: diff --git a/lib/cmd/cov.js b/lib/cmd/cov.js index 9cf2b7e3..b4c1fa34 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -83,6 +83,11 @@ class CovCommand extends Command { }, env), }; + // https://github.com/eggjs/egg/issues/3930 + if (context.argv.typescript) { + opt.env.SPAWN_WRAP_SHIM_ROOT = path.join(cwd, 'node_modules'); + } + // save coverage-xxxx.json to $PWD/coverage const covArgs = yield this.getCovArgs(context); if (!covArgs) return; diff --git a/test/fixtures/example-ts-cluster/app.ts b/test/fixtures/example-ts-cluster/app.ts new file mode 100644 index 00000000..b9d7d67c --- /dev/null +++ b/test/fixtures/example-ts-cluster/app.ts @@ -0,0 +1,8 @@ +'use strict'; + +import { Application } from 'egg'; + +export default (app: Application) => { + console.log(`hi, egg, ${app.config.keys}`); + console.log(`ts env: ${process.env.EGG_TYPESCRIPT}`); +}; diff --git a/test/fixtures/example-ts-cluster/app/controller/home.ts b/test/fixtures/example-ts-cluster/app/controller/home.ts new file mode 100644 index 00000000..58053862 --- /dev/null +++ b/test/fixtures/example-ts-cluster/app/controller/home.ts @@ -0,0 +1,11 @@ +'use strict'; + +import { Controller } from 'egg'; + +export default class HomeController extends Controller { + public async index() { + const obj: PlainObject = {}; + obj.text = 'hi, egg'; + this.ctx.body = obj.text; + } +} diff --git a/test/fixtures/example-ts-cluster/app/router.ts b/test/fixtures/example-ts-cluster/app/router.ts new file mode 100644 index 00000000..f8cf3f6f --- /dev/null +++ b/test/fixtures/example-ts-cluster/app/router.ts @@ -0,0 +1,7 @@ +'use strict'; + +import { Application } from 'egg'; + +export default (app: Application) => { + app.router.get('/', app.controller.home.index); +}; \ No newline at end of file diff --git a/test/fixtures/example-ts-cluster/config/config.default.ts b/test/fixtures/example-ts-cluster/config/config.default.ts new file mode 100644 index 00000000..106950bc --- /dev/null +++ b/test/fixtures/example-ts-cluster/config/config.default.ts @@ -0,0 +1,7 @@ +'use strict'; + +export default () => { + const config = {} as any; + config.keys = '123456'; + return config; +}; diff --git a/test/fixtures/example-ts-cluster/node_modules/egg/index.js b/test/fixtures/example-ts-cluster/node_modules/egg/index.js new file mode 100644 index 00000000..7102f7f7 --- /dev/null +++ b/test/fixtures/example-ts-cluster/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); +}, require('os').platform() === 'win32' ? 10000 : 6000); diff --git a/test/fixtures/example-ts-cluster/node_modules/egg/package.json b/test/fixtures/example-ts-cluster/node_modules/egg/package.json new file mode 100644 index 00000000..6697ad3f --- /dev/null +++ b/test/fixtures/example-ts-cluster/node_modules/egg/package.json @@ -0,0 +1,3 @@ +{ + "name": "egg" +} diff --git a/test/fixtures/example-ts-cluster/package.json b/test/fixtures/example-ts-cluster/package.json new file mode 100644 index 00000000..24d8c3e5 --- /dev/null +++ b/test/fixtures/example-ts-cluster/package.json @@ -0,0 +1,6 @@ +{ + "name": "example-ts", + "egg": { + "typescript": true + } +} \ No newline at end of file diff --git a/test/fixtures/example-ts-cluster/test/index.test.ts b/test/fixtures/example-ts-cluster/test/index.test.ts new file mode 100644 index 00000000..a66d4387 --- /dev/null +++ b/test/fixtures/example-ts-cluster/test/index.test.ts @@ -0,0 +1,25 @@ +"use strict"; + +import mm, { MockOption } from "egg-mock"; +import request = require("supertest"); + +describe("test/index.test.ts", () => { + let app: any; + before(() => { + app = mm.cluster({ + opt: { + execArgv: ["--require", require.resolve("ts-node/register")] + } + } as MockOption); + return app.ready(); + }); + + after(() => app.close()); + it("should work", async () => { + const req = request(`http://127.0.0.1:${app.port}`); + return req + .get("/") + .expect("hi, egg") + .expect(200); + }); +}); diff --git a/test/fixtures/example-ts-cluster/tsconfig.json b/test/fixtures/example-ts-cluster/tsconfig.json new file mode 100644 index 00000000..4f10abf7 --- /dev/null +++ b/test/fixtures/example-ts-cluster/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "target": "es2017", + "module": "commonjs", + "strict": true, + "noImplicitAny": false, + "moduleResolution": "node", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "pretty": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "skipLibCheck": true, + "skipDefaultLibCheck": true, + "inlineSourceMap": true, + "importHelpers": true + }, +} \ No newline at end of file diff --git a/test/fixtures/example-ts-cluster/typings/app/controller/index.d.ts b/test/fixtures/example-ts-cluster/typings/app/controller/index.d.ts new file mode 100644 index 00000000..de6b5d46 --- /dev/null +++ b/test/fixtures/example-ts-cluster/typings/app/controller/index.d.ts @@ -0,0 +1,10 @@ +// This file was auto created by egg-ts-helper +// Do not modify this file!!!!!!!!! + +import Home from '../../../app/controller/home'; + +declare module 'egg' { + interface IController { + home: Home; + } +} diff --git a/test/fixtures/example-ts-cluster/typings/global.d.ts b/test/fixtures/example-ts-cluster/typings/global.d.ts new file mode 100644 index 00000000..10aa1e9b --- /dev/null +++ b/test/fixtures/example-ts-cluster/typings/global.d.ts @@ -0,0 +1,3 @@ +interface PlainObject extends Object { + [key: string]: any; +} diff --git a/test/fixtures/example-ts-cluster/typings/index.d.ts b/test/fixtures/example-ts-cluster/typings/index.d.ts new file mode 100644 index 00000000..f7e4b522 --- /dev/null +++ b/test/fixtures/example-ts-cluster/typings/index.d.ts @@ -0,0 +1,7 @@ +import { Context } from 'egg'; + +// extend egg +declare module 'egg' { + interface Context { + } +} \ No newline at end of file diff --git a/test/ts.test.js b/test/ts.test.js index 946ad04e..554c6b1b 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -74,6 +74,15 @@ describe('test/ts.test.js', () => { .expect('code', 0) .end(); }); + + it('should cov app in cluster mod', () => { + cwd = path.join(__dirname, './fixtures/example-ts-cluster'); + return coffee.fork(eggBin, [ 'cov', '--ts' ], { cwd }) + // .debug() + .expect('stdout', process.env.NYC_ROOT_ID ? /Coverage summary/ : /Statements.*100%/) + .expect('code', 0) + .end(); + }); }); describe('error stacks', () => {