@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,99 @@
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 };
@@ -0,0 +1,7 @@
1
+ import { MockOptions } from "./types-CjZ-JeCo.js";
2
+ import { ApplicationUnittest } from "./application-BPpvw5dg.js";
3
+
4
+ //#region src/lib/app.d.ts
5
+ declare function createApp(createOptions?: MockOptions): ApplicationUnittest;
6
+ //#endregion
7
+ export { createApp };
@@ -0,0 +1,55 @@
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 };
@@ -0,0 +1,9 @@
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 };
@@ -0,0 +1,175 @@
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 };