From be9bcd22c91044b0efdbc3db6b8109cf625002b1 Mon Sep 17 00:00:00 2001 From: Eward Song Date: Tue, 12 Dec 2017 17:58:38 +0800 Subject: [PATCH] refactor: modify d.ts and support bootstrap --- bootstrap.js | 2 +- index.d.ts | 117 ++++++++++++++++++++++----------------------- index.js | 1 + package.json | 1 + test/index.test.ts | 12 ++++- 5 files changed, 71 insertions(+), 62 deletions(-) diff --git a/bootstrap.js b/bootstrap.js index 6414db2..2c80e19 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -1,7 +1,7 @@ 'use strict'; const assert = require('power-assert'); -const mock = require('./index'); +const mock = require('./index').default; const options = {}; if (process.env.EGG_BASE_DIR) options.baseDir = process.env.EGG_BASE_DIR; diff --git a/index.d.ts b/index.d.ts index 20ce39c..e22fb25 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,61 +1,58 @@ import { Application, Context } from 'egg'; -declare module 'egg' { - export interface Application { // tslint:disble-line - ready(): Promise; - close(): Promise; - callback(): any; - - /** - * mock Context - */ - mockContext(data?: any): Context; - - /** - * mock cookie session - */ - mockSession(data: any): Application; - - mockCookies(cookies: any): Application; - - mockHeaders(headers: any): Application; - - /** - * Mock service - */ - mockService(service: string, methodName: string, fn: any): Application; - - /** - * mock service that return error - */ - mockServiceError(service: string, methodName: string, err?: Error): Application; - - mockHttpclient(mockUrl: string, mockMethod: string | string[], mockResult: { - data?: Buffer | string | JSON; - status?: number; - headers?: any; - }): Application; - - mockHttpclient(mockUrl: string, mockResult: { - data?: Buffer | string | JSON; - status?: number; - headers?: any; - }): Application; - - /** - * mock csrf - */ - mockCsrf(): Application; - - /** - * http request helper - */ - httpRequest(): any; - } +export interface BaseMockApplication extends Application { // tslint:disble-line + ready(): Promise; + close(): Promise; + callback(): any; + /** + * mock Context + */ + mockContext(data?: any): C; + + /** + * mock cookie session + */ + mockSession(data: any): T; + + mockCookies(cookies: any): T; + + mockHeaders(headers: any): T; + + /** + * Mock service + */ + mockService(service: string, methodName: string, fn: any): T; + + /** + * mock service that return error + */ + mockServiceError(service: string, methodName: string, err?: Error): T; + + mockHttpclient(mockUrl: string, mockMethod: string | string[], mockResult: { + data?: Buffer | string | JSON; + status?: number; + headers?: any; + }): Application; + + mockHttpclient(mockUrl: string, mockResult: { + data?: Buffer | string | JSON; + status?: number; + headers?: any; + }): Application; + + /** + * mock csrf + */ + mockCsrf(): T; + + /** + * http request helper + */ + httpRequest(): any; } -interface MockOption { +export interface MockOption { /** * The directory of the application */ @@ -90,16 +87,18 @@ interface MockOption { type EnvType = 'default' | 'test' | 'prod' | 'local' | 'unittest'; type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE'; -interface EggMock { +interface MockApplication extends BaseMockApplication { } + +export interface EggMock { /** * Create a egg mocked application */ - app(option?: MockOption): Application; + app(option?: MockOption): MockApplication; /** * Create a mock cluster server, but you can't use API in application, you should test using supertest */ - cluster(option?: MockOption): Application; + cluster(option?: MockOption): MockApplication; /** * mock the serverEnv of Egg @@ -121,7 +120,5 @@ interface EggMock { */ restore(): void; } - -declare var mm: EggMock; - -export = mm; +declare const mm: EggMock; +export default mm; diff --git a/index.js b/index.js index a5c419f..852a35e 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ function mock(...args) { return mm(...args); } module.exports = mock; +module.exports.default = mock; // inherit & extends mm Object.assign(mock, mm, { diff --git a/package.json b/package.json index 537e853..d0159bb 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "contributors": "ali-contributors" }, "dependencies": { + "@types/power-assert": "^1.4.29", "await-event": "^2.1.0", "co": "^4.6.0", "coffee": "^4.1.0", diff --git a/test/index.test.ts b/test/index.test.ts index 85d5882..388abba 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,4 +1,4 @@ -import * as mm from '../index'; +import mm from '../index'; import * as path from 'path'; const fixtures = path.join(__dirname, 'fixtures'); @@ -55,3 +55,13 @@ mm.home('a') // mm.cluster(); mm.env('test'); + +// test for bootstrap +(global as any).before = () => {}; +(global as any).afterEach = () => {}; + +import { mock, app as bootApp, assert } from '../bootstrap'; +bootApp.ready; +mock.restore; +assert(true); +mock.app;