@eggjs/mock 6.0.7 → 6.1.0-beta.11

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 (214) hide show
  1. package/README.md +36 -57
  2. package/README.zh_CN.md +31 -51
  3. package/dist/_virtual/rolldown_runtime.js +7 -0
  4. package/dist/app/extend/agent.d.ts +39 -0
  5. package/dist/app/extend/agent.js +48 -0
  6. package/dist/app/extend/application.d.ts +175 -0
  7. package/dist/app/extend/application.js +385 -0
  8. package/dist/app/middleware/cluster_app_mock.d.ts +6 -0
  9. package/dist/app/middleware/cluster_app_mock.js +97 -0
  10. package/dist/app.d.ts +10 -0
  11. package/dist/app.js +17 -0
  12. package/dist/bootstrap.d.ts +9 -0
  13. package/dist/bootstrap.js +14 -0
  14. package/dist/index.d.ts +81 -0
  15. package/dist/index.js +47 -0
  16. package/dist/inject_mocha.d.ts +1 -0
  17. package/dist/inject_mocha.js +34 -0
  18. package/dist/lib/agent_handler.d.ts +7 -0
  19. package/dist/lib/agent_handler.js +26 -0
  20. package/dist/lib/app.d.ts +7 -0
  21. package/dist/lib/app.js +251 -0
  22. package/dist/lib/app_handler.d.ts +9 -0
  23. package/dist/lib/app_handler.js +56 -0
  24. package/dist/lib/cluster.d.ts +126 -0
  25. package/dist/lib/cluster.js +283 -0
  26. package/dist/lib/context.d.ts +4 -0
  27. package/dist/lib/context.js +10 -0
  28. package/dist/lib/format_options.d.ts +10 -0
  29. package/dist/lib/format_options.js +65 -0
  30. package/dist/lib/inject_context.d.ts +9 -0
  31. package/dist/lib/inject_context.js +106 -0
  32. package/dist/lib/mock_agent.d.ts +9 -0
  33. package/dist/lib/mock_agent.js +45 -0
  34. package/dist/lib/mock_custom_loader.d.ts +4 -0
  35. package/dist/lib/mock_custom_loader.js +35 -0
  36. package/dist/lib/mock_http_server.d.ts +6 -0
  37. package/dist/lib/mock_http_server.js +17 -0
  38. package/dist/lib/mock_httpclient.d.ts +40 -0
  39. package/dist/lib/mock_httpclient.js +110 -0
  40. package/dist/lib/parallel/agent.d.ts +24 -0
  41. package/dist/lib/parallel/agent.js +111 -0
  42. package/dist/lib/parallel/app.d.ts +24 -0
  43. package/dist/lib/parallel/app.js +99 -0
  44. package/dist/lib/parallel/util.d.ts +6 -0
  45. package/dist/lib/parallel/util.js +59 -0
  46. package/dist/lib/prerequire.d.ts +1 -0
  47. package/dist/lib/prerequire.js +1 -0
  48. package/dist/lib/request_call_function.d.ts +1 -0
  49. package/dist/lib/request_call_function.js +37 -0
  50. package/dist/lib/restore.d.ts +4 -0
  51. package/dist/lib/restore.js +16 -0
  52. package/dist/lib/start-cluster.d.ts +1 -0
  53. package/dist/lib/start-cluster.js +19 -0
  54. package/dist/lib/supertest.d.ts +16 -0
  55. package/dist/lib/supertest.js +39 -0
  56. package/dist/lib/tmp/empty.d.ts +1 -0
  57. package/dist/lib/tmp/empty.js +1 -0
  58. package/{src/lib/types.ts → dist/lib/types.d.ts} +12 -29
  59. package/dist/lib/types.js +1 -0
  60. package/dist/lib/utils.d.ts +12 -0
  61. package/dist/lib/utils.js +40 -0
  62. package/dist/register.d.ts +10 -0
  63. package/dist/register.js +40 -0
  64. package/dist/typings/index.d.ts +1 -0
  65. package/package.json +69 -98
  66. package/dist/commonjs/app/extend/agent.d.ts +0 -34
  67. package/dist/commonjs/app/extend/agent.js +0 -49
  68. package/dist/commonjs/app/extend/application.d.ts +0 -172
  69. package/dist/commonjs/app/extend/application.js +0 -450
  70. package/dist/commonjs/app/middleware/cluster_app_mock.d.ts +0 -3
  71. package/dist/commonjs/app/middleware/cluster_app_mock.js +0 -101
  72. package/dist/commonjs/app.d.ts +0 -6
  73. package/dist/commonjs/app.js +0 -20
  74. package/dist/commonjs/bootstrap.d.ts +0 -5
  75. package/dist/commonjs/bootstrap.js +0 -59
  76. package/dist/commonjs/index.d.ts +0 -77
  77. package/dist/commonjs/index.js +0 -114
  78. package/dist/commonjs/lib/agent_handler.d.ts +0 -3
  79. package/dist/commonjs/lib/agent_handler.js +0 -28
  80. package/dist/commonjs/lib/app.d.ts +0 -3
  81. package/dist/commonjs/lib/app.js +0 -301
  82. package/dist/commonjs/lib/app_handler.d.ts +0 -5
  83. package/dist/commonjs/lib/app_handler.js +0 -71
  84. package/dist/commonjs/lib/cluster.d.ts +0 -114
  85. package/dist/commonjs/lib/cluster.js +0 -337
  86. package/dist/commonjs/lib/context.d.ts +0 -1
  87. package/dist/commonjs/lib/context.js +0 -16
  88. package/dist/commonjs/lib/format_options.d.ts +0 -5
  89. package/dist/commonjs/lib/format_options.js +0 -100
  90. package/dist/commonjs/lib/inject_context.d.ts +0 -6
  91. package/dist/commonjs/lib/inject_context.js +0 -132
  92. package/dist/commonjs/lib/mock_agent.d.ts +0 -5
  93. package/dist/commonjs/lib/mock_agent.js +0 -49
  94. package/dist/commonjs/lib/mock_custom_loader.d.ts +0 -1
  95. package/dist/commonjs/lib/mock_custom_loader.js +0 -37
  96. package/dist/commonjs/lib/mock_http_server.d.ts +0 -2
  97. package/dist/commonjs/lib/mock_http_server.js +0 -24
  98. package/dist/commonjs/lib/mock_httpclient.d.ts +0 -36
  99. package/dist/commonjs/lib/mock_httpclient.js +0 -147
  100. package/dist/commonjs/lib/parallel/agent.d.ts +0 -20
  101. package/dist/commonjs/lib/parallel/agent.js +0 -125
  102. package/dist/commonjs/lib/parallel/app.d.ts +0 -20
  103. package/dist/commonjs/lib/parallel/app.js +0 -115
  104. package/dist/commonjs/lib/parallel/util.d.ts +0 -3
  105. package/dist/commonjs/lib/parallel/util.js +0 -77
  106. package/dist/commonjs/lib/prerequire.d.ts +0 -1
  107. package/dist/commonjs/lib/prerequire.js +0 -26
  108. package/dist/commonjs/lib/request_call_function.d.ts +0 -1
  109. package/dist/commonjs/lib/request_call_function.js +0 -52
  110. package/dist/commonjs/lib/restore.d.ts +0 -1
  111. package/dist/commonjs/lib/restore.js +0 -16
  112. package/dist/commonjs/lib/start-cluster.d.ts +0 -2
  113. package/dist/commonjs/lib/start-cluster.js +0 -23
  114. package/dist/commonjs/lib/supertest.d.ts +0 -11
  115. package/dist/commonjs/lib/supertest.js +0 -48
  116. package/dist/commonjs/lib/tmp/empty.d.ts +0 -1
  117. package/dist/commonjs/lib/tmp/empty.js +0 -3
  118. package/dist/commonjs/lib/types.d.ts +0 -68
  119. package/dist/commonjs/lib/types.js +0 -3
  120. package/dist/commonjs/lib/utils.d.ts +0 -9
  121. package/dist/commonjs/lib/utils.js +0 -80
  122. package/dist/commonjs/package.json +0 -3
  123. package/dist/commonjs/register.d.ts +0 -8
  124. package/dist/commonjs/register.js +0 -80
  125. package/dist/esm/app/extend/agent.d.ts +0 -34
  126. package/dist/esm/app/extend/agent.js +0 -46
  127. package/dist/esm/app/extend/application.d.ts +0 -172
  128. package/dist/esm/app/extend/application.js +0 -444
  129. package/dist/esm/app/middleware/cluster_app_mock.d.ts +0 -3
  130. package/dist/esm/app/middleware/cluster_app_mock.js +0 -99
  131. package/dist/esm/app.d.ts +0 -6
  132. package/dist/esm/app.js +0 -17
  133. package/dist/esm/bootstrap.d.ts +0 -5
  134. package/dist/esm/bootstrap.js +0 -15
  135. package/dist/esm/index.d.ts +0 -77
  136. package/dist/esm/index.js +0 -91
  137. package/dist/esm/lib/agent_handler.d.ts +0 -3
  138. package/dist/esm/lib/agent_handler.js +0 -24
  139. package/dist/esm/lib/app.d.ts +0 -3
  140. package/dist/esm/lib/app.js +0 -295
  141. package/dist/esm/lib/app_handler.d.ts +0 -5
  142. package/dist/esm/lib/app_handler.js +0 -65
  143. package/dist/esm/lib/cluster.d.ts +0 -114
  144. package/dist/esm/lib/cluster.js +0 -328
  145. package/dist/esm/lib/context.d.ts +0 -1
  146. package/dist/esm/lib/context.js +0 -13
  147. package/dist/esm/lib/format_options.d.ts +0 -5
  148. package/dist/esm/lib/format_options.js +0 -94
  149. package/dist/esm/lib/inject_context.d.ts +0 -6
  150. package/dist/esm/lib/inject_context.js +0 -126
  151. package/dist/esm/lib/mock_agent.d.ts +0 -5
  152. package/dist/esm/lib/mock_agent.js +0 -45
  153. package/dist/esm/lib/mock_custom_loader.d.ts +0 -1
  154. package/dist/esm/lib/mock_custom_loader.js +0 -34
  155. package/dist/esm/lib/mock_http_server.d.ts +0 -2
  156. package/dist/esm/lib/mock_http_server.js +0 -18
  157. package/dist/esm/lib/mock_httpclient.d.ts +0 -36
  158. package/dist/esm/lib/mock_httpclient.js +0 -144
  159. package/dist/esm/lib/parallel/agent.d.ts +0 -20
  160. package/dist/esm/lib/parallel/agent.js +0 -117
  161. package/dist/esm/lib/parallel/app.d.ts +0 -20
  162. package/dist/esm/lib/parallel/app.js +0 -110
  163. package/dist/esm/lib/parallel/util.d.ts +0 -3
  164. package/dist/esm/lib/parallel/util.js +0 -73
  165. package/dist/esm/lib/prerequire.d.ts +0 -1
  166. package/dist/esm/lib/prerequire.js +0 -25
  167. package/dist/esm/lib/request_call_function.d.ts +0 -1
  168. package/dist/esm/lib/request_call_function.js +0 -47
  169. package/dist/esm/lib/restore.d.ts +0 -1
  170. package/dist/esm/lib/restore.js +0 -13
  171. package/dist/esm/lib/start-cluster.d.ts +0 -2
  172. package/dist/esm/lib/start-cluster.js +0 -18
  173. package/dist/esm/lib/supertest.d.ts +0 -11
  174. package/dist/esm/lib/supertest.js +0 -40
  175. package/dist/esm/lib/tmp/empty.d.ts +0 -1
  176. package/dist/esm/lib/tmp/empty.js +0 -2
  177. package/dist/esm/lib/types.d.ts +0 -68
  178. package/dist/esm/lib/types.js +0 -2
  179. package/dist/esm/lib/utils.d.ts +0 -9
  180. package/dist/esm/lib/utils.js +0 -69
  181. package/dist/esm/package.json +0 -3
  182. package/dist/esm/register.d.ts +0 -8
  183. package/dist/esm/register.js +0 -75
  184. package/dist/package.json +0 -4
  185. package/src/app/extend/agent.ts +0 -57
  186. package/src/app/extend/application.ts +0 -512
  187. package/src/app/middleware/cluster_app_mock.ts +0 -102
  188. package/src/app.ts +0 -18
  189. package/src/bootstrap.ts +0 -25
  190. package/src/index.ts +0 -112
  191. package/src/lib/agent_handler.ts +0 -28
  192. package/src/lib/app.ts +0 -314
  193. package/src/lib/app_handler.ts +0 -77
  194. package/src/lib/cluster.ts +0 -363
  195. package/src/lib/context.ts +0 -14
  196. package/src/lib/format_options.ts +0 -103
  197. package/src/lib/inject_context.ts +0 -134
  198. package/src/lib/mock_agent.ts +0 -57
  199. package/src/lib/mock_custom_loader.ts +0 -36
  200. package/src/lib/mock_http_server.ts +0 -19
  201. package/src/lib/mock_httpclient.ts +0 -183
  202. package/src/lib/parallel/agent.ts +0 -128
  203. package/src/lib/parallel/app.ts +0 -123
  204. package/src/lib/parallel/util.ts +0 -66
  205. package/src/lib/prerequire.ts +0 -25
  206. package/src/lib/request_call_function.ts +0 -49
  207. package/src/lib/restore.ts +0 -14
  208. package/src/lib/start-cluster.ts +0 -23
  209. package/src/lib/supertest.ts +0 -45
  210. package/src/lib/tmp/.gitkeep +0 -0
  211. package/src/lib/tmp/empty.ts +0 -0
  212. package/src/lib/utils.ts +0 -82
  213. package/src/register.ts +0 -80
  214. package/src/typings/index.d.ts +0 -4
