1 | import { Application, Context, EggLogger } from 'egg';
|
2 | import { MockMate } from 'mm';
|
3 | import { Test } from 'supertest';
|
4 | import { MockAgent } from 'urllib';
|
5 | import { Suite } from 'mocha';
|
6 |
|
7 | export { MockAgent };
|
8 | interface EggTest extends Test {
|
9 | unexpectHeader(name: string, b?: Function): EggTest;
|
10 | expectHeader(name: string, b?: Function): EggTest;
|
11 | }
|
12 |
|
13 | type Methods = 'get' | 'post' | 'delete' | 'del' | 'put' | 'head' | 'options' | 'patch' | 'trace' | 'connect';
|
14 |
|
15 | export interface BaseMockApplication<T, C> extends Application {
|
16 | ready(): Promise<void>;
|
17 | close(): Promise<void>;
|
18 | callback(): any;
|
19 |
|
20 | |
21 |
|
22 |
|
23 | mockContext(data?: any): C;
|
24 |
|
25 | |
26 |
|
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 |
|
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 |
|
79 | interface ResultObject {
|
80 | data?: string | object;
|
81 | status?: number;
|
82 | headers?: any;
|
83 | delay?: number;
|
84 | persist?: boolean;
|
85 | repeats?: number;
|
86 | }
|
87 |
|
88 | type ResultFunction = (url?: string, opts?: any) => ResultObject | string | void;
|
89 |
|
90 | type MockHttpClientResult = ResultObject | ResultFunction | string;
|
91 |
|
92 | export interface MockOption {
|
93 | |
94 |
|
95 |
|
96 | baseDir?: string;
|
97 |
|
98 | |
99 |
|
100 |
|
101 | plugins?: any;
|
102 |
|
103 | |
104 |
|
105 |
|
106 | framework?: string;
|
107 |
|
108 | |
109 |
|
110 |
|
111 | cache?: boolean;
|
112 |
|
113 | |
114 |
|
115 |
|
116 | coverage?: boolean;
|
117 |
|
118 | |
119 |
|
120 |
|
121 | clean?: boolean;
|
122 | }
|
123 |
|
124 | type EnvType = 'default' | 'test' | 'prod' | 'local' | 'unittest' | string & {};
|
125 | type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE';
|
126 |
|
127 | export interface MockApplication extends BaseMockApplication<Application, Context> { }
|
128 |
|
129 | export interface EggMock extends MockMate {
|
130 | |
131 |
|
132 |
|
133 | app: (option?: MockOption) => MockApplication;
|
134 |
|
135 | |
136 |
|
137 |
|
138 | cluster: (option?: MockOption) => MockApplication;
|
139 |
|
140 | |
141 |
|
142 |
|
143 | env: (env: EnvType) => void;
|
144 |
|
145 | |
146 |
|
147 |
|
148 | consoleLevel: (level: LogLevel) => void;
|
149 |
|
150 | |
151 |
|
152 |
|
153 | home: (homePath: string) => void;
|
154 |
|
155 | |
156 |
|
157 |
|
158 | restore: () => any;
|
159 |
|
160 | |
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | setGetAppCallback: (cb: (suite: Suite) => Promise<MockApplication> ) => void;
|
167 | }
|
168 |
|
169 | declare const mm: EggMock;
|
170 | export default mm;
|