@eggjs/mock 7.0.0-beta.19 → 7.0.0-beta.20
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/agent-BKYkjoCx.js +111 -0
- package/dist/agent-CHCe8tnW.d.ts +24 -0
- package/dist/agent-DZ_fHoxJ.js +48 -0
- package/dist/agent_handler-DeUtoRVT.js +26 -0
- package/dist/app/extend/agent.d.ts +1 -1
- package/dist/app/extend/agent.js +3 -46
- package/dist/app/extend/application.d.ts +4 -174
- package/dist/app/extend/application.js +7 -383
- package/dist/app-6Bn3F5Uw.js +252 -0
- package/dist/app-CoypamK1.js +99 -0
- package/dist/app-l47a-gqT.d.ts +7 -0
- package/dist/app_handler-CkiK6fyC.js +55 -0
- package/dist/app_handler-Gp5J9X-v.d.ts +9 -0
- package/dist/application-BPpvw5dg.d.ts +175 -0
- package/dist/application-LrKwuOKn.js +385 -0
- package/dist/bootstrap.d.ts +9 -3
- package/dist/bootstrap.js +20 -3
- package/dist/cluster-CA_hr9eV.js +283 -0
- package/dist/cluster-RTfUwmUd.d.ts +126 -0
- package/dist/context-BIduDy1M.js +10 -0
- package/dist/format_options-LRnReKde.js +65 -0
- package/dist/index-BtBMDZg8.d.ts +78 -0
- package/dist/index.d.ts +10 -81
- package/dist/index.js +20 -45
- package/dist/inject_context-BxpcF-ds.js +106 -0
- package/dist/inject_mocha-BVf8v6El.js +38 -0
- package/dist/inject_mocha-Daf1Aj8M.d.ts +1 -0
- package/dist/inject_mocha.d.ts +1 -1
- package/dist/inject_mocha.js +21 -32
- package/dist/lib/agent_handler.d.ts +4 -1
- package/dist/lib/agent_handler.js +7 -24
- package/dist/lib/app.d.ts +5 -6
- package/dist/lib/app.js +12 -252
- package/dist/lib/app_handler.d.ts +5 -8
- package/dist/lib/app_handler.js +19 -54
- package/dist/lib/cluster.d.ts +5 -125
- package/dist/lib/cluster.js +5 -281
- package/dist/lib/context.js +1 -8
- package/dist/lib/format_options.d.ts +2 -2
- package/dist/lib/format_options.js +2 -63
- package/dist/lib/inject_context.js +20 -104
- package/dist/lib/mock_agent.js +1 -43
- package/dist/lib/mock_custom_loader.js +1 -33
- package/dist/lib/mock_http_server.js +1 -15
- package/dist/lib/mock_httpclient.d.ts +1 -39
- package/dist/lib/mock_httpclient.js +2 -108
- package/dist/lib/parallel/agent.d.ts +4 -23
- package/dist/lib/parallel/agent.js +6 -109
- package/dist/lib/parallel/app.d.ts +3 -2
- package/dist/lib/parallel/app.js +7 -97
- package/dist/lib/parallel/util.d.ts +1 -5
- package/dist/lib/parallel/util.js +2 -57
- package/dist/lib/restore.d.ts +1 -3
- package/dist/lib/restore.js +7 -14
- package/dist/lib/supertest.d.ts +1 -15
- package/dist/lib/supertest.js +3 -37
- package/dist/lib/types.d.ts +3 -76
- package/dist/lib/types.js +2 -0
- package/dist/lib/utils.js +1 -38
- package/dist/mock_agent-CxT7Q0_N.js +45 -0
- package/dist/mock_custom_loader-f_tHRojG.js +35 -0
- package/dist/mock_http_server-De0r07gr.js +17 -0
- package/dist/mock_httpclient-1EgQkMxi.d.ts +40 -0
- package/dist/mock_httpclient-B6UVI7GS.js +110 -0
- package/dist/register.d.ts +2 -0
- package/dist/register.js +22 -4
- package/dist/restore-BL0cE0KG.d.ts +4 -0
- package/dist/restore-CVQYXquh.js +16 -0
- package/dist/src-D22EjdGo.js +47 -0
- package/dist/supertest-BIds28yL.d.ts +16 -0
- package/dist/supertest-Cg412Los.js +39 -0
- package/dist/types-CPNMyF89.js +1 -0
- package/dist/types-CjZ-JeCo.d.ts +75 -0
- package/dist/util-1xYBaaoh.d.ts +6 -0
- package/dist/util-BpppqTXv.js +59 -0
- package/dist/utils-MhP_Krx1.js +40 -0
- package/package.json +6 -7
- package/dist/_virtual/rolldown_runtime.js +0 -7
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { debuglog } from "node:util";
|
|
2
|
+
import { Dispatcher, HttpClient, MockAgent, getGlobalDispatcher, setGlobalDispatcher } from "urllib";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/mock_agent.ts
|
|
5
|
+
const debug = debuglog("egg/mock/lib/mock_agent");
|
|
6
|
+
globalThis.__mockAgent = null;
|
|
7
|
+
globalThis.__httpClientDispatchers = /* @__PURE__ */ new Map();
|
|
8
|
+
function getMockAgent(app) {
|
|
9
|
+
debug("getMockAgent");
|
|
10
|
+
if (!globalThis.__globalDispatcher) {
|
|
11
|
+
globalThis.__globalDispatcher = getGlobalDispatcher();
|
|
12
|
+
debug("create global dispatcher");
|
|
13
|
+
}
|
|
14
|
+
if (app?.httpClient && !globalThis.__httpClientDispatchers.has(app.httpClient)) {
|
|
15
|
+
globalThis.__httpClientDispatchers.set(app.httpClient, app.httpClient.getDispatcher());
|
|
16
|
+
debug("add new httpClient, size: %d", globalThis.__httpClientDispatchers.size);
|
|
17
|
+
}
|
|
18
|
+
if (!globalThis.__mockAgent) {
|
|
19
|
+
globalThis.__mockAgent = new MockAgent();
|
|
20
|
+
setGlobalDispatcher(globalThis.__mockAgent);
|
|
21
|
+
if (typeof app?.httpClient?.setDispatcher === "function") app.httpClient.setDispatcher(globalThis.__mockAgent);
|
|
22
|
+
debug("create new mockAgent");
|
|
23
|
+
}
|
|
24
|
+
return globalThis.__mockAgent;
|
|
25
|
+
}
|
|
26
|
+
async function restoreMockAgent() {
|
|
27
|
+
debug("restoreMockAgent start");
|
|
28
|
+
if (globalThis.__globalDispatcher) {
|
|
29
|
+
setGlobalDispatcher(globalThis.__globalDispatcher);
|
|
30
|
+
debug("restore global dispatcher");
|
|
31
|
+
}
|
|
32
|
+
debug("restore httpClient, size: %d", globalThis.__httpClientDispatchers.size);
|
|
33
|
+
for (const [httpClient$1, dispatcher] of globalThis.__httpClientDispatchers) httpClient$1.setDispatcher(dispatcher);
|
|
34
|
+
globalThis.__httpClientDispatchers.clear();
|
|
35
|
+
if (globalThis.__mockAgent) {
|
|
36
|
+
const agent = globalThis.__mockAgent;
|
|
37
|
+
globalThis.__mockAgent = null;
|
|
38
|
+
await agent.close();
|
|
39
|
+
debug("close mockAgent");
|
|
40
|
+
}
|
|
41
|
+
debug("restoreMockAgent end");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
export { getMockAgent, restoreMockAgent };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { debuglog } from "node:util";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/mock_custom_loader.ts
|
|
4
|
+
const debug = debuglog("egg/mock/lib/mock_custom_loader");
|
|
5
|
+
function setCustomLoader(app) {
|
|
6
|
+
const customLoader = app.config.customLoader;
|
|
7
|
+
if (!customLoader) return;
|
|
8
|
+
for (const field of Object.keys(customLoader)) {
|
|
9
|
+
const loaderConfig = Object.assign({}, customLoader[field]);
|
|
10
|
+
loaderConfig.field = field;
|
|
11
|
+
addMethod(loaderConfig);
|
|
12
|
+
}
|
|
13
|
+
function addMethod(loaderConfig) {
|
|
14
|
+
const field = loaderConfig.field;
|
|
15
|
+
const appMethodName = "mock" + field.replace(/^[a-z]/i, (s) => s.toUpperCase());
|
|
16
|
+
if (app[appMethodName]) {
|
|
17
|
+
app.coreLogger.warn("Can't override app.%s", appMethodName);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
debug("[addMethod] %s => %j", appMethodName, loaderConfig);
|
|
21
|
+
app[appMethodName] = function(service, methodName, fn) {
|
|
22
|
+
if (typeof service === "string") {
|
|
23
|
+
const arr = service.split(".");
|
|
24
|
+
service = loaderConfig.inject === "ctx" ? this[field + "Classes"] : this[field];
|
|
25
|
+
for (const key of arr) service = service[key];
|
|
26
|
+
service = service.prototype || service;
|
|
27
|
+
}
|
|
28
|
+
this._mockFn(service, methodName, fn);
|
|
29
|
+
return this;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { setCustomLoader };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import http, { Server } from "node:http";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/mock_http_server.ts
|
|
4
|
+
const SERVER = Symbol("http_server");
|
|
5
|
+
function createServer(app) {
|
|
6
|
+
let server = app[SERVER] || app.callback();
|
|
7
|
+
if (typeof server === "function") {
|
|
8
|
+
server = http.createServer(server);
|
|
9
|
+
app[SERVER] = server;
|
|
10
|
+
if (!app.server) app.server = server;
|
|
11
|
+
app.emit("server", server);
|
|
12
|
+
}
|
|
13
|
+
return server;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { createServer };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { BodyInit, Dispatcher, Headers } from "urllib";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/mock_httpclient.d.ts
|
|
4
|
+
interface MockResultOptions {
|
|
5
|
+
data: string | Buffer | Record<string, any>;
|
|
6
|
+
/**
|
|
7
|
+
* http status
|
|
8
|
+
*/
|
|
9
|
+
status?: number;
|
|
10
|
+
/**
|
|
11
|
+
* response header
|
|
12
|
+
*/
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
/**
|
|
15
|
+
* delay the associated reply by a set amount in ms
|
|
16
|
+
*/
|
|
17
|
+
delay?: number;
|
|
18
|
+
/**
|
|
19
|
+
* any matching request will always reply with the defined response indefinitely
|
|
20
|
+
*/
|
|
21
|
+
persist?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* any matching request will reply with the defined response a fixed amount of times
|
|
24
|
+
*/
|
|
25
|
+
repeats?: number;
|
|
26
|
+
}
|
|
27
|
+
type ResultObject = MockResultOptions;
|
|
28
|
+
interface MockResponseCallbackOptions {
|
|
29
|
+
path: string;
|
|
30
|
+
method: string;
|
|
31
|
+
headers?: Headers | Record<string, string>;
|
|
32
|
+
origin?: string;
|
|
33
|
+
body?: BodyInit | Dispatcher.DispatchOptions['body'] | null;
|
|
34
|
+
maxRedirections?: number;
|
|
35
|
+
}
|
|
36
|
+
type MockResultFunction = (url: string, options: MockResponseCallbackOptions) => MockResultOptions | string;
|
|
37
|
+
declare function createMockHttpClient(app: any): (mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string) => void;
|
|
38
|
+
type MockHttpClientMethod = ReturnType<typeof createMockHttpClient>;
|
|
39
|
+
//#endregion
|
|
40
|
+
export { MockHttpClientMethod, MockResponseCallbackOptions, MockResultFunction, MockResultOptions, ResultObject, createMockHttpClient };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { getMockAgent } from "./mock_agent-CxT7Q0_N.js";
|
|
2
|
+
import { mm } from "mm";
|
|
3
|
+
import { extend } from "@eggjs/extend2";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/mock_httpclient.ts
|
|
6
|
+
function normalizeResult(result) {
|
|
7
|
+
if (typeof result === "string") result = { data: result };
|
|
8
|
+
if (!result.status) result.status = 200;
|
|
9
|
+
result.data = result.data || "";
|
|
10
|
+
if (Buffer.isBuffer(result.data)) {} else if (typeof result.data === "object") result.data = Buffer.from(JSON.stringify(result.data));
|
|
11
|
+
else if (typeof result.data === "string") result.data = Buffer.from(result.data);
|
|
12
|
+
else throw new Error("`mockResult.data` must be buffer, string or json");
|
|
13
|
+
result.headers = result.headers ?? {};
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
const MOCK_CONFIGS = Symbol("MOCK_CONFIGS");
|
|
17
|
+
const MOCK_CONFIG_INDEX = Symbol("MOCK_CONFIG_INDEX");
|
|
18
|
+
function createMockHttpClient(app) {
|
|
19
|
+
/**
|
|
20
|
+
* mock httpclient
|
|
21
|
+
* @function mockHttpclient
|
|
22
|
+
* @param {String} mockUrl - url
|
|
23
|
+
* @param {String|Array} mockMethod - http method, default is '*'
|
|
24
|
+
* @param {Object|Function} mockResult - you data
|
|
25
|
+
* - data - buffer / string / json
|
|
26
|
+
* - status - http status
|
|
27
|
+
* - headers - response header
|
|
28
|
+
* - delay - delay the associated reply by a set amount in ms.
|
|
29
|
+
* - persist - any matching request will always reply with the defined response indefinitely, default is true
|
|
30
|
+
* - repeats - number, any matching request will reply with the defined response a fixed amount of times
|
|
31
|
+
*/
|
|
32
|
+
return function mockHttpClient(mockUrl, mockMethod, mockResult) {
|
|
33
|
+
let mockMethods = mockMethod;
|
|
34
|
+
if (!mockResult) {
|
|
35
|
+
mockResult = mockMethod;
|
|
36
|
+
mockMethods = ["*"];
|
|
37
|
+
}
|
|
38
|
+
if (!Array.isArray(mockMethods)) mockMethods = [mockMethods];
|
|
39
|
+
mockMethods = mockMethods.map((method) => (method || "GET").toUpperCase());
|
|
40
|
+
let mockConfigs = app[MOCK_CONFIGS];
|
|
41
|
+
if (!mockConfigs) {
|
|
42
|
+
mockConfigs = [];
|
|
43
|
+
mm(app, MOCK_CONFIGS, mockConfigs);
|
|
44
|
+
}
|
|
45
|
+
let mockConfigIndex = -1;
|
|
46
|
+
let origin = mockUrl;
|
|
47
|
+
let originMethod;
|
|
48
|
+
const pathname = mockUrl;
|
|
49
|
+
let pathMethod;
|
|
50
|
+
if (typeof mockUrl === "string") {
|
|
51
|
+
const urlObject = new URL(mockUrl);
|
|
52
|
+
origin = urlObject.origin;
|
|
53
|
+
const originalPathname = urlObject.pathname;
|
|
54
|
+
pathMethod = (path) => {
|
|
55
|
+
if (path === originalPathname) return true;
|
|
56
|
+
if (path.includes("?")) return path.startsWith(originalPathname);
|
|
57
|
+
return false;
|
|
58
|
+
};
|
|
59
|
+
} else if (mockUrl instanceof RegExp) {
|
|
60
|
+
let requestOrigin = "";
|
|
61
|
+
originMethod = (value) => {
|
|
62
|
+
requestOrigin = value;
|
|
63
|
+
return true;
|
|
64
|
+
};
|
|
65
|
+
pathMethod = (path) => {
|
|
66
|
+
for (const config of mockConfigs) if (config.mockUrl.test(`${requestOrigin}${path}`)) {
|
|
67
|
+
mm(app, MOCK_CONFIG_INDEX, config.mockConfigIndex);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
};
|
|
72
|
+
mockConfigIndex = mockConfigs.length;
|
|
73
|
+
mockConfigs.push({
|
|
74
|
+
mockUrl,
|
|
75
|
+
mockResult,
|
|
76
|
+
mockConfigIndex
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const mockPool = originMethod ? getMockAgent(app).get(originMethod) : getMockAgent(app).get(originMethod ?? origin);
|
|
80
|
+
let persist = true;
|
|
81
|
+
if (typeof mockResult === "object" && typeof mockResult.persist === "boolean") persist = mockResult.persist;
|
|
82
|
+
mockMethods.forEach(function(method) {
|
|
83
|
+
const mockScope = mockPool.intercept({
|
|
84
|
+
path: pathMethod ?? pathname,
|
|
85
|
+
method: method === "*" ? () => true : method
|
|
86
|
+
}).reply((options) => {
|
|
87
|
+
const requestUrl = `${options.origin}${options.path}`;
|
|
88
|
+
let mockRequestResult;
|
|
89
|
+
if (mockConfigIndex >= 0) {
|
|
90
|
+
mockResult = mockConfigs[app[MOCK_CONFIG_INDEX]].mockResult;
|
|
91
|
+
mockRequestResult = typeof mockResult === "function" ? mockResult(requestUrl, options) : mockResult;
|
|
92
|
+
} else mockRequestResult = typeof mockResult === "function" ? mockResult(requestUrl, options) : mockResult;
|
|
93
|
+
const result = extend(true, {}, normalizeResult(mockRequestResult));
|
|
94
|
+
return {
|
|
95
|
+
statusCode: result.status,
|
|
96
|
+
data: result.data,
|
|
97
|
+
responseOptions: { headers: result.headers }
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
if (typeof mockResult === "object") {
|
|
101
|
+
if (mockResult.delay && mockResult.delay > 0) mockScope.delay(mockResult.delay);
|
|
102
|
+
}
|
|
103
|
+
if (persist) mockScope.persist();
|
|
104
|
+
else if (typeof mockResult === "object" && mockResult.repeats && mockResult.repeats > 0) mockScope.times(mockResult.repeats);
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
//#endregion
|
|
110
|
+
export { createMockHttpClient };
|
package/dist/register.d.ts
CHANGED
package/dist/register.js
CHANGED
|
@@ -1,7 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import "./
|
|
1
|
+
import "./inject_mocha-BVf8v6El.js";
|
|
2
|
+
import "./mock_http_server-De0r07gr.js";
|
|
3
|
+
import "./utils-MhP_Krx1.js";
|
|
4
|
+
import "./supertest-Cg412Los.js";
|
|
5
|
+
import "./format_options-LRnReKde.js";
|
|
6
|
+
import "./cluster-CA_hr9eV.js";
|
|
7
|
+
import "./context-BIduDy1M.js";
|
|
8
|
+
import "./mock_custom_loader-f_tHRojG.js";
|
|
9
|
+
import "./mock_agent-CxT7Q0_N.js";
|
|
10
|
+
import "./mock_httpclient-B6UVI7GS.js";
|
|
11
|
+
import "./types-CPNMyF89.js";
|
|
12
|
+
import "./application-LrKwuOKn.js";
|
|
13
|
+
import "./agent-DZ_fHoxJ.js";
|
|
14
|
+
import "./app-6Bn3F5Uw.js";
|
|
15
|
+
import "./restore-CVQYXquh.js";
|
|
16
|
+
import "./util-BpppqTXv.js";
|
|
17
|
+
import "./app-CoypamK1.js";
|
|
18
|
+
import "./agent-BKYkjoCx.js";
|
|
19
|
+
import { closeAgent, setupAgent } from "./agent_handler-DeUtoRVT.js";
|
|
20
|
+
import { getApp } from "./app_handler-CkiK6fyC.js";
|
|
21
|
+
import { proxyMock } from "./src-D22EjdGo.js";
|
|
22
|
+
import "./inject_context-BxpcF-ds.js";
|
|
5
23
|
import { debuglog } from "node:util";
|
|
6
24
|
|
|
7
25
|
//#region src/register.ts
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { restore as restore$1 } from "./cluster-CA_hr9eV.js";
|
|
2
|
+
import { restoreMockAgent } from "./mock_agent-CxT7Q0_N.js";
|
|
3
|
+
import { restore } from "mm";
|
|
4
|
+
import { debuglog } from "node:util";
|
|
5
|
+
|
|
6
|
+
//#region src/lib/restore.ts
|
|
7
|
+
const debug = debuglog("egg/mock/lib/restore");
|
|
8
|
+
async function restore$2() {
|
|
9
|
+
restore();
|
|
10
|
+
await restore$1();
|
|
11
|
+
await restoreMockAgent();
|
|
12
|
+
debug("restore all");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { restore$2 as restore };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createCluster } from "./cluster-CA_hr9eV.js";
|
|
2
|
+
import { createApp } from "./app-6Bn3F5Uw.js";
|
|
3
|
+
import { restore as restore$1 } from "./restore-CVQYXquh.js";
|
|
4
|
+
import { setGetAppCallback } from "./app_handler-CkiK6fyC.js";
|
|
5
|
+
import mm$1, { mock } from "mm";
|
|
6
|
+
|
|
7
|
+
//#region src/index.ts
|
|
8
|
+
const mock$1 = {
|
|
9
|
+
...mm$1,
|
|
10
|
+
restore: restore$1,
|
|
11
|
+
app: createApp,
|
|
12
|
+
cluster: createCluster,
|
|
13
|
+
env(env) {
|
|
14
|
+
mock(process.env, "EGG_MOCK_SERVER_ENV", env);
|
|
15
|
+
mock(process.env, "EGG_SERVER_ENV", env);
|
|
16
|
+
},
|
|
17
|
+
consoleLevel(level) {
|
|
18
|
+
level = (level || "").toUpperCase();
|
|
19
|
+
mock(process.env, "EGG_LOG", level);
|
|
20
|
+
},
|
|
21
|
+
home(homePath) {
|
|
22
|
+
if (homePath) mock(process.env, "EGG_HOME", homePath);
|
|
23
|
+
},
|
|
24
|
+
setGetAppCallback
|
|
25
|
+
};
|
|
26
|
+
const proxyMock = new Proxy(mock, {
|
|
27
|
+
apply(target, _, args) {
|
|
28
|
+
return target(args[0], args[1], args[2]);
|
|
29
|
+
},
|
|
30
|
+
get(_target, property, receiver) {
|
|
31
|
+
return Reflect.get(mock$1, property, receiver);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
var src_default = proxyMock;
|
|
35
|
+
process.setMaxListeners(100);
|
|
36
|
+
process.once("SIGQUIT", () => {
|
|
37
|
+
process.exit(0);
|
|
38
|
+
});
|
|
39
|
+
process.once("SIGTERM", () => {
|
|
40
|
+
process.exit(0);
|
|
41
|
+
});
|
|
42
|
+
process.once("SIGINT", () => {
|
|
43
|
+
process.exit(0);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
//#endregion
|
|
47
|
+
export { proxyMock, src_default };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Request, Test } from "@eggjs/supertest";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/supertest.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Test against the given `app`,
|
|
7
|
+
* returning a new `Test`.
|
|
8
|
+
*/
|
|
9
|
+
declare class EggTestRequest extends Request {
|
|
10
|
+
#private;
|
|
11
|
+
constructor(app: any);
|
|
12
|
+
protected _testRequest(method: string, url: string): Test;
|
|
13
|
+
}
|
|
14
|
+
declare function request(app: any): EggTestRequest;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { EggTestRequest, request };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createServer } from "./mock_http_server-De0r07gr.js";
|
|
2
|
+
import { getSourceDirname } from "./utils-MhP_Krx1.js";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { readJSONSync } from "utility";
|
|
5
|
+
import { Request, Test } from "@eggjs/supertest";
|
|
6
|
+
|
|
7
|
+
//#region src/lib/supertest.ts
|
|
8
|
+
let pkgVersion = "";
|
|
9
|
+
/**
|
|
10
|
+
* Test against the given `app`,
|
|
11
|
+
* returning a new `Test`.
|
|
12
|
+
*/
|
|
13
|
+
var EggTestRequest = class extends Request {
|
|
14
|
+
#app;
|
|
15
|
+
constructor(app) {
|
|
16
|
+
super(createServer(app));
|
|
17
|
+
this.#app = app;
|
|
18
|
+
}
|
|
19
|
+
_testRequest(method, url) {
|
|
20
|
+
if (url[0] !== "/") {
|
|
21
|
+
const realUrl = this.#app.router.pathFor(url);
|
|
22
|
+
if (!realUrl) throw new Error(`Can't find router:${url}, please check your 'app/router.js'`);
|
|
23
|
+
url = realUrl;
|
|
24
|
+
}
|
|
25
|
+
const test = super._testRequest(method, url);
|
|
26
|
+
if (!pkgVersion) {
|
|
27
|
+
const pkgFile = path.join(getSourceDirname(), "../package.json");
|
|
28
|
+
pkgVersion = readJSONSync(pkgFile).version;
|
|
29
|
+
}
|
|
30
|
+
test.set("User-Agent", `@eggjs/mock/${pkgVersion} Node.js/${process.version}`);
|
|
31
|
+
return test;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
function request(app) {
|
|
35
|
+
return new EggTestRequest(app);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { EggTestRequest, request };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { MockAgent as MockAgent$1 } from "urllib";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/types.d.ts
|
|
4
|
+
interface MockOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The mode of the application
|
|
7
|
+
*/
|
|
8
|
+
mode?: 'all-in-one-process' | 'cluster' | 'single';
|
|
9
|
+
/**
|
|
10
|
+
* The directory of the application
|
|
11
|
+
*/
|
|
12
|
+
baseDir?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Custom you plugins
|
|
15
|
+
*/
|
|
16
|
+
plugins?: any;
|
|
17
|
+
/**
|
|
18
|
+
* The directory of the egg framework
|
|
19
|
+
*
|
|
20
|
+
* Set to `true` to use the current directory as framework directory
|
|
21
|
+
*/
|
|
22
|
+
framework?: string | boolean;
|
|
23
|
+
/**
|
|
24
|
+
* current test on plugin
|
|
25
|
+
*/
|
|
26
|
+
plugin?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated please use `framework` instead
|
|
29
|
+
*/
|
|
30
|
+
customEgg?: string | boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Cache application based on baseDir
|
|
33
|
+
*/
|
|
34
|
+
cache?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Switch on process coverage, but it'll be slower
|
|
37
|
+
*/
|
|
38
|
+
coverage?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Remove $baseDir/logs and $baseDir/run before start, default is `true`
|
|
41
|
+
*/
|
|
42
|
+
clean?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* default options.mockCtxStorage value on each mockContext
|
|
45
|
+
*/
|
|
46
|
+
mockCtxStorage?: boolean;
|
|
47
|
+
beforeInit?: (app: any) => Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
interface MockClusterOptions extends MockOptions {
|
|
50
|
+
workers?: number | string;
|
|
51
|
+
cache?: boolean;
|
|
52
|
+
port?: number;
|
|
53
|
+
/**
|
|
54
|
+
* opt pass to coffee, such as { execArgv: ['--debug'] }
|
|
55
|
+
*/
|
|
56
|
+
opt?: object;
|
|
57
|
+
startMode?: 'process' | 'worker_threads';
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @deprecated please use MockOptions instead
|
|
61
|
+
* keep this for compatible
|
|
62
|
+
*/
|
|
63
|
+
type MockOption = MockOptions;
|
|
64
|
+
interface MockApplicationOptions extends MockOptions {
|
|
65
|
+
baseDir: string;
|
|
66
|
+
framework: string;
|
|
67
|
+
clusterPort?: number;
|
|
68
|
+
}
|
|
69
|
+
interface MockClusterApplicationOptions extends MockClusterOptions {
|
|
70
|
+
baseDir: string;
|
|
71
|
+
framework: string;
|
|
72
|
+
port: number;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
export { type MockAgent$1 as MockAgent, MockApplicationOptions, MockClusterApplicationOptions, MockClusterOptions, MockOption, MockOptions };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { getProperty } from "./utils-MhP_Krx1.js";
|
|
2
|
+
import { debuglog } from "node:util";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/parallel/util.ts
|
|
5
|
+
const debug = debuglog("egg/mock/lib/parallel/util");
|
|
6
|
+
const MOCK_APP_METHOD = [
|
|
7
|
+
"ready",
|
|
8
|
+
"isClosed",
|
|
9
|
+
"closed",
|
|
10
|
+
"close",
|
|
11
|
+
"on",
|
|
12
|
+
"once"
|
|
13
|
+
];
|
|
14
|
+
const APP_INIT = Symbol("appInit");
|
|
15
|
+
function proxyApp(app) {
|
|
16
|
+
return new Proxy(app, {
|
|
17
|
+
get(target, prop) {
|
|
18
|
+
if (MOCK_APP_METHOD.includes(prop)) return getProperty(target, prop);
|
|
19
|
+
if (!target[APP_INIT]) throw new Error(`can't get ${prop} before ready`);
|
|
20
|
+
debug("proxy handler.get %s", prop);
|
|
21
|
+
return target._instance[prop];
|
|
22
|
+
},
|
|
23
|
+
set(target, prop, value) {
|
|
24
|
+
if (MOCK_APP_METHOD.includes(prop)) return true;
|
|
25
|
+
if (!target[APP_INIT]) throw new Error(`can't set ${prop} before ready`);
|
|
26
|
+
debug("proxy handler.set %s", prop);
|
|
27
|
+
target._instance[prop] = value;
|
|
28
|
+
return true;
|
|
29
|
+
},
|
|
30
|
+
defineProperty(target, prop, descriptor) {
|
|
31
|
+
if (MOCK_APP_METHOD.includes(prop)) return true;
|
|
32
|
+
if (!target[APP_INIT]) throw new Error(`can't defineProperty ${prop} before ready`);
|
|
33
|
+
debug("proxy handler.defineProperty %s", prop);
|
|
34
|
+
Object.defineProperty(target._instance, prop, descriptor);
|
|
35
|
+
return true;
|
|
36
|
+
},
|
|
37
|
+
deleteProperty(target, prop) {
|
|
38
|
+
if (MOCK_APP_METHOD.includes(prop)) return true;
|
|
39
|
+
if (!target[APP_INIT]) throw new Error(`can't delete ${prop} before ready`);
|
|
40
|
+
debug("proxy handler.deleteProperty %s", prop);
|
|
41
|
+
delete target._instance[prop];
|
|
42
|
+
return true;
|
|
43
|
+
},
|
|
44
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
45
|
+
if (MOCK_APP_METHOD.includes(prop)) return Object.getOwnPropertyDescriptor(target, prop);
|
|
46
|
+
if (!target[APP_INIT]) throw new Error(`can't getOwnPropertyDescriptor ${prop} before ready`);
|
|
47
|
+
debug("proxy handler.getOwnPropertyDescriptor %s", prop);
|
|
48
|
+
return Object.getOwnPropertyDescriptor(target._instance, prop);
|
|
49
|
+
},
|
|
50
|
+
getPrototypeOf(target) {
|
|
51
|
+
if (!target[APP_INIT]) throw new Error("can't getPrototypeOf before ready");
|
|
52
|
+
debug("proxy handler.getPrototypeOf %s");
|
|
53
|
+
return Object.getPrototypeOf(target._instance);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
export { APP_INIT, MOCK_APP_METHOD, proxyApp };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { rmSync } from "node:fs";
|
|
3
|
+
import { rm } from "node:fs/promises";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { scheduler } from "node:timers/promises";
|
|
6
|
+
|
|
7
|
+
//#region src/lib/utils.ts
|
|
8
|
+
function getSourceDirname() {
|
|
9
|
+
if (typeof __dirname !== "undefined") return path.dirname(__dirname);
|
|
10
|
+
return path.dirname(path.dirname(fileURLToPath(import.meta.url)));
|
|
11
|
+
}
|
|
12
|
+
async function sleep(delay) {
|
|
13
|
+
await scheduler.wait(delay);
|
|
14
|
+
}
|
|
15
|
+
async function rimraf(filepath) {
|
|
16
|
+
await rm(filepath, {
|
|
17
|
+
force: true,
|
|
18
|
+
recursive: true
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function rimrafSync(filepath) {
|
|
22
|
+
rmSync(filepath, {
|
|
23
|
+
force: true,
|
|
24
|
+
recursive: true
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function getProperty(target, prop) {
|
|
28
|
+
const member = target[prop];
|
|
29
|
+
if (typeof member === "function") return member.bind(target);
|
|
30
|
+
return member;
|
|
31
|
+
}
|
|
32
|
+
function getEggOptions() {
|
|
33
|
+
return {
|
|
34
|
+
baseDir: process.env.EGG_BASE_DIR ?? process.cwd(),
|
|
35
|
+
framework: process.env.EGG_FRAMEWORK
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
export { getEggOptions, getProperty, getSourceDirname, rimraf, rimrafSync, sleep };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/mock",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.20",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -70,19 +70,18 @@
|
|
|
70
70
|
"detect-port": "^2.1.0",
|
|
71
71
|
"egg-logger": "^3.5.0",
|
|
72
72
|
"get-ready": "^3.1.0",
|
|
73
|
-
"globby": "^11.0.2",
|
|
74
73
|
"is-type-of": "^2.2.0",
|
|
75
74
|
"merge-descriptors": "^2.0.0",
|
|
76
75
|
"mm": "^4.0.2",
|
|
77
76
|
"sdk-base": "^5.0.1",
|
|
78
77
|
"urllib": "^4.8.2",
|
|
79
78
|
"utility": "^2.5.0",
|
|
80
|
-
"@eggjs/supertest": "9.0.0-beta.
|
|
81
|
-
"@eggjs/
|
|
82
|
-
"@eggjs/
|
|
79
|
+
"@eggjs/supertest": "9.0.0-beta.20",
|
|
80
|
+
"@eggjs/utils": "5.0.0-beta.20",
|
|
81
|
+
"@eggjs/extend2": "5.0.0-beta.20"
|
|
83
82
|
},
|
|
84
83
|
"peerDependencies": {
|
|
85
|
-
"egg": "4.1.0-beta.
|
|
84
|
+
"egg": "4.1.0-beta.20"
|
|
86
85
|
},
|
|
87
86
|
"devDependencies": {
|
|
88
87
|
"@eggjs/tegg": "^3.2.2",
|
|
@@ -93,7 +92,7 @@
|
|
|
93
92
|
"egg-errors": "^2.3.2",
|
|
94
93
|
"tsdown": "^0.15.4",
|
|
95
94
|
"typescript": "^5.9.3",
|
|
96
|
-
"@eggjs/tracer": "4.0.0-beta.
|
|
95
|
+
"@eggjs/tracer": "4.0.0-beta.20"
|
|
97
96
|
},
|
|
98
97
|
"scripts": {
|
|
99
98
|
"build": "tsdown",
|