@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.
Files changed (78) hide show
  1. package/dist/agent-BKYkjoCx.js +111 -0
  2. package/dist/agent-CHCe8tnW.d.ts +24 -0
  3. package/dist/agent-DZ_fHoxJ.js +48 -0
  4. package/dist/agent_handler-DeUtoRVT.js +26 -0
  5. package/dist/app/extend/agent.d.ts +1 -1
  6. package/dist/app/extend/agent.js +3 -46
  7. package/dist/app/extend/application.d.ts +4 -174
  8. package/dist/app/extend/application.js +7 -383
  9. package/dist/app-6Bn3F5Uw.js +252 -0
  10. package/dist/app-CoypamK1.js +99 -0
  11. package/dist/app-l47a-gqT.d.ts +7 -0
  12. package/dist/app_handler-CkiK6fyC.js +55 -0
  13. package/dist/app_handler-Gp5J9X-v.d.ts +9 -0
  14. package/dist/application-BPpvw5dg.d.ts +175 -0
  15. package/dist/application-LrKwuOKn.js +385 -0
  16. package/dist/bootstrap.d.ts +9 -3
  17. package/dist/bootstrap.js +20 -3
  18. package/dist/cluster-CA_hr9eV.js +283 -0
  19. package/dist/cluster-RTfUwmUd.d.ts +126 -0
  20. package/dist/context-BIduDy1M.js +10 -0
  21. package/dist/format_options-LRnReKde.js +65 -0
  22. package/dist/index-BtBMDZg8.d.ts +78 -0
  23. package/dist/index.d.ts +10 -81
  24. package/dist/index.js +20 -45
  25. package/dist/inject_context-BxpcF-ds.js +106 -0
  26. package/dist/inject_mocha-BVf8v6El.js +38 -0
  27. package/dist/inject_mocha-Daf1Aj8M.d.ts +1 -0
  28. package/dist/inject_mocha.d.ts +1 -1
  29. package/dist/inject_mocha.js +21 -32
  30. package/dist/lib/agent_handler.d.ts +4 -1
  31. package/dist/lib/agent_handler.js +7 -24
  32. package/dist/lib/app.d.ts +5 -6
  33. package/dist/lib/app.js +12 -252
  34. package/dist/lib/app_handler.d.ts +5 -8
  35. package/dist/lib/app_handler.js +19 -54
  36. package/dist/lib/cluster.d.ts +5 -125
  37. package/dist/lib/cluster.js +5 -281
  38. package/dist/lib/context.js +1 -8
  39. package/dist/lib/format_options.d.ts +2 -2
  40. package/dist/lib/format_options.js +2 -63
  41. package/dist/lib/inject_context.js +20 -104
  42. package/dist/lib/mock_agent.js +1 -43
  43. package/dist/lib/mock_custom_loader.js +1 -33
  44. package/dist/lib/mock_http_server.js +1 -15
  45. package/dist/lib/mock_httpclient.d.ts +1 -39
  46. package/dist/lib/mock_httpclient.js +2 -108
  47. package/dist/lib/parallel/agent.d.ts +4 -23
  48. package/dist/lib/parallel/agent.js +6 -109
  49. package/dist/lib/parallel/app.d.ts +3 -2
  50. package/dist/lib/parallel/app.js +7 -97
  51. package/dist/lib/parallel/util.d.ts +1 -5
  52. package/dist/lib/parallel/util.js +2 -57
  53. package/dist/lib/restore.d.ts +1 -3
  54. package/dist/lib/restore.js +7 -14
  55. package/dist/lib/supertest.d.ts +1 -15
  56. package/dist/lib/supertest.js +3 -37
  57. package/dist/lib/types.d.ts +3 -76
  58. package/dist/lib/types.js +2 -0
  59. package/dist/lib/utils.js +1 -38
  60. package/dist/mock_agent-CxT7Q0_N.js +45 -0
  61. package/dist/mock_custom_loader-f_tHRojG.js +35 -0
  62. package/dist/mock_http_server-De0r07gr.js +17 -0
  63. package/dist/mock_httpclient-1EgQkMxi.d.ts +40 -0
  64. package/dist/mock_httpclient-B6UVI7GS.js +110 -0
  65. package/dist/register.d.ts +2 -0
  66. package/dist/register.js +22 -4
  67. package/dist/restore-BL0cE0KG.d.ts +4 -0
  68. package/dist/restore-CVQYXquh.js +16 -0
  69. package/dist/src-D22EjdGo.js +47 -0
  70. package/dist/supertest-BIds28yL.d.ts +16 -0
  71. package/dist/supertest-Cg412Los.js +39 -0
  72. package/dist/types-CPNMyF89.js +1 -0
  73. package/dist/types-CjZ-JeCo.d.ts +75 -0
  74. package/dist/util-1xYBaaoh.d.ts +6 -0
  75. package/dist/util-BpppqTXv.js +59 -0
  76. package/dist/utils-MhP_Krx1.js +40 -0
  77. package/package.json +6 -7
  78. package/dist/_virtual/rolldown_runtime.js +0 -7