package/src/index.ts DELETED
@@ -1,112 +0,0 @@
1
- import mm from 'mm';
2
- import { mock as _mock } from 'mm';
3
- import { createCluster } from './lib/cluster.js';
4
- import { createApp } from './lib/app.js';
5
- // import { getMockAgent } from './lib/mock_agent.js';
6
- import { restore } from './lib/restore.js';
7
- import { setGetAppCallback } from './lib/app_handler.js';
8
- import ApplicationUnittest from './app/extend/application.js';
9
-
10
- export * from './lib/types.js';
11
-
12
- // egg-bin will set this flag to require files for instrument
13
- // if (process.env.EGG_BIN_PREREQUIRE) {
14
- // require('./lib/prerequire');
15
- // }
16
-
17
- // inherit & extends mm
18
- const mock = {
19
- ...mm,
20
- restore,
21
-
22
- /**
23
- * Create a egg mocked application
24
- * @function mm#app
25
- * @param {Object} [options]
26
- * - {String} baseDir - The directory of the application
27
- * - {Object} plugins - Custom you plugins
28
- * - {String} framework - The directory of the egg framework
29
- * - {Boolean} [true] cache - Cache application based on baseDir
30
- * - {Boolean} [true] coverage - Switch on process coverage, but it'll be slower
31
- * - {Boolean} [true] clean - Remove $baseDir/logs
32
- * @return {App} return {@link Application}
33
- * @example
34
- * ```js
35
- * const app = mm.app();
36
- * ```
37
- */
38
- app: createApp,
39
-
40
- /**
41
- * Create a egg mocked cluster application
42
- * @function mm#cluster
43
- * @see ClusterApplication
44
- */
45
- cluster: createCluster,
46
-
47
- /**
48
- * mock the serverEnv of Egg
49
- * @member {Function} mm#env
50
- * @param {String} env - contain default, test, prod, local, unittest
51
- * @see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L78
52
- */
53
- env(env: string) {
54
- _mock(process.env, 'EGG_MOCK_SERVER_ENV', env);
55
- _mock(process.env, 'EGG_SERVER_ENV', env);
56
- },
57
-
58
- /**
59
- * mock console level
60
- * @param {String} level - logger level
61
- */
62
- consoleLevel(level: string) {
63
- level = (level || '').toUpperCase();
64
- _mock(process.env, 'EGG_LOG', level);
65
- },
66
-
67
- home(homePath?: string) {
68
- if (homePath) {
69
- _mock(process.env, 'EGG_HOME', homePath);
70
- }
71
- },
72
-
73
- setGetAppCallback,
74
- };
75
-
76
- // import mm from '@eggjs/mock';
77
- const proxyMock = new Proxy(_mock, {
78
- apply(target, _, args) {
79
- return target(args[0], args[1], args[2]);
80
- },
81
- get(_target, property, receiver) {
82
- // import mm from '@eggjs/mock';
83
- // mm.isMocked(foo, 'bar')
84
- return Reflect.get(mock, property, receiver);
85
- },
86
- }) as unknown as ((target: any, property: PropertyKey, value?: any) => void) & typeof mock;
87
-
88
- export default proxyMock;
89
-
90
- export {
91
- proxyMock as mock,
92
- // alias to mm
93
- proxyMock as mm,
94
- ApplicationUnittest as MockApplication,
95
- setGetAppCallback,
96
- createApp,
97
- createCluster,
98
- };
99
-
100
- process.setMaxListeners(100);
101
-
102
- process.once('SIGQUIT', () => {
103
- process.exit(0);
104
- });
105
-
106
- process.once('SIGTERM', () => {
107
- process.exit(0);
108
- });
109
-
110
- process.once('SIGINT', () => {
111
- process.exit(0);
112
- });
@@ -1,28 +0,0 @@
1
- import { debuglog } from 'node:util';
2
- import { createAgent, MockAgent } from './parallel/agent.js';
3
- import { getEggOptions } from './utils.js';
4
-
5
- const debug = debuglog('@eggjs/mock/lib/agent_handler');
6
-
7
- let agent: MockAgent;
8
-
9
- export async function setupAgent() {
10
- debug('setupAgent call, env.ENABLE_MOCHA_PARALLEL: %s, process.env.AUTO_AGENT: %s, agent: %s',
11
- process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT, !!agent);
12
- if (agent) {
13
- await agent.ready();
14
- return agent;
15
- }
16
- if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
17
- agent = createAgent(getEggOptions());
18
- await agent.ready();
19
- }
20
- return agent;
21
- }
22
-
23
- export async function closeAgent() {
24
- debug('setupAgent call, agent: %s', !!agent);
25
- if (agent) {
26
- await agent.close();
27
- }
28
- }
package/src/lib/app.ts DELETED
@@ -1,314 +0,0 @@
1
- import { debuglog } from 'node:util';
2
- import { strict as assert } from 'node:assert';
3
- import os from 'node:os';
4
- import path from 'node:path';
5
- import { Base } from 'sdk-base';
6
- import { detectPort } from 'detect-port';
7
- import { importModule } from '@eggjs/utils';
8
- import { sleep, rimraf, getProperty, getSourceDirname } from './utils.js';
9
- import { formatOptions } from './format_options.js';
10
- import { context } from './context.js';
11
- import { setCustomLoader } from './mock_custom_loader.js';
12
- import { createServer } from './mock_http_server.js';
13
- import type { MockOptions, MockApplicationOptions } from './types.js';
14
- import ApplicationUnittest from '../app/extend/application.js';
15
- import AgentUnittest from '../app/extend/agent.js';
16
-
17
- const debug = debuglog('@eggjs/mock/lib/app');
18
-
19
- const apps = new Map<string, ApplicationUnittest>();
20
- const APP_INIT = Symbol('appInit');
21
- const MESSENGER = Symbol('messenger');
22
- const MOCK_APP_METHOD = [
23
- 'ready',
24
- 'closed',
25
- 'isClosed',
26
- 'close',
27
- '_agent',
28
- '_app',
29
- 'on',
30
- 'once',
31
- 'then',
32
- ];
33
-
34
- class MockApplicationWorker extends Base {
35
- _agent: AgentUnittest;
36
- _app: ApplicationUnittest;
37
- declare options: MockApplicationOptions;
38
- baseDir: string;
39
- [APP_INIT] = false;
40
- _initOnListeners: Set<any[]>;
41
- _initOnceListeners: Set<any[]>;
42
-
43
- constructor(options: MockApplicationOptions) {
44
- super({
45
- initMethod: '_init',
46
- ...options,
47
- });
48
- this.baseDir = options.baseDir;
49
- this._initOnListeners = new Set();
50
- this._initOnceListeners = new Set();
51
- }
52
-
53
- async _init() {
54
- if (this.options.beforeInit) {
55
- await this.options.beforeInit(this);
56
- // init once
57
- this.options.beforeInit = undefined;
58
- }
59
- if (this.options.clean !== false) {
60
- const logDir = path.join(this.options.baseDir, 'logs');
61
- try {
62
- if (os.platform() === 'win32') {
63
- await sleep(1000);
64
- }
65
- await rimraf(logDir);
66
- } catch (err: any) {
67
- console.error(`remove log dir ${logDir} failed: ${err.stack}`);
68
- }
69
- const runDir = path.join(this.options.baseDir, 'run');
70
- try {
71
- if (os.platform() === 'win32') {
72
- await sleep(1000);
73
- }
74
- await rimraf(runDir);
75
- } catch (err: any) {
76
- console.error(`remove run dir ${runDir} failed: ${err.stack}`);
77
- }
78
- }
79
-
80
- this.options.clusterPort = await detectPort();
81
- debug('get clusterPort %s', this.options.clusterPort);
82
- const egg = await importModule(this.options.framework);
83
- assert(egg.Agent, `should export Agent class from framework ${this.options.framework}`);
84
-
85
- const Agent = egg.Agent;
86
- const agent = this._agent = new Agent({ ...this.options }) as AgentUnittest;
87
- debug('agent instantiate');
88
- await agent.ready();
89
- debug('agent ready');
90
-
91
- const ApplicationClass = bindMessenger(egg.Application, agent);
92
- const app = this._app = new ApplicationClass({ ...this.options }) as unknown as ApplicationUnittest;
93
-
94
- // https://github.com/eggjs/egg/blob/8bb7c7e7d59d6aeca4b2ed1eb580368dcb731a4d/lib/egg.js#L125
95
- // egg single mode mount this at start(), so egg-mock should impel it.
96
- app.agent = agent;
97
- Reflect.set(agent, 'app', app);
98
-
99
- // egg-mock plugin need to override egg context
100
- Object.assign(app.context, context);
101
-
102
- debug('app instantiate');
103
- this[APP_INIT] = true;
104
- debug('this[APP_INIT] = true');
105
- this.#bindEvent();
106
- debug('http server instantiate');
107
- createServer(app);
108
- await app.ready();
109
- // work for config ready
110
- setCustomLoader(app);
111
-
112
- const msg = {
113
- action: 'egg-ready',
114
- data: this.options,
115
- };
116
- app.messenger.onMessage(msg);
117
- agent.messenger.onMessage(msg);
118
- debug('app ready');
119
- }
120
-
121
- #bindEvent() {
122
- debug('bind cache events to app');
123
- for (const args of this._initOnListeners) {
124
- debug('on(%s), use cache and pass to app', args);
125
- this._app.on(args[0], args[1]);
126
- this.removeListener(args[0], args[1]);
127
- }
128
- for (const args of this._initOnceListeners) {
129
- debug('once(%s), use cache and pass to app', args);
130
- this._app.once(args[0], args[1]);
131
- this.removeListener(args[0], args[1]);
132
- }
133
- }
134
-
135
- on(...args: any[]) {
136
- if (this[APP_INIT]) {
137
- debug('on(%s), pass to app', args);
138
- this._app.on(args[0], args[1]);
139
- } else {
140
- debug('on(%s), cache it because app has not init', args);
141
- this._initOnListeners.add(args);
142
- super.on(args[0], args[1]);
143
- }
144
- return this;
145
- }
146
-
147
- once(...args: any[]) {
148
- if (this[APP_INIT]) {
149
- debug('once(%s), pass to app', args);
150
- this._app.once(args[0], args[1]);
151
- } else {
152
- debug('once(%s), cache it because app has not init', args);
153
- this._initOnceListeners.add(args);
154
- // maybe some edge case bug here
155
- super.on(args[0], args[1]);
156
- }
157
- return this;
158
- }
159
-
160
- /**
161
- * close app
162
- */
163
- async _close() {
164
- const baseDir = this.baseDir;
165
- if (this._app) {
166
- await this._app.close();
167
- } else {
168
- // when app init throws an exception, must wait for app quit gracefully
169
- await sleep(200);
170
- }
171
-
172
- if (this._agent) {
173
- await this._agent.close();
174
- }
175
-
176
- apps.delete(baseDir);
177
- debug('delete app cache %s, remain %s', baseDir, [ ...apps.keys() ]);
178
-
179
- if (os.platform() === 'win32') {
180
- await sleep(1000);
181
- }
182
- }
183
-
184
- /**
185
- * @deprecated please use isClosed instead, keep compatible with old version
186
- */
187
- get closed() {
188
- return this.isClosed;
189
- }
190
- }
191
-
192
- export function createApp(createOptions?: MockOptions): ApplicationUnittest {
193
- const options = formatOptions(createOptions);
194
- if (options.cache && apps.has(options.baseDir)) {
195
- const app = apps.get(options.baseDir);
196
- // return cache when it hasn't been killed
197
- if (app && !app.isClosed) {
198
- debug('use cache app %s', options.baseDir);
199
- return app;
200
- }
201
- // delete the cache when it's closed
202
- apps.delete(options.baseDir);
203
- }
204
-
205
- const appWorker = new MockApplicationWorker(options);
206
- const proxyApp = new Proxy(appWorker, {
207
- get(target: any, prop: string) {
208
- // don't delegate properties on MockApplication
209
- if (MOCK_APP_METHOD.includes(prop)) {
210
- return getProperty(target, prop);
211
- }
212
- if (!target[APP_INIT]) {
213
- throw new Error(`can't get ${prop} before ready`);
214
- }
215
- // it's asynchronous when agent and app are loading,
216
- // so should get the properties after loader ready
217
- debug('proxy handler.get %s', prop);
218
- return target._app[prop];
219
- },
220
- set(target, prop: string, value) {
221
- if (MOCK_APP_METHOD.includes(prop)) return true;
222
- if (!target[APP_INIT]) throw new Error(`can't set ${prop} before ready`);
223
- debug('proxy handler.set %s', prop);
224
- target._app[prop] = value;
225
- return true;
226
- },
227
- defineProperty(target, prop: string, descriptor) {
228
- // can't define properties on MockApplication
229
- if (MOCK_APP_METHOD.includes(prop)) return true;
230
- if (!target[APP_INIT]) throw new Error(`can't defineProperty ${prop} before ready`);
231
- debug('proxy handler.defineProperty %s', prop);
232
- Object.defineProperty(target._app, prop, descriptor);
233
- return true;
234
- },
235
- deleteProperty(target, prop: string) {
236
- // can't delete properties on MockApplication
237
- if (MOCK_APP_METHOD.includes(prop)) return true;
238
- if (!target[APP_INIT]) throw new Error(`can't delete ${prop} before ready`);
239
- debug('proxy handler.deleteProperty %s', prop);
240
- delete target._app[prop];
241
- return true;
242
- },
243
- getOwnPropertyDescriptor(target, prop: string) {
244
- if (MOCK_APP_METHOD.includes(prop)) {
245
- return Object.getOwnPropertyDescriptor(target, prop);
246
- }
247
- if (!target[APP_INIT]) {
248
- throw new Error(`can't getOwnPropertyDescriptor ${prop} before ready`);
249
- }
250
- debug('proxy handler.getOwnPropertyDescriptor %s', prop);
251
- return Object.getOwnPropertyDescriptor(target._app, prop);
252
- },
253
- getPrototypeOf(target) {
254
- if (!target[APP_INIT]) {
255
- throw new Error('can\'t getPrototypeOf before ready');
256
- }
257
- debug('proxy handler.getPrototypeOf %s');
258
- return Object.getPrototypeOf(target._app);
259
- },
260
- });
261
-
262
- apps.set(options.baseDir, proxyApp);
263
- return proxyApp;
264
- }
265
-
266
- function bindMessenger(ApplicationClass: any, agent: AgentUnittest) {
267
- const agentMessenger = agent.messenger;
268
- return class MessengerApplication extends ApplicationClass {
269
- [MESSENGER]: any;
270
-
271
- constructor(options: any) {
272
- super(options);
273
-
274
- // enable app to send to a random agent
275
- this.messenger.sendRandom = (action: string, data: unknown) => {
276
- this.messenger.sendToAgent(action, data);
277
- };
278
- // enable agent to send to a random app
279
- agentMessenger.on('egg-ready', () => {
280
- agentMessenger.sendRandom = (action: string, data: unknown) => {
281
- agentMessenger.sendToApp(action, data);
282
- return agentMessenger;
283
- };
284
- });
285
-
286
- agentMessenger.send = new Proxy(agentMessenger.send, {
287
- apply: this._sendMessage.bind(this),
288
- });
289
- }
290
- _sendMessage(_target: any, _thisArg: unknown, [ action, data, to ]: [ string, unknown | undefined, string]) {
291
- const appMessenger = this.messenger;
292
- setImmediate(() => {
293
- if (to === 'app') {
294
- appMessenger.onMessage({ action, data });
295
- } else {
296
- agentMessenger.onMessage({ action, data });
297
- }
298
- });
299
- }
300
- get messenger() {
301
- return this[MESSENGER];
302
- }
303
- set messenger(m) {
304
- m.send = new Proxy(m.send, {
305
- apply: this._sendMessage.bind(this),
306
- });
307
- this[MESSENGER] = m;
308
- }
309
-
310
- get [Symbol.for('egg#eggPath')]() {
311
- return path.join(getSourceDirname(), 'lib/tmp');
312
- }
313
- };
314
- }
@@ -1,77 +0,0 @@
1
- import { debuglog } from 'node:util';
2
- import { createApp as createParallelApp } from './parallel/app.js';
3
- import { setupAgent } from './agent_handler.js';
4
- import { createApp } from './app.js';
5
- import { restore } from './restore.js';
6
- import { getEggOptions } from './utils.js';
7
- import ApplicationUnittest from '../app/extend/application.js';
8
-
9
- const debug = debuglog('@eggjs/mock/lib/app_handler');
10
-
11
- declare namespace globalThis {
12
- let __eggMockAppInstance: ApplicationUnittest | null;
13
- }
14
-
15
- globalThis.__eggMockAppInstance = null;
16
-
17
- export function setupApp() {
18
- let app = globalThis.__eggMockAppInstance!;
19
- if (app) {
20
- debug('return exists app');
21
- return app;
22
- }
23
-
24
- const options = getEggOptions();
25
- debug('env.ENABLE_MOCHA_PARALLEL: %s, process.env.AUTO_AGENT: %s',
26
- process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT);
27
- if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
28
- // setup agent first
29
- app = createParallelApp({
30
- ...options,
31
- beforeInit: async parallelApp => {
32
- const agent = await setupAgent();
33
- parallelApp.options.clusterPort = agent.options.clusterPort;
34
- debug('mockParallelApp beforeInit get clusterPort: %s', parallelApp.options.clusterPort);
35
- },
36
- });
37
- debug('mockParallelApp app: %s', !!app);
38
- } else {
39
- app = createApp(options) as unknown as ApplicationUnittest;
40
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
41
- // @ts-ignore
42
- if (typeof beforeAll === 'function') {
43
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
44
- // @ts-ignore
45
- // jest
46
- beforeAll(() => app.ready());
47
- }
48
- if (typeof afterEach === 'function') {
49
- // mocha and jest
50
- afterEach(() => app.backgroundTasksFinished());
51
- afterEach(restore);
52
- }
53
- }
54
- globalThis.__eggMockAppInstance = app;
55
- return app;
56
- }
57
-
58
- let getAppCallback: (suite: unknown, test?: unknown) => any;
59
-
60
- export function setGetAppCallback(cb: (suite: unknown, test?: unknown) => any) {
61
- getAppCallback = cb;
62
- }
63
-
64
- export async function getApp(suite?: unknown, test?: unknown) {
65
- if (getAppCallback) {
66
- return getAppCallback(suite, test);
67
- }
68
- const app = globalThis.__eggMockAppInstance!;
69
- if (app) {
70
- await app.ready();
71
- }
72
- return app;
73
- }
74
-
75
- export function getBootstrapApp() {
76
- return globalThis.__eggMockAppInstance!;
77
- }