@eggjs/mock 7.0.0-beta.20 → 7.0.0-beta.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app/extend/agent.d.ts +33 -38
- package/dist/app/extend/agent.js +46 -5
- package/dist/app/extend/application.d.ts +171 -5
- package/dist/app/extend/application.js +445 -9
- package/dist/app/middleware/cluster_app_mock.d.ts +2 -5
- package/dist/app/middleware/cluster_app_mock.js +99 -96
- package/dist/app.d.ts +5 -9
- package/dist/app.js +18 -17
- package/dist/bootstrap.d.ts +5 -15
- package/dist/bootstrap.js +13 -29
- package/dist/index.d.ts +77 -10
- package/dist/index.js +90 -22
- package/dist/inject_mocha.d.ts +1 -1
- package/dist/inject_mocha.js +35 -23
- package/dist/lib/agent_handler.d.ts +3 -10
- package/dist/lib/agent_handler.js +24 -9
- package/dist/lib/app.d.ts +3 -6
- package/dist/lib/app.js +291 -14
- package/dist/lib/app_handler.d.ts +5 -6
- package/dist/lib/app_handler.js +69 -21
- package/dist/lib/cluster.d.ts +121 -6
- package/dist/lib/cluster.js +340 -7
- package/dist/lib/context.d.ts +1 -4
- package/dist/lib/context.js +11 -3
- package/dist/lib/format_options.d.ts +2 -7
- package/dist/lib/format_options.js +93 -4
- package/dist/lib/inject_context.d.ts +1 -4
- package/dist/lib/inject_context.js +126 -22
- package/dist/lib/mock_agent.d.ts +4 -8
- package/dist/lib/mock_agent.js +45 -3
- package/dist/lib/mock_custom_loader.d.ts +1 -4
- package/dist/lib/mock_custom_loader.js +34 -3
- package/dist/lib/mock_http_server.d.ts +2 -6
- package/dist/lib/mock_http_server.js +18 -3
- package/dist/lib/mock_httpclient.d.ts +36 -2
- package/dist/lib/mock_httpclient.js +146 -4
- package/dist/lib/parallel/agent.d.ts +20 -5
- package/dist/lib/parallel/agent.js +117 -8
- package/dist/lib/parallel/app.d.ts +19 -24
- package/dist/lib/parallel/app.js +111 -9
- package/dist/lib/parallel/util.d.ts +3 -2
- package/dist/lib/parallel/util.js +66 -4
- package/dist/lib/prerequire.d.ts +1 -1
- package/dist/lib/prerequire.js +25 -1
- package/dist/lib/request_call_function.d.ts +1 -1
- package/dist/lib/request_call_function.js +47 -34
- package/dist/lib/restore.d.ts +1 -2
- package/dist/lib/restore.js +13 -9
- package/dist/lib/start-cluster.d.ts +2 -1
- package/dist/lib/start-cluster.js +14 -15
- package/dist/lib/supertest.d.ts +11 -2
- package/dist/lib/supertest.js +40 -5
- package/dist/lib/tmp/empty.d.ts +1 -1
- package/dist/lib/tmp/empty.js +2 -1
- package/dist/lib/types.d.ts +72 -3
- package/dist/lib/types.js +2 -3
- package/dist/lib/utils.d.ts +8 -11
- package/dist/lib/utils.js +69 -3
- package/dist/register.d.ts +7 -11
- package/dist/register.js +41 -55
- package/package.json +7 -7
- package/dist/agent-BKYkjoCx.js +0 -111
- package/dist/agent-CHCe8tnW.d.ts +0 -24
- package/dist/agent-DZ_fHoxJ.js +0 -48
- package/dist/agent_handler-DeUtoRVT.js +0 -26
- package/dist/app-6Bn3F5Uw.js +0 -252
- package/dist/app-CoypamK1.js +0 -99
- package/dist/app-l47a-gqT.d.ts +0 -7
- package/dist/app_handler-CkiK6fyC.js +0 -55
- package/dist/app_handler-Gp5J9X-v.d.ts +0 -9
- package/dist/application-BPpvw5dg.d.ts +0 -175
- package/dist/application-LrKwuOKn.js +0 -385
- package/dist/cluster-CA_hr9eV.js +0 -283
- package/dist/cluster-RTfUwmUd.d.ts +0 -126
- package/dist/context-BIduDy1M.js +0 -10
- package/dist/format_options-LRnReKde.js +0 -65
- package/dist/index-BtBMDZg8.d.ts +0 -78
- package/dist/inject_context-BxpcF-ds.js +0 -106
- package/dist/inject_mocha-BVf8v6El.js +0 -38
- package/dist/inject_mocha-Daf1Aj8M.d.ts +0 -1
- package/dist/mock_agent-CxT7Q0_N.js +0 -45
- package/dist/mock_custom_loader-f_tHRojG.js +0 -35
- package/dist/mock_http_server-De0r07gr.js +0 -17
- package/dist/mock_httpclient-1EgQkMxi.d.ts +0 -40
- package/dist/mock_httpclient-B6UVI7GS.js +0 -110
- package/dist/restore-BL0cE0KG.d.ts +0 -4
- package/dist/restore-CVQYXquh.js +0 -16
- package/dist/src-D22EjdGo.js +0 -47
- package/dist/supertest-BIds28yL.d.ts +0 -16
- package/dist/supertest-Cg412Los.js +0 -39
- package/dist/types-CPNMyF89.js +0 -1
- package/dist/types-CjZ-JeCo.d.ts +0 -75
- package/dist/typings/index.d.ts +0 -1
- package/dist/util-1xYBaaoh.d.ts +0 -6
- package/dist/util-BpppqTXv.js +0 -59
- package/dist/utils-MhP_Krx1.js +0 -40
package/dist/app-6Bn3F5Uw.js
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import { createServer } from "./mock_http_server-De0r07gr.js";
|
|
2
|
-
import { getProperty, getSourceDirname, rimraf, sleep } from "./utils-MhP_Krx1.js";
|
|
3
|
-
import { formatOptions } from "./format_options-LRnReKde.js";
|
|
4
|
-
import { context } from "./context-BIduDy1M.js";
|
|
5
|
-
import { setCustomLoader } from "./mock_custom_loader-f_tHRojG.js";
|
|
6
|
-
import { strict } from "node:assert";
|
|
7
|
-
import path from "node:path";
|
|
8
|
-
import { debuglog } from "node:util";
|
|
9
|
-
import os from "node:os";
|
|
10
|
-
import { importModule } from "@eggjs/utils";
|
|
11
|
-
import { Base } from "sdk-base";
|
|
12
|
-
import { detectPort } from "detect-port";
|
|
13
|
-
|
|
14
|
-
//#region src/lib/app.ts
|
|
15
|
-
const debug = debuglog("egg/mock/lib/app");
|
|
16
|
-
const apps = /* @__PURE__ */ new Map();
|
|
17
|
-
const APP_INIT = Symbol("appInit");
|
|
18
|
-
const MESSENGER = Symbol("messenger");
|
|
19
|
-
const MOCK_APP_METHOD = [
|
|
20
|
-
"ready",
|
|
21
|
-
"closed",
|
|
22
|
-
"isClosed",
|
|
23
|
-
"close",
|
|
24
|
-
"_agent",
|
|
25
|
-
"_app",
|
|
26
|
-
"on",
|
|
27
|
-
"once",
|
|
28
|
-
"then"
|
|
29
|
-
];
|
|
30
|
-
var MockApplicationWorker = class extends Base {
|
|
31
|
-
_agent;
|
|
32
|
-
_app;
|
|
33
|
-
baseDir;
|
|
34
|
-
[APP_INIT] = false;
|
|
35
|
-
_initOnListeners;
|
|
36
|
-
_initOnceListeners;
|
|
37
|
-
constructor(options) {
|
|
38
|
-
super({
|
|
39
|
-
initMethod: "_init",
|
|
40
|
-
...options
|
|
41
|
-
});
|
|
42
|
-
this.baseDir = options.baseDir;
|
|
43
|
-
this._initOnListeners = /* @__PURE__ */ new Set();
|
|
44
|
-
this._initOnceListeners = /* @__PURE__ */ new Set();
|
|
45
|
-
}
|
|
46
|
-
async _init() {
|
|
47
|
-
if (this.options.beforeInit) {
|
|
48
|
-
await this.options.beforeInit(this);
|
|
49
|
-
this.options.beforeInit = void 0;
|
|
50
|
-
}
|
|
51
|
-
if (this.options.clean !== false) {
|
|
52
|
-
const logDir = path.join(this.options.baseDir, "logs");
|
|
53
|
-
try {
|
|
54
|
-
if (os.platform() === "win32") await sleep(1e3);
|
|
55
|
-
await rimraf(logDir);
|
|
56
|
-
} catch (err) {
|
|
57
|
-
console.error(`remove log dir ${logDir} failed: ${err.stack}`);
|
|
58
|
-
}
|
|
59
|
-
const runDir = path.join(this.options.baseDir, "run");
|
|
60
|
-
try {
|
|
61
|
-
if (os.platform() === "win32") await sleep(1e3);
|
|
62
|
-
await rimraf(runDir);
|
|
63
|
-
} catch (err) {
|
|
64
|
-
console.error(`remove run dir ${runDir} failed: ${err.stack}`);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
this.options.clusterPort = await detectPort();
|
|
68
|
-
debug("[init] options: %o", this.options);
|
|
69
|
-
const egg = await importModule(this.options.framework);
|
|
70
|
-
strict(egg.Agent, `should export Agent class from framework ${this.options.framework}`);
|
|
71
|
-
const Agent = egg.Agent;
|
|
72
|
-
const agent = this._agent = new Agent({ ...this.options });
|
|
73
|
-
debug("agent instantiate");
|
|
74
|
-
await agent.ready();
|
|
75
|
-
debug("agent ready");
|
|
76
|
-
const app = this._app = new (bindMessenger(egg.Application, agent))({ ...this.options });
|
|
77
|
-
app.agent = agent;
|
|
78
|
-
Reflect.set(agent, "app", app);
|
|
79
|
-
Object.assign(app.context, context);
|
|
80
|
-
debug("app instantiate");
|
|
81
|
-
this[APP_INIT] = true;
|
|
82
|
-
debug("this[APP_INIT] = true");
|
|
83
|
-
this.#bindEvent();
|
|
84
|
-
debug("http server instantiate");
|
|
85
|
-
createServer(app);
|
|
86
|
-
await app.ready();
|
|
87
|
-
setCustomLoader(app);
|
|
88
|
-
const msg = {
|
|
89
|
-
action: "egg-ready",
|
|
90
|
-
data: this.options
|
|
91
|
-
};
|
|
92
|
-
app.messenger.onMessage(msg);
|
|
93
|
-
agent.messenger.onMessage(msg);
|
|
94
|
-
debug("app ready");
|
|
95
|
-
}
|
|
96
|
-
#bindEvent() {
|
|
97
|
-
debug("bind cache events to app");
|
|
98
|
-
for (const args of this._initOnListeners) {
|
|
99
|
-
debug("on(%s), use cache and pass to app", args);
|
|
100
|
-
this._app.on(args[0], args[1]);
|
|
101
|
-
this.removeListener(args[0], args[1]);
|
|
102
|
-
}
|
|
103
|
-
for (const args of this._initOnceListeners) {
|
|
104
|
-
debug("once(%s), use cache and pass to app", args);
|
|
105
|
-
this._app.once(args[0], args[1]);
|
|
106
|
-
this.removeListener(args[0], args[1]);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
on(...args) {
|
|
110
|
-
if (this[APP_INIT]) {
|
|
111
|
-
debug("on(%s), pass to app", args);
|
|
112
|
-
this._app.on(args[0], args[1]);
|
|
113
|
-
} else {
|
|
114
|
-
debug("on(%s), cache it because app has not init", args);
|
|
115
|
-
this._initOnListeners.add(args);
|
|
116
|
-
super.on(args[0], args[1]);
|
|
117
|
-
}
|
|
118
|
-
return this;
|
|
119
|
-
}
|
|
120
|
-
once(...args) {
|
|
121
|
-
if (this[APP_INIT]) {
|
|
122
|
-
debug("once(%s), pass to app", args);
|
|
123
|
-
this._app.once(args[0], args[1]);
|
|
124
|
-
} else {
|
|
125
|
-
debug("once(%s), cache it because app has not init", args);
|
|
126
|
-
this._initOnceListeners.add(args);
|
|
127
|
-
super.on(args[0], args[1]);
|
|
128
|
-
}
|
|
129
|
-
return this;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* close app
|
|
133
|
-
*/
|
|
134
|
-
async _close() {
|
|
135
|
-
const baseDir = this.baseDir;
|
|
136
|
-
if (this._app) await this._app.close();
|
|
137
|
-
else await sleep(200);
|
|
138
|
-
if (this._agent) await this._agent.close();
|
|
139
|
-
apps.delete(baseDir);
|
|
140
|
-
debug("delete app cache %s, remain %s", baseDir, [...apps.keys()]);
|
|
141
|
-
if (os.platform() === "win32") await sleep(1e3);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* @deprecated please use isClosed instead, keep compatible with old version
|
|
145
|
-
*/
|
|
146
|
-
get closed() {
|
|
147
|
-
return this.isClosed;
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
function createApp(createOptions) {
|
|
151
|
-
const options = formatOptions({
|
|
152
|
-
...createOptions,
|
|
153
|
-
mode: "all-in-one-process"
|
|
154
|
-
});
|
|
155
|
-
debug("[createApp] options: %o", options);
|
|
156
|
-
if (options.cache && apps.has(options.baseDir)) {
|
|
157
|
-
const app = apps.get(options.baseDir);
|
|
158
|
-
if (app && !app.isClosed) {
|
|
159
|
-
debug("use cache app %s", options.baseDir);
|
|
160
|
-
return app;
|
|
161
|
-
}
|
|
162
|
-
apps.delete(options.baseDir);
|
|
163
|
-
}
|
|
164
|
-
const appWorker = new MockApplicationWorker(options);
|
|
165
|
-
const proxyApp = new Proxy(appWorker, {
|
|
166
|
-
get(target, prop) {
|
|
167
|
-
if (MOCK_APP_METHOD.includes(prop)) return getProperty(target, prop);
|
|
168
|
-
if (!target[APP_INIT]) throw new Error(`can't get ${prop} before ready`);
|
|
169
|
-
debug("proxy handler.get %s", prop);
|
|
170
|
-
return target._app[prop];
|
|
171
|
-
},
|
|
172
|
-
set(target, prop, value) {
|
|
173
|
-
if (MOCK_APP_METHOD.includes(prop)) return true;
|
|
174
|
-
if (!target[APP_INIT]) throw new Error(`can't set ${prop} before ready`);
|
|
175
|
-
debug("proxy handler.set %s", prop);
|
|
176
|
-
target._app[prop] = value;
|
|
177
|
-
return true;
|
|
178
|
-
},
|
|
179
|
-
defineProperty(target, prop, descriptor) {
|
|
180
|
-
if (MOCK_APP_METHOD.includes(prop)) return true;
|
|
181
|
-
if (!target[APP_INIT]) throw new Error(`can't defineProperty ${prop} before ready`);
|
|
182
|
-
debug("proxy handler.defineProperty %s", prop);
|
|
183
|
-
Object.defineProperty(target._app, prop, descriptor);
|
|
184
|
-
return true;
|
|
185
|
-
},
|
|
186
|
-
deleteProperty(target, prop) {
|
|
187
|
-
if (MOCK_APP_METHOD.includes(prop)) return true;
|
|
188
|
-
if (!target[APP_INIT]) throw new Error(`can't delete ${prop} before ready`);
|
|
189
|
-
debug("proxy handler.deleteProperty %s", prop);
|
|
190
|
-
delete target._app[prop];
|
|
191
|
-
return true;
|
|
192
|
-
},
|
|
193
|
-
getOwnPropertyDescriptor(target, prop) {
|
|
194
|
-
if (MOCK_APP_METHOD.includes(prop)) return Object.getOwnPropertyDescriptor(target, prop);
|
|
195
|
-
if (!target[APP_INIT]) throw new Error(`can't getOwnPropertyDescriptor ${prop} before ready`);
|
|
196
|
-
debug("proxy handler.getOwnPropertyDescriptor %s", prop);
|
|
197
|
-
return Object.getOwnPropertyDescriptor(target._app, prop);
|
|
198
|
-
},
|
|
199
|
-
getPrototypeOf(target) {
|
|
200
|
-
if (!target[APP_INIT]) throw new Error("can't getPrototypeOf before ready");
|
|
201
|
-
debug("proxy handler.getPrototypeOf %s");
|
|
202
|
-
return Object.getPrototypeOf(target._app);
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
apps.set(options.baseDir, proxyApp);
|
|
206
|
-
return proxyApp;
|
|
207
|
-
}
|
|
208
|
-
function bindMessenger(ApplicationClass, agent) {
|
|
209
|
-
const agentMessenger = agent.messenger;
|
|
210
|
-
return class MessengerApplication extends ApplicationClass {
|
|
211
|
-
[MESSENGER];
|
|
212
|
-
constructor(options) {
|
|
213
|
-
super(options);
|
|
214
|
-
this.messenger.sendRandom = (action, data) => {
|
|
215
|
-
this.messenger.sendToAgent(action, data);
|
|
216
|
-
};
|
|
217
|
-
agentMessenger.on("egg-ready", () => {
|
|
218
|
-
agentMessenger.sendRandom = (action, data) => {
|
|
219
|
-
agentMessenger.sendToApp(action, data);
|
|
220
|
-
return agentMessenger;
|
|
221
|
-
};
|
|
222
|
-
});
|
|
223
|
-
agentMessenger.send = new Proxy(agentMessenger.send, { apply: this._sendMessage.bind(this) });
|
|
224
|
-
}
|
|
225
|
-
_sendMessage(_target, _thisArg, [action, data, to]) {
|
|
226
|
-
const appMessenger = this.messenger;
|
|
227
|
-
setImmediate(() => {
|
|
228
|
-
if (to === "app") appMessenger.onMessage({
|
|
229
|
-
action,
|
|
230
|
-
data
|
|
231
|
-
});
|
|
232
|
-
else agentMessenger.onMessage({
|
|
233
|
-
action,
|
|
234
|
-
data
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
get messenger() {
|
|
239
|
-
return this[MESSENGER];
|
|
240
|
-
}
|
|
241
|
-
set messenger(m) {
|
|
242
|
-
m.send = new Proxy(m.send, { apply: this._sendMessage.bind(this) });
|
|
243
|
-
this[MESSENGER] = m;
|
|
244
|
-
}
|
|
245
|
-
get [Symbol.for("egg#eggPath")]() {
|
|
246
|
-
return path.join(getSourceDirname(), "lib/tmp");
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
//#endregion
|
|
252
|
-
export { createApp };
|
package/dist/app-CoypamK1.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { createServer } from "./mock_http_server-De0r07gr.js";
|
|
2
|
-
import { sleep } from "./utils-MhP_Krx1.js";
|
|
3
|
-
import { formatOptions } from "./format_options-LRnReKde.js";
|
|
4
|
-
import { context } from "./context-BIduDy1M.js";
|
|
5
|
-
import { setCustomLoader } from "./mock_custom_loader-f_tHRojG.js";
|
|
6
|
-
import { APP_INIT, proxyApp } from "./util-BpppqTXv.js";
|
|
7
|
-
import { Application } from "egg";
|
|
8
|
-
import { debuglog } from "node:util";
|
|
9
|
-
import { importModule } from "@eggjs/utils";
|
|
10
|
-
import { Base } from "sdk-base";
|
|
11
|
-
|
|
12
|
-
//#region src/lib/parallel/app.ts
|
|
13
|
-
const debug = debuglog("egg/mock/lib/parallel/app");
|
|
14
|
-
var MockParallelApplication = class extends Base {
|
|
15
|
-
baseDir;
|
|
16
|
-
[APP_INIT] = false;
|
|
17
|
-
#initOnListeners = /* @__PURE__ */ new Set();
|
|
18
|
-
#initOnceListeners = /* @__PURE__ */ new Set();
|
|
19
|
-
_instance;
|
|
20
|
-
constructor(options) {
|
|
21
|
-
super({ initMethod: "_init" });
|
|
22
|
-
this.options = options;
|
|
23
|
-
this.baseDir = options.baseDir;
|
|
24
|
-
}
|
|
25
|
-
async _init() {
|
|
26
|
-
if (this.options.beforeInit) {
|
|
27
|
-
await this.options.beforeInit(this);
|
|
28
|
-
delete this.options.beforeInit;
|
|
29
|
-
}
|
|
30
|
-
if (!this.options.clusterPort) this.options.clusterPort = parseInt(process.env.CLUSTER_PORT);
|
|
31
|
-
if (!this.options.clusterPort) throw new Error("cannot get env.CLUSTER_PORT, parallel run fail");
|
|
32
|
-
debug("get clusterPort %s", this.options.clusterPort);
|
|
33
|
-
const { Application: Application$1 } = await importModule(this.options.framework);
|
|
34
|
-
const app = this._instance = new Application$1({ ...this.options });
|
|
35
|
-
Object.assign(app.context, context);
|
|
36
|
-
setCustomLoader(app);
|
|
37
|
-
debug("app instantiate");
|
|
38
|
-
this[APP_INIT] = true;
|
|
39
|
-
debug("this[APP_INIT] = true");
|
|
40
|
-
this.#bindEvents();
|
|
41
|
-
debug("http server instantiate");
|
|
42
|
-
createServer(app);
|
|
43
|
-
await app.ready();
|
|
44
|
-
const msg = {
|
|
45
|
-
action: "egg-ready",
|
|
46
|
-
data: this.options
|
|
47
|
-
};
|
|
48
|
-
app.messenger.onMessage(msg);
|
|
49
|
-
debug("app ready");
|
|
50
|
-
}
|
|
51
|
-
#bindEvents() {
|
|
52
|
-
for (const args of this.#initOnListeners) {
|
|
53
|
-
debug("on(%s), use cache and pass to app", args);
|
|
54
|
-
this._instance.on(args[0], args[1]);
|
|
55
|
-
this.removeListener(args[0], args[1]);
|
|
56
|
-
}
|
|
57
|
-
for (const args of this.#initOnceListeners) {
|
|
58
|
-
debug("once(%s), use cache and pass to app", args);
|
|
59
|
-
this._instance.once(args[0], args[1]);
|
|
60
|
-
this.removeListener(args[0], args[1]);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
on(...args) {
|
|
64
|
-
if (this[APP_INIT]) {
|
|
65
|
-
debug("on(%s), pass to app", args);
|
|
66
|
-
this._instance.on(args[0], args[1]);
|
|
67
|
-
} else {
|
|
68
|
-
debug("on(%s), cache it because app has not init", args);
|
|
69
|
-
if (this.#initOnListeners) this.#initOnListeners.add(args);
|
|
70
|
-
super.on(args[0], args[1]);
|
|
71
|
-
}
|
|
72
|
-
return this;
|
|
73
|
-
}
|
|
74
|
-
once(...args) {
|
|
75
|
-
if (this[APP_INIT]) {
|
|
76
|
-
debug("once(%s), pass to app", args);
|
|
77
|
-
this._instance.once(args[0], args[1]);
|
|
78
|
-
} else {
|
|
79
|
-
debug("once(%s), cache it because app has not init", args);
|
|
80
|
-
if (this.#initOnceListeners) this.#initOnceListeners.add(args);
|
|
81
|
-
super.on(args[0], args[1]);
|
|
82
|
-
}
|
|
83
|
-
return this;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* close app
|
|
87
|
-
*/
|
|
88
|
-
async _close() {
|
|
89
|
-
if (this._instance) await this._instance.close();
|
|
90
|
-
else await sleep(200);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
function createApp(initOptions) {
|
|
94
|
-
const app = new MockParallelApplication(formatOptions(initOptions));
|
|
95
|
-
return proxyApp(app);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
//#endregion
|
|
99
|
-
export { MockParallelApplication, createApp };
|
package/dist/app-l47a-gqT.d.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { getEggOptions } from "./utils-MhP_Krx1.js";
|
|
2
|
-
import { createApp as createApp$1 } from "./app-6Bn3F5Uw.js";
|
|
3
|
-
import { restore } from "./restore-CVQYXquh.js";
|
|
4
|
-
import { createApp } from "./app-CoypamK1.js";
|
|
5
|
-
import { setupAgent } from "./agent_handler-DeUtoRVT.js";
|
|
6
|
-
import { debuglog } from "node:util";
|
|
7
|
-
|
|
8
|
-
//#region src/lib/app_handler.ts
|
|
9
|
-
const debug = debuglog("egg/mock/lib/app_handler");
|
|
10
|
-
globalThis.__eggMockAppInstance = null;
|
|
11
|
-
function setupApp() {
|
|
12
|
-
let app = globalThis.__eggMockAppInstance;
|
|
13
|
-
if (app) {
|
|
14
|
-
debug("return exists app");
|
|
15
|
-
return app;
|
|
16
|
-
}
|
|
17
|
-
const options = getEggOptions();
|
|
18
|
-
debug("env.ENABLE_MOCHA_PARALLEL: %s, process.env.AUTO_AGENT: %s", process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT);
|
|
19
|
-
if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
|
|
20
|
-
app = createApp({
|
|
21
|
-
...options,
|
|
22
|
-
beforeInit: async (parallelApp) => {
|
|
23
|
-
const agent = await setupAgent();
|
|
24
|
-
parallelApp.options.clusterPort = agent.options.clusterPort;
|
|
25
|
-
debug("mockParallelApp beforeInit get clusterPort: %s", parallelApp.options.clusterPort);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
debug("mockParallelApp app: %s", !!app);
|
|
29
|
-
} else {
|
|
30
|
-
app = createApp$1(options);
|
|
31
|
-
if (typeof beforeAll === "function") beforeAll(() => app.ready());
|
|
32
|
-
if (typeof afterEach === "function") {
|
|
33
|
-
afterEach(() => app.backgroundTasksFinished());
|
|
34
|
-
afterEach(restore);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
globalThis.__eggMockAppInstance = app;
|
|
38
|
-
return app;
|
|
39
|
-
}
|
|
40
|
-
let getAppCallback;
|
|
41
|
-
function setGetAppCallback(cb) {
|
|
42
|
-
getAppCallback = cb;
|
|
43
|
-
}
|
|
44
|
-
async function getApp(suite, test) {
|
|
45
|
-
if (getAppCallback) return getAppCallback(suite, test);
|
|
46
|
-
const app = globalThis.__eggMockAppInstance;
|
|
47
|
-
if (app) await app.ready();
|
|
48
|
-
return app;
|
|
49
|
-
}
|
|
50
|
-
function getBootstrapApp() {
|
|
51
|
-
return globalThis.__eggMockAppInstance;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
//#endregion
|
|
55
|
-
export { getApp, getBootstrapApp, setGetAppCallback, setupApp };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { ApplicationUnittest } from "./application-BPpvw5dg.js";
|
|
2
|
-
|
|
3
|
-
//#region src/lib/app_handler.d.ts
|
|
4
|
-
declare function setupApp(): ApplicationUnittest;
|
|
5
|
-
declare function setGetAppCallback(cb: (suite: unknown, test?: unknown) => any): void;
|
|
6
|
-
declare function getApp(suite?: unknown, test?: unknown): Promise<any>;
|
|
7
|
-
declare function getBootstrapApp(): ApplicationUnittest;
|
|
8
|
-
//#endregion
|
|
9
|
-
export { getApp, getBootstrapApp, setGetAppCallback, setupApp };
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { MockHttpClientMethod, MockResultFunction, MockResultOptions } from "./mock_httpclient-1EgQkMxi.js";
|
|
2
|
-
import { EggTestRequest } from "./supertest-BIds28yL.js";
|
|
3
|
-
import { MockOptions } from "./types-CjZ-JeCo.js";
|
|
4
|
-
import { Application, Context } from "egg";
|
|
5
|
-
import { mock } from "mm";
|
|
6
|
-
import { IncomingMessage } from "node:http";
|
|
7
|
-
import { Logger } from "egg-logger";
|
|
8
|
-
import { MockAgent } from "urllib";
|
|
9
|
-
|
|
10
|
-
//#region src/app/extend/application.d.ts
|
|
11
|
-
interface MockContextOptions {
|
|
12
|
-
/**
|
|
13
|
-
* mock ctxStorage or not, default is `true`
|
|
14
|
-
*/
|
|
15
|
-
mockCtxStorage?: boolean;
|
|
16
|
-
/**
|
|
17
|
-
* reuse ctxStorage or not, default is `true`
|
|
18
|
-
*/
|
|
19
|
-
reuseCtxStorage?: boolean;
|
|
20
|
-
}
|
|
21
|
-
interface MockContextData {
|
|
22
|
-
headers?: Record<string, string | string[]>;
|
|
23
|
-
[key: string]: any;
|
|
24
|
-
}
|
|
25
|
-
interface MockContext extends Context {
|
|
26
|
-
service: any;
|
|
27
|
-
}
|
|
28
|
-
declare abstract class ApplicationUnittest extends Application {
|
|
29
|
-
[key: string]: any;
|
|
30
|
-
options: MockOptions & Application['options'];
|
|
31
|
-
_mockHttpClient?: MockHttpClientMethod;
|
|
32
|
-
agent: NonNullable<Application['agent']>;
|
|
33
|
-
/**
|
|
34
|
-
* mock Context
|
|
35
|
-
* @function App#mockContext
|
|
36
|
-
* @param {Object} data - ctx data
|
|
37
|
-
* @param {Object} [options] - mock ctx options
|
|
38
|
-
* @example
|
|
39
|
-
* ```js
|
|
40
|
-
* const ctx = app.mockContext({
|
|
41
|
-
* user: {
|
|
42
|
-
* name: 'Jason'
|
|
43
|
-
* }
|
|
44
|
-
* });
|
|
45
|
-
* console.log(ctx.user.name); // Jason
|
|
46
|
-
*
|
|
47
|
-
* // controller
|
|
48
|
-
* module.exports = function*() {
|
|
49
|
-
* this.body = this.user.name;
|
|
50
|
-
* };
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
mockContext(data?: MockContextData, options?: MockContextOptions): MockContext;
|
|
54
|
-
mockContextScope(fn: (ctx?: MockContext) => Promise<any>, data?: MockContextData): Promise<any>;
|
|
55
|
-
/**
|
|
56
|
-
* mock cookie session
|
|
57
|
-
* @function App#mockSession
|
|
58
|
-
* @param {Object} data - session object
|
|
59
|
-
*/
|
|
60
|
-
mockSession(data: any): this;
|
|
61
|
-
/**
|
|
62
|
-
* Mock service
|
|
63
|
-
* @function App#mockService
|
|
64
|
-
* @param {String} service - name
|
|
65
|
-
* @param {String} methodName - method
|
|
66
|
-
* @param {Object|Function|Error} fn - mock you data
|
|
67
|
-
*/
|
|
68
|
-
mockService(service: string | any, methodName: string, fn: any): this;
|
|
69
|
-
/**
|
|
70
|
-
* mock service that return error
|
|
71
|
-
* @function App#mockServiceError
|
|
72
|
-
* @param {String} service - name
|
|
73
|
-
* @param {String} methodName - method
|
|
74
|
-
* @param {Error} [err] - error information
|
|
75
|
-
*/
|
|
76
|
-
mockServiceError(service: string | any, methodName: string, err?: string | Error): this;
|
|
77
|
-
_mockFn(obj: any, name: string, data: any): void;
|
|
78
|
-
/**
|
|
79
|
-
* mock request
|
|
80
|
-
* @function App#mockRequest
|
|
81
|
-
* @param {Request} req - mock request
|
|
82
|
-
*/
|
|
83
|
-
mockRequest(req: MockContextData): IncomingMessage;
|
|
84
|
-
/**
|
|
85
|
-
* mock cookies
|
|
86
|
-
* @function App#mockCookies
|
|
87
|
-
*/
|
|
88
|
-
mockCookies(cookies: Record<string, string | string[]>): this;
|
|
89
|
-
/**
|
|
90
|
-
* mock header
|
|
91
|
-
* @function App#mockHeaders
|
|
92
|
-
*/
|
|
93
|
-
mockHeaders(headers: Record<string, string | string[]>): this;
|
|
94
|
-
/**
|
|
95
|
-
* mock csrf
|
|
96
|
-
* @function App#mockCsrf
|
|
97
|
-
* @since 1.11
|
|
98
|
-
*/
|
|
99
|
-
mockCsrf(): this;
|
|
100
|
-
/**
|
|
101
|
-
* mock httpclient
|
|
102
|
-
* @alias mockHttpClient
|
|
103
|
-
* @function App#mockHttpclient
|
|
104
|
-
*/
|
|
105
|
-
mockHttpclient(mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string): this;
|
|
106
|
-
/**
|
|
107
|
-
* mock httpclient
|
|
108
|
-
* @function App#mockHttpClient
|
|
109
|
-
*/
|
|
110
|
-
mockHttpClient(mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string): this;
|
|
111
|
-
/**
|
|
112
|
-
* @deprecated Please use app.mockHttpClient instead of app.mockUrllib
|
|
113
|
-
*/
|
|
114
|
-
mockUrllib(mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string): this;
|
|
115
|
-
/**
|
|
116
|
-
* get mock httpclient agent
|
|
117
|
-
* @function App#mockHttpclientAgent
|
|
118
|
-
*/
|
|
119
|
-
mockAgent(): MockAgent;
|
|
120
|
-
mockAgentRestore(): Promise<void>;
|
|
121
|
-
/**
|
|
122
|
-
* @see mm#restore
|
|
123
|
-
* @function App#mockRestore
|
|
124
|
-
*/
|
|
125
|
-
mockRestore(): Promise<void>;
|
|
126
|
-
/**
|
|
127
|
-
* @see mm
|
|
128
|
-
* @function App#mm
|
|
129
|
-
*/
|
|
130
|
-
get mm(): typeof mock;
|
|
131
|
-
/**
|
|
132
|
-
* override loadAgent
|
|
133
|
-
* @function App#loadAgent
|
|
134
|
-
*/
|
|
135
|
-
loadAgent(): void;
|
|
136
|
-
/**
|
|
137
|
-
* mock serverEnv
|
|
138
|
-
* @function App#mockEnv
|
|
139
|
-
* @param {String} env - serverEnv
|
|
140
|
-
*/
|
|
141
|
-
mockEnv(env: string): this;
|
|
142
|
-
/**
|
|
143
|
-
* http request helper
|
|
144
|
-
* @function App#httpRequest
|
|
145
|
-
* @return {SupertestRequest} req - supertest request
|
|
146
|
-
* @see https://github.com/visionmedia/supertest
|
|
147
|
-
*/
|
|
148
|
-
httpRequest(): EggTestRequest;
|
|
149
|
-
/**
|
|
150
|
-
* collection logger message, then can be use on `expectLog()`
|
|
151
|
-
* @param {String|Logger} [logger] - logger instance, default is `app.logger`
|
|
152
|
-
* @function App#mockLog
|
|
153
|
-
*/
|
|
154
|
-
mockLog(logger?: string | Logger): void;
|
|
155
|
-
__checkExpectLog(expectOrNot: boolean, str: string | RegExp, logger?: string | Logger): void;
|
|
156
|
-
/**
|
|
157
|
-
* expect str/regexp in the logger, if your server disk is slow, please call `mockLog()` first.
|
|
158
|
-
* @param {String|RegExp} str - test str or regexp
|
|
159
|
-
* @param {String|Logger} [logger] - logger instance, default is `ctx.logger`
|
|
160
|
-
* @function App#expectLog
|
|
161
|
-
*/
|
|
162
|
-
expectLog(str: string | RegExp, logger?: string | Logger): void;
|
|
163
|
-
/**
|
|
164
|
-
* not expect str/regexp in the logger, if your server disk is slow, please call `mockLog()` first.
|
|
165
|
-
* @param {String|RegExp} str - test str or regexp
|
|
166
|
-
* @param {String|Logger} [logger] - logger instance, default is `ctx.logger`
|
|
167
|
-
* @function App#notExpectLog
|
|
168
|
-
*/
|
|
169
|
-
notExpectLog(str: string | RegExp, logger?: string | Logger): void;
|
|
170
|
-
backgroundTasksFinished(): Promise<void>;
|
|
171
|
-
get _backgroundTasks(): Promise<any>[];
|
|
172
|
-
set _backgroundTasks(tasks: Promise<any>[]);
|
|
173
|
-
}
|
|
174
|
-
//#endregion
|
|
175
|
-
export { ApplicationUnittest, MockContext, MockContextData, MockContextOptions };
|