Skip to content

Commit

Permalink
refactor: modify d.ts and support bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
shepherdwind authored and popomore committed Dec 12, 2017
1 parent 27da337 commit be9bcd2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 62 deletions.
2 changes: 1 addition & 1 deletion bootstrap.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
117 changes: 57 additions & 60 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,58 @@
import { Application, Context } from 'egg';

declare module 'egg' {
export interface Application { // tslint:disble-line
ready(): Promise<void>;
close(): Promise<void>;
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<T, C> extends Application { // tslint:disble-line
ready(): Promise<void>;
close(): Promise<void>;
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
*/
Expand Down Expand Up @@ -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<Application, Context> { }

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
Expand All @@ -121,7 +120,5 @@ interface EggMock {
*/
restore(): void;
}

declare var mm: EggMock;

export = mm;
declare const mm: EggMock;
export default mm;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function mock(...args) {
return mm(...args);
}
module.exports = mock;
module.exports.default = mock;

// inherit & extends mm
Object.assign(mock, mm, {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 11 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as mm from '../index';
import mm from '../index';
import * as path from 'path';
const fixtures = path.join(__dirname, 'fixtures');

Expand Down Expand Up @@ -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;

0 comments on commit be9bcd2

Please sign in to comment.