Skip to content

Commit

Permalink
feat: add tsd (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
shepherdwind authored and popomore committed Jun 20, 2017
1 parent a4ba2a2 commit 2b1644e
Show file tree
Hide file tree
Showing 10 changed files with 889 additions and 1 deletion.
780 changes: 780 additions & 0 deletions index.d.ts

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"egg"
],
"dependencies": {
"@types/accepts": "^1.3.2",
"@types/koa": "^2.0.39",
"@types/koa-router": "^7.0.22",
"accepts": "^1.3.3",
"agentkeepalive": "^3.2.0",
"cluster-client": "^1.6.4",
Expand Down Expand Up @@ -68,15 +71,19 @@
"runscript": "^1.2.1",
"spy": "^1.0.0",
"supertest": "^3.0.0",
"ts-node": "^3.0.6",
"typescript": "^2.3.4",
"webstorm-disable-index": "^1.1.2"
},
"main": "index.js",
"types": "index.d.ts",
"files": [
"app",
"config",
"bin",
"lib",
"index.js"
"index.js",
"index.d.ts"
],
"scripts": {
"lint": "eslint app config lib test *.js",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/apps/app-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.js
node_modules
18 changes: 18 additions & 0 deletions test/fixtures/apps/app-ts/app/controller/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller } from 'egg';

// add user controller and service
declare module 'egg' {
interface IController {
foo: FooController;
}
}

// controller
export default class FooController extends Controller {
async getData() {
this.ctx.body = await this.ctx.service.foo.bar();
}
async getBar() {
this.ctx.body = await this.service.foo.bar();
}
}
7 changes: 7 additions & 0 deletions test/fixtures/apps/app-ts/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Application } from 'egg';

export default (app: Application) => {
const controller = app.controller;
app.get('/foo', controller.foo.getData);
app.post('/', controller.foo.getData);
}
14 changes: 14 additions & 0 deletions test/fixtures/apps/app-ts/app/service/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Service } from 'egg';

// add user controller and service
declare module 'egg' {
interface IService {
foo: FooService;
}
}

export default class FooService extends Service {
async bar() {
return { env: this.config.env };
}
}
3 changes: 3 additions & 0 deletions test/fixtures/apps/app-ts/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
keys: 'foo',
}
4 changes: 4 additions & 0 deletions test/fixtures/apps/app-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app-ts",
"version": "1.0.0"
}
12 changes: 12 additions & 0 deletions test/fixtures/apps/app-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es6",
"baseUrl": ".",
"paths": {
"egg": ["../../../../index"]
},
"module": "commonjs",
"lib": ["es7"],
"strict": true
}
}
41 changes: 41 additions & 0 deletions test/ts/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const request = require('supertest');
const mm = require('egg-mock');
const runscript = require('runscript');
const path = require('path');
const utils = require('../utils');
const baseDir = path.join(__dirname, '../fixtures/apps/app-ts');

describe('test/ts/index.test.js', () => {
before(function* () {
if (process.env.CI) {
yield runscript('tsc && npmlink ../../../../', { cwd: baseDir });
} else {
yield runscript('tsc && npm link ../../../../', { cwd: baseDir });
}
});

describe('compiler code', () => {

afterEach(mm.restore);
let app;
before(function* () {
app = utils.app('apps/app-ts');
yield app.ready();
});
after(function* () {
yield app.close();
});

it('controller run ok', done => {
request(app.callback())
.get('/foo')
.expect(200)
.expect({ env: 'unittest' })
.end(done);
});
});

});

0 comments on commit 2b1644e

Please sign in to comment.