UNPKG

3.79 kBTypeScriptView Raw
1import { Application, Context, EggLogger } from 'egg';
2import { MockMate } from 'mm';
3import { Test } from 'supertest';
4import { MockAgent } from 'urllib';
5import { Suite } from 'mocha';
6
7export { MockAgent };
8interface EggTest extends Test {
9 unexpectHeader(name: string, b?: Function): EggTest;
10 expectHeader(name: string, b?: Function): EggTest;
11}
12
13type Methods = 'get' | 'post' | 'delete' | 'del' | 'put' | 'head' | 'options' | 'patch' | 'trace' | 'connect';
14
15export interface BaseMockApplication<T, C> extends Application { // tslint:disble-line
16 ready(): Promise<void>;
17 close(): Promise<void>;
18 callback(): any;
19
20 /**
21 * mock Context
22 */
23 mockContext(data?: any): C;
24
25 /**
26 * mock Context
27 */
28 mockContextScope<R>(fn: (ctx: C) => Promise<R>, data?: any): Promise<R>;
29
30 /**
31 * mock cookie session
32 */
33 mockSession(data: any): T;
34
35 mockCookies(cookies: any): T;
36
37 mockHeaders(headers: any): T;
38
39 /**
40 * Mock service
41 */
42 mockService(service: string, methodName: string, fn: any): T;
43
44 /**
45 * mock service that return error
46 */
47 mockServiceError(service: string, methodName: string, err?: Error): T;
48
49 mockHttpclient(mockUrl: string | RegExp, mockMethod: string | string[], mockResult: MockHttpClientResult): Application;
50
51 mockHttpclient(mockUrl: string | RegExp, mockResult: MockHttpClientResult): Application;
52
53 mockAgent(): MockAgent;
54 mockAgentRestore(): Promise<void>;
55 mockRestore(): Promise<void>;
56
57 /**
58 * mock csrf
59 */
60 mockCsrf(): T;
61
62 /**
63 * http request helper
64 */
65 httpRequest(): {
66 [key in Methods]: (url: string) => EggTest;
67 } & {
68 [key: string]: (url: string) => EggTest;
69 };
70
71 /**
72 * mock logger
73 */
74 mockLog(logger?: EggLogger | string): void;
75 expectLog(expected: string | RegExp, logger?: EggLogger | string): void;
76 notExpectLog(expected: string | RegExp, logger?: EggLogger | string): void;
77}
78
79interface ResultObject {
80 data?: string | object;
81 status?: number;
82 headers?: any;
83 delay?: number;
84 persist?: boolean;
85 repeats?: number;
86}
87
88type ResultFunction = (url?: string, opts?: any) => ResultObject | string | void;
89
90type MockHttpClientResult = ResultObject | ResultFunction | string;
91
92export interface MockOption {
93 /**
94 * The directory of the application
95 */
96 baseDir?: string;
97
98 /**
99 * Custom you plugins
100 */
101 plugins?: any;
102
103 /**
104 * The directory of the egg framework
105 */
106 framework?: string;
107
108 /**
109 * Cache application based on baseDir
110 */
111 cache?: boolean;
112
113 /**
114 * Swtich on process coverage, but it'll be slower
115 */
116 coverage?: boolean;
117
118 /**
119 * Remove $baseDir/logs
120 */
121 clean?: boolean;
122}
123
124type EnvType = 'default' | 'test' | 'prod' | 'local' | 'unittest' | string & {};
125type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE';
126
127export interface MockApplication extends BaseMockApplication<Application, Context> { }
128
129export interface EggMock extends MockMate {
130 /**
131 * Create a egg mocked application
132 */
133 app: (option?: MockOption) => MockApplication;
134
135 /**
136 * Create a mock cluster server, but you can't use API in application, you should test using supertest
137 */
138 cluster: (option?: MockOption) => MockApplication;
139
140 /**
141 * mock the serverEnv of Egg
142 */
143 env: (env: EnvType) => void;
144
145 /**
146 * mock console level
147 */
148 consoleLevel: (level: LogLevel) => void;
149
150 /**
151 * set EGG_HOME path
152 */
153 home: (homePath: string) => void;
154
155 /**
156 * restore mock
157 */
158 restore: () => any;
159
160 /**
161 * If you use mm.app instead of egg-mock/bootstrap to bootstrap app.
162 * Should manually call setGetAppCallback,
163 * then egg-mock will inject ctx for each test case
164 * @param cb
165 */
166 setGetAppCallback: (cb: (suite: Suite) => Promise<MockApplication> ) => void;
167}
168
169declare const mm: EggMock;
170export default mm;