@@ -0,0 +1,111 @@
1
+ import { rimraf, sleep } from "./utils-MhP_Krx1.js";
2
+ import { formatOptions } from "./format_options-LRnReKde.js";
3
+ import { context } from "./context-BIduDy1M.js";
4
+ import { setCustomLoader } from "./mock_custom_loader-f_tHRojG.js";
5
+ import { APP_INIT } from "./util-BpppqTXv.js";
6
+ import path from "node:path";
7
+ import { debuglog } from "node:util";
8
+ import { importModule } from "@eggjs/utils";
9
+ import { Base } from "sdk-base";
10
+ import { detectPort } from "detect-port";
11
+
12
+ //#region src/lib/parallel/agent.ts
13
+ const debug = debuglog("egg/mock/lib/parallel/agent");
14
+ var MockAgent = 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 = this.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.clean !== false) {
31
+ const logDir = path.join(this.options.baseDir, "logs");
32
+ try {
33
+ await rimraf(logDir);
34
+ } catch (err) {
35
+ console.error(`remove log dir ${logDir} failed: ${err.stack}`);
36
+ }
37
+ const runDir = path.join(this.options.baseDir, "run");
38
+ try {
39
+ await rimraf(runDir);
40
+ } catch (err) {
41
+ console.error(`remove run dir ${runDir} failed: ${err.stack}`);
42
+ }
43
+ }
44
+ this.options.clusterPort = await detectPort();
45
+ process.env.CLUSTER_PORT = String(this.options.clusterPort);
46
+ debug("get clusterPort %s", this.options.clusterPort);
47
+ const { Agent } = await importModule(this.options.framework);
48
+ const agent = this._instance = new Agent({ ...this.options });
49
+ Object.assign(agent.context, context);
50
+ setCustomLoader(agent);
51
+ debug("agent instantiate");
52
+ this[APP_INIT] = true;
53
+ debug("this[APP_INIT] = true");
54
+ this.#bindEvents();
55
+ await agent.ready();
56
+ const msg = {
57
+ action: "egg-ready",
58
+ data: this.options
59
+ };
60
+ agent.messenger.onMessage(msg);
61
+ debug("agent ready");
62
+ }
63
+ #bindEvents() {
64
+ debug("bind cache events to agent");
65
+ for (const args of this.#initOnListeners) {
66
+ debug("on(%s), use cache and pass to agent", args);
67
+ this._instance.on(args[0], args[1]);
68
+ this.removeListener(args[0], args[1]);
69
+ }
70
+ for (const args of this.#initOnceListeners) {
71
+ debug("once(%s), use cache and pass to agent", args);
72
+ this._instance.once(args[0], args[1]);
73
+ this.removeListener(args[0], args[1]);
74
+ }
75
+ }
76
+ on(...args) {
77
+ if (this[APP_INIT]) {
78
+ debug("on(%s), pass to agent", args);
79
+ this._instance.on(args[0], args[1]);
80
+ } else {
81
+ debug("on(%s), cache it because agent has not init", args);
82
+ this.#initOnListeners.add(args);
83
+ super.on(args[0], args[1]);
84
+ }
85
+ return this;
86
+ }
87
+ once(...args) {
88
+ if (this[APP_INIT]) {
89
+ debug("once(%s), pass to agent", args);
90
+ this._instance.once(args[0], args[1]);
91
+ } else {
92
+ debug("once(%s), cache it because agent has not init", args);
93
+ this.#initOnceListeners.add(args);
94
+ super.on(args[0], args[1]);
95
+ }
96
+ return this;
97
+ }
98
+ /**
99
+ * close agent
100
+ */
101
+ async _close() {
102
+ if (this._instance) await this._instance.close();
103
+ else await sleep(200);
104
+ }
105
+ };
106
+ function createAgent(options) {
107
+ return new MockAgent(formatOptions(options));
108
+ }
109
+
110
+ //#endregion
111
+ export { MockAgent, createAgent };
@@ -0,0 +1,24 @@
1
+ import { MockApplicationOptions, MockOptions } from "./types-CjZ-JeCo.js";
2
+ import { APP_INIT } from "./util-1xYBaaoh.js";
3
+ import { Agent } from "egg";
4
+ import { Base } from "sdk-base";
5
+
6
+ //#region src/lib/parallel/agent.d.ts
7
+ declare class MockAgent extends Base {
8
+ #private;
9
+ options: MockApplicationOptions;
10
+ baseDir: string;
11
+ [APP_INIT]: boolean;
12
+ _instance: Agent;
13
+ constructor(options: MockApplicationOptions);
14
+ _init(): Promise<void>;
15
+ on(...args: any[]): this;
16
+ once(...args: any[]): this;
17
+ /**
18
+ * close agent
19
+ */
20
+ _close(): Promise<void>;
21
+ }
22
+ declare function createAgent(options: MockOptions): MockAgent;
23
+ //#endregion
24
+ export { MockAgent, createAgent };
@@ -0,0 +1,48 @@
1
+ import { getMockAgent, restoreMockAgent } from "./mock_agent-CxT7Q0_N.js";
2
+ import { createMockHttpClient } from "./mock_httpclient-B6UVI7GS.js";
3
+ import { Agent } from "egg";
4
+ import { mock, restore } from "mm";
5
+
6
+ //#region src/app/extend/agent.ts
7
+ var AgentUnittest = class extends Agent {
8
+ _mockHttpClient;
9
+ /**
10
+ * mock httpclient
11
+ * @alias mockHttpClient
12
+ * @function App#mockHttpclient
13
+ */
14
+ mockHttpclient(mockUrl, mockMethod, mockResult) {
15
+ return this.mockHttpClient(mockUrl, mockMethod, mockResult);
16
+ }
17
+ /**
18
+ * mock httpclient
19
+ * @function App#mockHttpClient
20
+ */
21
+ mockHttpClient(mockUrl, mockMethod, mockResult) {
22
+ if (!this._mockHttpClient) this._mockHttpClient = createMockHttpClient(this);
23
+ return this._mockHttpClient(mockUrl, mockMethod, mockResult);
24
+ }
25
+ /**
26
+ * get mock httpclient agent
27
+ * @function Agent#mockHttpclientAgent
28
+ */
29
+ mockAgent() {
30
+ return getMockAgent(this);
31
+ }
32
+ async mockAgentRestore() {
33
+ await restoreMockAgent();
34
+ }
35
+ /**
36
+ * @see mm#restore
37
+ * @function Agent#mockRestore
38
+ */
39
+ mockRestore = restore;
40
+ /**
41
+ * @see mm
42
+ * @function Agent#mm
43
+ */
44
+ mm = mock;
45
+ };
46
+
47
+ //#endregion
48
+ export { AgentUnittest };
@@ -0,0 +1,26 @@
1
+ import { getEggOptions } from "./utils-MhP_Krx1.js";
2
+ import { createAgent } from "./agent-BKYkjoCx.js";
3
+ import { debuglog } from "node:util";
4
+
5
+ //#region src/lib/agent_handler.ts
6
+ const debug = debuglog("egg/mock/lib/agent_handler");
7
+ let agent;
8
+ async function setupAgent() {
9
+ debug("setupAgent call, env.ENABLE_MOCHA_PARALLEL: %s, process.env.AUTO_AGENT: %s, agent: %s", process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT, !!agent);
10
+ if (agent) {
11
+ await agent.ready();
12
+ return agent;
13
+ }
14
+ if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
15
+ agent = createAgent(getEggOptions());
16
+ await agent.ready();
17
+ }
18
+ return agent;
19
+ }
20
+ async function closeAgent() {
21
+ debug("setupAgent call, agent: %s", !!agent);
22
+ if (agent) await agent.close();
23
+ }
24
+
25
+ //#endregion
26
+ export { closeAgent, setupAgent };
@@ -1,4 +1,4 @@
1
- import { MockHttpClientMethod, MockResultFunction, MockResultOptions } from "../../lib/mock_httpclient.js";
1
+ import { MockHttpClientMethod, MockResultFunction, MockResultOptions } from "../../mock_httpclient-1EgQkMxi.js";
2
2
  import { Agent } from "egg";
3
3
  import { mock, restore } from "mm";
4
4
  import * as urllib0 from "urllib";
@@ -1,48 +1,5 @@
1
- import { getMockAgent, restoreMockAgent } from "../../lib/mock_agent.js";
2
- import { createMockHttpClient } from "../../lib/mock_httpclient.js";
3
- import { Agent } from "egg";
4
- import { mock, restore } from "mm";
1
+ import "../../mock_agent-CxT7Q0_N.js";
2
+ import "../../mock_httpclient-B6UVI7GS.js";
3
+ import { AgentUnittest } from "../../agent-DZ_fHoxJ.js";
5
4
 
6
- //#region src/app/extend/agent.ts
7
- var AgentUnittest = class extends Agent {
8
- _mockHttpClient;
9
- /**
10
- * mock httpclient
11
- * @alias mockHttpClient
12
- * @function App#mockHttpclient
13
- */
14
- mockHttpclient(mockUrl, mockMethod, mockResult) {
15
- return this.mockHttpClient(mockUrl, mockMethod, mockResult);
16
- }
17
- /**
18
- * mock httpclient
19
- * @function App#mockHttpClient
20
- */
21
- mockHttpClient(mockUrl, mockMethod, mockResult) {
22
- if (!this._mockHttpClient) this._mockHttpClient = createMockHttpClient(this);
23
- return this._mockHttpClient(mockUrl, mockMethod, mockResult);
24
- }
25
- /**
26
- * get mock httpclient agent
27
- * @function Agent#mockHttpclientAgent
28
- */
29
- mockAgent() {
30
- return getMockAgent(this);
31
- }
32
- async mockAgentRestore() {
33
- await restoreMockAgent();
34
- }
35
- /**
36
- * @see mm#restore
37
- * @function Agent#mockRestore
38
- */
39
- mockRestore = restore;
40
- /**
41
- * @see mm
42
- * @function Agent#mm
43
- */
44
- mm = mock;
45
- };
46
-
47
- //#endregion
48
5
  export { AgentUnittest as default };
@@ -1,175 +1,5 @@
1
- import { MockHttpClientMethod, MockResultFunction, MockResultOptions } from "../../lib/mock_httpclient.js";
2
- import { EggTestRequest } from "../../lib/supertest.js";
3
- import { MockOptions } from "../../lib/types.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
1
+ import "../../mock_httpclient-1EgQkMxi.js";
2
+ import "../../supertest-BIds28yL.js";
3
+ import "../../types-CjZ-JeCo.js";
4
+ import { ApplicationUnittest, MockContext, MockContextData, MockContextOptions } from "../../application-BPpvw5dg.js";
175
5
  export { MockContext, MockContextData, MockContextOptions, ApplicationUnittest as default };