@eggjs/mock 6.0.7 → 6.1.0-beta.2
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/README.md +35 -56
- package/README.zh_CN.md +30 -50
- package/dist/_virtual/rolldown_runtime.js +7 -0
- package/dist/app/extend/agent.d.ts +39 -0
- package/dist/app/extend/agent.js +48 -0
- package/dist/app/extend/application.d.ts +176 -0
- package/dist/app/extend/application.js +385 -0
- package/dist/app/middleware/cluster_app_mock.d.ts +6 -0
- package/dist/app/middleware/cluster_app_mock.js +97 -0
- package/dist/app.d.ts +10 -0
- package/dist/app.js +17 -0
- package/dist/bootstrap.d.ts +9 -0
- package/dist/bootstrap.js +14 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.js +48 -0
- package/dist/lib/agent_handler.d.ts +7 -0
- package/dist/lib/agent_handler.js +26 -0
- package/dist/lib/app.d.ts +7 -0
- package/dist/lib/app.js +251 -0
- package/dist/lib/app_handler.d.ts +9 -0
- package/dist/lib/app_handler.js +56 -0
- package/dist/lib/cluster.d.ts +126 -0
- package/dist/lib/cluster.js +283 -0
- package/dist/lib/context.d.ts +4 -0
- package/dist/lib/context.js +13 -0
- package/dist/lib/format_options.d.ts +10 -0
- package/dist/lib/format_options.js +65 -0
- package/dist/lib/inject_context.d.ts +9 -0
- package/dist/lib/inject_context.js +106 -0
- package/dist/lib/mock_agent.d.ts +9 -0
- package/dist/lib/mock_agent.js +45 -0
- package/dist/lib/mock_custom_loader.d.ts +4 -0
- package/dist/lib/mock_custom_loader.js +35 -0
- package/dist/lib/mock_http_server.d.ts +6 -0
- package/dist/lib/mock_http_server.js +17 -0
- package/dist/lib/mock_httpclient.d.ts +40 -0
- package/dist/lib/mock_httpclient.js +110 -0
- package/dist/lib/parallel/agent.d.ts +24 -0
- package/dist/lib/parallel/agent.js +111 -0
- package/dist/lib/parallel/app.d.ts +24 -0
- package/dist/lib/parallel/app.js +98 -0
- package/dist/lib/parallel/util.d.ts +6 -0
- package/dist/lib/parallel/util.js +59 -0
- package/dist/lib/prerequire.d.ts +1 -0
- package/dist/lib/prerequire.js +1 -0
- package/dist/lib/request_call_function.d.ts +1 -0
- package/dist/lib/request_call_function.js +37 -0
- package/dist/lib/restore.d.ts +4 -0
- package/dist/lib/restore.js +16 -0
- package/dist/{commonjs/lib → lib}/start-cluster.d.ts +1 -1
- package/dist/lib/start-cluster.js +19 -0
- package/dist/lib/supertest.d.ts +16 -0
- package/dist/lib/supertest.js +39 -0
- package/dist/lib/tmp/empty.d.ts +1 -0
- package/dist/lib/tmp/empty.js +1 -0
- package/{src/lib/types.ts → dist/lib/types.d.ts} +12 -29
- package/dist/lib/types.js +1 -0
- package/dist/lib/utils.d.ts +12 -0
- package/dist/lib/utils.js +40 -0
- package/dist/register.d.ts +12 -0
- package/dist/register.js +65 -0
- package/dist/typings/index.d.ts +1 -0
- package/package.json +68 -100
- package/dist/commonjs/app/extend/agent.d.ts +0 -34
- package/dist/commonjs/app/extend/agent.js +0 -49
- package/dist/commonjs/app/extend/application.d.ts +0 -172
- package/dist/commonjs/app/extend/application.js +0 -450
- package/dist/commonjs/app/middleware/cluster_app_mock.d.ts +0 -3
- package/dist/commonjs/app/middleware/cluster_app_mock.js +0 -101
- package/dist/commonjs/app.d.ts +0 -6
- package/dist/commonjs/app.js +0 -20
- package/dist/commonjs/bootstrap.d.ts +0 -5
- package/dist/commonjs/bootstrap.js +0 -59
- package/dist/commonjs/index.d.ts +0 -77
- package/dist/commonjs/index.js +0 -114
- package/dist/commonjs/lib/agent_handler.d.ts +0 -3
- package/dist/commonjs/lib/agent_handler.js +0 -28
- package/dist/commonjs/lib/app.d.ts +0 -3
- package/dist/commonjs/lib/app.js +0 -301
- package/dist/commonjs/lib/app_handler.d.ts +0 -5
- package/dist/commonjs/lib/app_handler.js +0 -71
- package/dist/commonjs/lib/cluster.d.ts +0 -114
- package/dist/commonjs/lib/cluster.js +0 -337
- package/dist/commonjs/lib/context.d.ts +0 -1
- package/dist/commonjs/lib/context.js +0 -16
- package/dist/commonjs/lib/format_options.d.ts +0 -5
- package/dist/commonjs/lib/format_options.js +0 -100
- package/dist/commonjs/lib/inject_context.d.ts +0 -6
- package/dist/commonjs/lib/inject_context.js +0 -132
- package/dist/commonjs/lib/mock_agent.d.ts +0 -5
- package/dist/commonjs/lib/mock_agent.js +0 -49
- package/dist/commonjs/lib/mock_custom_loader.d.ts +0 -1
- package/dist/commonjs/lib/mock_custom_loader.js +0 -37
- package/dist/commonjs/lib/mock_http_server.d.ts +0 -2
- package/dist/commonjs/lib/mock_http_server.js +0 -24
- package/dist/commonjs/lib/mock_httpclient.d.ts +0 -36
- package/dist/commonjs/lib/mock_httpclient.js +0 -147
- package/dist/commonjs/lib/parallel/agent.d.ts +0 -20
- package/dist/commonjs/lib/parallel/agent.js +0 -125
- package/dist/commonjs/lib/parallel/app.d.ts +0 -20
- package/dist/commonjs/lib/parallel/app.js +0 -115
- package/dist/commonjs/lib/parallel/util.d.ts +0 -3
- package/dist/commonjs/lib/parallel/util.js +0 -77
- package/dist/commonjs/lib/prerequire.d.ts +0 -1
- package/dist/commonjs/lib/prerequire.js +0 -26
- package/dist/commonjs/lib/request_call_function.d.ts +0 -1
- package/dist/commonjs/lib/request_call_function.js +0 -52
- package/dist/commonjs/lib/restore.d.ts +0 -1
- package/dist/commonjs/lib/restore.js +0 -16
- package/dist/commonjs/lib/start-cluster.js +0 -23
- package/dist/commonjs/lib/supertest.d.ts +0 -11
- package/dist/commonjs/lib/supertest.js +0 -48
- package/dist/commonjs/lib/tmp/empty.d.ts +0 -1
- package/dist/commonjs/lib/tmp/empty.js +0 -3
- package/dist/commonjs/lib/types.d.ts +0 -68
- package/dist/commonjs/lib/types.js +0 -3
- package/dist/commonjs/lib/utils.d.ts +0 -9
- package/dist/commonjs/lib/utils.js +0 -80
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/register.d.ts +0 -8
- package/dist/commonjs/register.js +0 -80
- package/dist/esm/app/extend/agent.d.ts +0 -34
- package/dist/esm/app/extend/agent.js +0 -46
- package/dist/esm/app/extend/application.d.ts +0 -172
- package/dist/esm/app/extend/application.js +0 -444
- package/dist/esm/app/middleware/cluster_app_mock.d.ts +0 -3
- package/dist/esm/app/middleware/cluster_app_mock.js +0 -99
- package/dist/esm/app.d.ts +0 -6
- package/dist/esm/app.js +0 -17
- package/dist/esm/bootstrap.d.ts +0 -5
- package/dist/esm/bootstrap.js +0 -15
- package/dist/esm/index.d.ts +0 -77
- package/dist/esm/index.js +0 -91
- package/dist/esm/lib/agent_handler.d.ts +0 -3
- package/dist/esm/lib/agent_handler.js +0 -24
- package/dist/esm/lib/app.d.ts +0 -3
- package/dist/esm/lib/app.js +0 -295
- package/dist/esm/lib/app_handler.d.ts +0 -5
- package/dist/esm/lib/app_handler.js +0 -65
- package/dist/esm/lib/cluster.d.ts +0 -114
- package/dist/esm/lib/cluster.js +0 -328
- package/dist/esm/lib/context.d.ts +0 -1
- package/dist/esm/lib/context.js +0 -13
- package/dist/esm/lib/format_options.d.ts +0 -5
- package/dist/esm/lib/format_options.js +0 -94
- package/dist/esm/lib/inject_context.d.ts +0 -6
- package/dist/esm/lib/inject_context.js +0 -126
- package/dist/esm/lib/mock_agent.d.ts +0 -5
- package/dist/esm/lib/mock_agent.js +0 -45
- package/dist/esm/lib/mock_custom_loader.d.ts +0 -1
- package/dist/esm/lib/mock_custom_loader.js +0 -34
- package/dist/esm/lib/mock_http_server.d.ts +0 -2
- package/dist/esm/lib/mock_http_server.js +0 -18
- package/dist/esm/lib/mock_httpclient.d.ts +0 -36
- package/dist/esm/lib/mock_httpclient.js +0 -144
- package/dist/esm/lib/parallel/agent.d.ts +0 -20
- package/dist/esm/lib/parallel/agent.js +0 -117
- package/dist/esm/lib/parallel/app.d.ts +0 -20
- package/dist/esm/lib/parallel/app.js +0 -110
- package/dist/esm/lib/parallel/util.d.ts +0 -3
- package/dist/esm/lib/parallel/util.js +0 -73
- package/dist/esm/lib/prerequire.d.ts +0 -1
- package/dist/esm/lib/prerequire.js +0 -25
- package/dist/esm/lib/request_call_function.d.ts +0 -1
- package/dist/esm/lib/request_call_function.js +0 -47
- package/dist/esm/lib/restore.d.ts +0 -1
- package/dist/esm/lib/restore.js +0 -13
- package/dist/esm/lib/start-cluster.d.ts +0 -2
- package/dist/esm/lib/start-cluster.js +0 -18
- package/dist/esm/lib/supertest.d.ts +0 -11
- package/dist/esm/lib/supertest.js +0 -40
- package/dist/esm/lib/tmp/empty.d.ts +0 -1
- package/dist/esm/lib/tmp/empty.js +0 -2
- package/dist/esm/lib/types.d.ts +0 -68
- package/dist/esm/lib/types.js +0 -2
- package/dist/esm/lib/utils.d.ts +0 -9
- package/dist/esm/lib/utils.js +0 -69
- package/dist/esm/package.json +0 -3
- package/dist/esm/register.d.ts +0 -8
- package/dist/esm/register.js +0 -75
- package/dist/package.json +0 -4
- package/src/app/extend/agent.ts +0 -57
- package/src/app/extend/application.ts +0 -512
- package/src/app/middleware/cluster_app_mock.ts +0 -102
- package/src/app.ts +0 -18
- package/src/bootstrap.ts +0 -25
- package/src/index.ts +0 -112
- package/src/lib/agent_handler.ts +0 -28
- package/src/lib/app.ts +0 -314
- package/src/lib/app_handler.ts +0 -77
- package/src/lib/cluster.ts +0 -363
- package/src/lib/context.ts +0 -14
- package/src/lib/format_options.ts +0 -103
- package/src/lib/inject_context.ts +0 -134
- package/src/lib/mock_agent.ts +0 -57
- package/src/lib/mock_custom_loader.ts +0 -36
- package/src/lib/mock_http_server.ts +0 -19
- package/src/lib/mock_httpclient.ts +0 -183
- package/src/lib/parallel/agent.ts +0 -128
- package/src/lib/parallel/app.ts +0 -123
- package/src/lib/parallel/util.ts +0 -66
- package/src/lib/prerequire.ts +0 -25
- package/src/lib/request_call_function.ts +0 -49
- package/src/lib/restore.ts +0 -14
- package/src/lib/start-cluster.ts +0 -23
- package/src/lib/supertest.ts +0 -45
- package/src/lib/tmp/.gitkeep +0 -0
- package/src/lib/tmp/empty.ts +0 -0
- package/src/lib/utils.ts +0 -82
- package/src/register.ts +0 -80
- package/src/typings/index.d.ts +0 -4
package/src/lib/cluster.ts
DELETED
|
@@ -1,363 +0,0 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import os from 'node:os';
|
|
4
|
-
import childProcess from 'node:child_process';
|
|
5
|
-
import { once } from 'node:events';
|
|
6
|
-
import { Coffee } from 'coffee';
|
|
7
|
-
import { Ready } from 'get-ready';
|
|
8
|
-
import { request as supertestRequest } from './supertest.js';
|
|
9
|
-
import { sleep, rimrafSync, getSourceDirname } from './utils.js';
|
|
10
|
-
import { formatOptions } from './format_options.js';
|
|
11
|
-
import type { MockClusterOptions, MockClusterApplicationOptions } from './types.js';
|
|
12
|
-
|
|
13
|
-
const debug = debuglog('@eggjs/mock/lib/cluster');
|
|
14
|
-
|
|
15
|
-
const clusters = new Map();
|
|
16
|
-
const serverBin = path.join(getSourceDirname(), 'lib/start-cluster.js');
|
|
17
|
-
const requestCallFunctionFile = path.join(getSourceDirname(), 'lib/request_call_function.js');
|
|
18
|
-
let masterPort = 17000;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* A cluster version of egg.Application, you can test with supertest
|
|
22
|
-
* @example
|
|
23
|
-
* ```js
|
|
24
|
-
* const mm = require('mm');
|
|
25
|
-
* const request = require('supertest');
|
|
26
|
-
*
|
|
27
|
-
* describe('ClusterApplication', () => {
|
|
28
|
-
* let app;
|
|
29
|
-
* before(function (done) {
|
|
30
|
-
* app = mm.cluster({ baseDir });
|
|
31
|
-
* app.ready(done);
|
|
32
|
-
* });
|
|
33
|
-
*
|
|
34
|
-
* after(function () {
|
|
35
|
-
* app.close();
|
|
36
|
-
* });
|
|
37
|
-
*
|
|
38
|
-
* it('should 200', function (done) {
|
|
39
|
-
* request(app.callback())
|
|
40
|
-
* .get('/')
|
|
41
|
-
* .expect(200, done);
|
|
42
|
-
* });
|
|
43
|
-
* });
|
|
44
|
-
*/
|
|
45
|
-
export class ClusterApplication extends Coffee {
|
|
46
|
-
[key: string | symbol]: any;
|
|
47
|
-
options: MockClusterApplicationOptions;
|
|
48
|
-
port: number;
|
|
49
|
-
baseDir: string;
|
|
50
|
-
closed: boolean;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @class
|
|
54
|
-
* @param {Object} options
|
|
55
|
-
* - {String} baseDir - The directory of the application
|
|
56
|
-
* - {Object} plugins - Custom you plugins
|
|
57
|
-
* - {String} framework - The directory of the egg framework
|
|
58
|
-
* - {Boolean} [cache=true] - Cache application based on baseDir
|
|
59
|
-
* - {Boolean} [coverage=true] - Switch on process coverage, but it'll be slower
|
|
60
|
-
* - {Boolean} [clean=true] - Remove $baseDir/logs
|
|
61
|
-
* - {Object} [opt] - opt pass to coffee, such as { execArgv: ['--debug'] }
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
constructor(options: MockClusterApplicationOptions) {
|
|
65
|
-
const opt = options.opt;
|
|
66
|
-
delete options.opt;
|
|
67
|
-
|
|
68
|
-
// incremental port
|
|
69
|
-
options.port = options.port ?? ++masterPort;
|
|
70
|
-
// Set 1 worker when test
|
|
71
|
-
if (!options.workers) {
|
|
72
|
-
options.workers = 1;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const args = [ JSON.stringify(options) ];
|
|
76
|
-
debug('fork %s, args: %s, opt: %j', serverBin, args.join(' '), opt);
|
|
77
|
-
super({
|
|
78
|
-
method: 'fork',
|
|
79
|
-
cmd: serverBin,
|
|
80
|
-
args,
|
|
81
|
-
opt,
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
Ready.mixin(this);
|
|
85
|
-
|
|
86
|
-
this.port = options.port;
|
|
87
|
-
this.baseDir = options.baseDir;
|
|
88
|
-
|
|
89
|
-
// print stdout and stderr when DEBUG, otherwise stderr.
|
|
90
|
-
this.debug(process.env.DEBUG ? 0 : 2);
|
|
91
|
-
|
|
92
|
-
// disable coverage
|
|
93
|
-
if (options.coverage === false) {
|
|
94
|
-
this.coverage(false);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
process.nextTick(() => {
|
|
98
|
-
this.proc.on('message', (msg: any) => {
|
|
99
|
-
// 'egg-ready' and { action: 'egg-ready' }
|
|
100
|
-
const action = msg && msg.action ? msg.action : msg;
|
|
101
|
-
switch (action) {
|
|
102
|
-
case 'egg-ready':
|
|
103
|
-
this.emit('close', 0);
|
|
104
|
-
break;
|
|
105
|
-
case 'app-worker-died':
|
|
106
|
-
case 'agent-worker-died':
|
|
107
|
-
this.emit('close', 1);
|
|
108
|
-
break;
|
|
109
|
-
default:
|
|
110
|
-
// ignore it
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
this.end(() => this.ready(true));
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* the process that forked
|
|
121
|
-
* @member {ChildProcess}
|
|
122
|
-
*/
|
|
123
|
-
get process() {
|
|
124
|
-
return this.proc;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Compatible API for supertest
|
|
129
|
-
*/
|
|
130
|
-
callback() {
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Compatible API for supertest
|
|
136
|
-
* @member {String} url
|
|
137
|
-
* @private
|
|
138
|
-
*/
|
|
139
|
-
get url() {
|
|
140
|
-
return 'http://127.0.0.1:' + this.port;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Compatible API for supertest
|
|
145
|
-
*/
|
|
146
|
-
address() {
|
|
147
|
-
return {
|
|
148
|
-
port: this.port,
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Compatible API for supertest
|
|
154
|
-
*/
|
|
155
|
-
listen() {
|
|
156
|
-
return this;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* kill the process
|
|
161
|
-
*/
|
|
162
|
-
async close() {
|
|
163
|
-
this.closed = true;
|
|
164
|
-
|
|
165
|
-
const proc = this.proc;
|
|
166
|
-
const baseDir = this.baseDir;
|
|
167
|
-
if (proc.connected) {
|
|
168
|
-
proc.kill('SIGTERM');
|
|
169
|
-
await once(proc, 'exit');
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
clusters.delete(baseDir);
|
|
173
|
-
debug('delete cluster cache %s, remain %s', baseDir, [ ...clusters.keys() ]);
|
|
174
|
-
|
|
175
|
-
if (os.platform() === 'win32') {
|
|
176
|
-
await sleep(1000);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
get isClosed() {
|
|
181
|
-
return this.closed;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// mock app.router.pathFor(name) api
|
|
185
|
-
get router() {
|
|
186
|
-
const self = this;
|
|
187
|
-
return {
|
|
188
|
-
pathFor(url: string) {
|
|
189
|
-
return self._callFunctionOnAppWorker('pathFor', [ url ], 'router', true);
|
|
190
|
-
},
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* get app[property] value in app worker
|
|
196
|
-
*/
|
|
197
|
-
getAppInstanceProperty(property: string) {
|
|
198
|
-
return this._callFunctionOnAppWorker('__getter__', [], property, true);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* collection logger message, then can be use on `expectLog()`
|
|
203
|
-
* it's different from `app.expectLog()`, only support string params.
|
|
204
|
-
*
|
|
205
|
-
* @param {String} [logger] - logger instance name, default is `logger`
|
|
206
|
-
* @function ClusterApplication#expectLog
|
|
207
|
-
*/
|
|
208
|
-
mockLog(logger?: string) {
|
|
209
|
-
logger = logger ?? 'logger';
|
|
210
|
-
this._callFunctionOnAppWorker('mockLog', [ logger ], null, true);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* expect str in the logger
|
|
215
|
-
* it's different from `app.expectLog()`, only support string params.
|
|
216
|
-
*
|
|
217
|
-
* @param {String} str - test str
|
|
218
|
-
* @param {String} [logger] - logger instance name, default is `logger`
|
|
219
|
-
* @function ClusterApplication#expectLog
|
|
220
|
-
*/
|
|
221
|
-
expectLog(str: string, logger?: string) {
|
|
222
|
-
logger = logger ?? 'logger';
|
|
223
|
-
this._callFunctionOnAppWorker('expectLog', [ str, logger ], null, true);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* not expect str in the logger
|
|
228
|
-
* it's different from `app.notExpectLog()`, only support string params.
|
|
229
|
-
*
|
|
230
|
-
* @param {String} str - test str
|
|
231
|
-
* @param {String} [logger] - logger instance name, default is `logger`
|
|
232
|
-
* @function ClusterApplication#notExpectLog
|
|
233
|
-
*/
|
|
234
|
-
notExpectLog(str: string, logger?: string) {
|
|
235
|
-
logger = logger ?? 'logger';
|
|
236
|
-
this._callFunctionOnAppWorker('notExpectLog', [ str, logger ], null, true);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
httpRequest() {
|
|
240
|
-
return supertestRequest(this);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
_callFunctionOnAppWorker(method: string, args: any[] = [], property: any = undefined, needResult = false) {
|
|
244
|
-
for (let i = 0; i < args.length; i++) {
|
|
245
|
-
const arg = args[i];
|
|
246
|
-
if (typeof arg === 'function') {
|
|
247
|
-
args[i] = {
|
|
248
|
-
__egg_mock_type: 'function',
|
|
249
|
-
value: arg.toString(),
|
|
250
|
-
};
|
|
251
|
-
} else if (arg instanceof Error) {
|
|
252
|
-
const errObject: any = {
|
|
253
|
-
__egg_mock_type: 'error',
|
|
254
|
-
name: arg.name,
|
|
255
|
-
message: arg.message,
|
|
256
|
-
stack: arg.stack,
|
|
257
|
-
};
|
|
258
|
-
for (const key in arg) {
|
|
259
|
-
if (key !== 'name' && key !== 'message' && key !== 'stack') {
|
|
260
|
-
errObject[key] = (arg as any)[key];
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
args[i] = errObject;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
const data = {
|
|
267
|
-
port: this.port,
|
|
268
|
-
method,
|
|
269
|
-
args,
|
|
270
|
-
property,
|
|
271
|
-
needResult,
|
|
272
|
-
};
|
|
273
|
-
const child = childProcess.spawnSync(process.execPath, [
|
|
274
|
-
requestCallFunctionFile,
|
|
275
|
-
JSON.stringify(data),
|
|
276
|
-
], {
|
|
277
|
-
stdio: 'pipe',
|
|
278
|
-
});
|
|
279
|
-
// if (child.stderr && child.stderr.length > 0) {
|
|
280
|
-
// console.error(child.stderr.toString());
|
|
281
|
-
// }
|
|
282
|
-
let result;
|
|
283
|
-
if (child.stdout && child.stdout.length > 0) {
|
|
284
|
-
if (needResult) {
|
|
285
|
-
result = JSON.parse(child.stdout.toString());
|
|
286
|
-
} else {
|
|
287
|
-
console.error(child.stdout.toString());
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if (child.status !== 0) {
|
|
292
|
-
throw new Error(child.stderr.toString());
|
|
293
|
-
}
|
|
294
|
-
if (child.error) {
|
|
295
|
-
throw child.error;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
return result;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
export function createCluster(initOptions?: MockClusterOptions) {
|
|
303
|
-
const options = formatOptions(initOptions) as MockClusterApplicationOptions;
|
|
304
|
-
if (options.cache && clusters.has(options.baseDir)) {
|
|
305
|
-
const clusterApp = clusters.get(options.baseDir);
|
|
306
|
-
// return cache when it hasn't been killed
|
|
307
|
-
if (!clusterApp.isClosed) {
|
|
308
|
-
return clusterApp;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// delete the cache when it's closed
|
|
312
|
-
clusters.delete(options.baseDir);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
if (options.clean !== false) {
|
|
316
|
-
const logDir = path.join(options.baseDir, 'logs');
|
|
317
|
-
try {
|
|
318
|
-
rimrafSync(logDir);
|
|
319
|
-
} catch (err: any) {
|
|
320
|
-
console.error(`remove log dir ${logDir} failed: ${err.stack}`);
|
|
321
|
-
}
|
|
322
|
-
const runDir = path.join(options.baseDir, 'run');
|
|
323
|
-
try {
|
|
324
|
-
rimrafSync(runDir);
|
|
325
|
-
} catch (err: any) {
|
|
326
|
-
console.error(`remove run dir ${runDir} failed: ${err.stack}`);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
let clusterApp = new ClusterApplication(options);
|
|
331
|
-
clusterApp = new Proxy(clusterApp, {
|
|
332
|
-
get(target, prop) {
|
|
333
|
-
debug('proxy handler.get %s', prop);
|
|
334
|
-
// proxy mockXXX function to app worker
|
|
335
|
-
const method = prop;
|
|
336
|
-
if (typeof method === 'string' && /^mock\w+$/.test(method) && target[method] === undefined) {
|
|
337
|
-
return function mockProxy(...args: any[]) {
|
|
338
|
-
return target._callFunctionOnAppWorker(method, args, null, true);
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
return target[prop];
|
|
342
|
-
},
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
clusters.set(options.baseDir, clusterApp);
|
|
346
|
-
return clusterApp;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
// export to let mm.restore() worked
|
|
350
|
-
export async function restore() {
|
|
351
|
-
for (const clusterApp of clusters.values()) {
|
|
352
|
-
// will proxy to app.mockRestore()
|
|
353
|
-
await clusterApp.mockRestore();
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
// ensure to close App process on test exit.
|
|
358
|
-
process.on('exit', () => {
|
|
359
|
-
for (const clusterApp of clusters.values()) {
|
|
360
|
-
debug('on exit close clusterApp, port: %s', clusterApp.port);
|
|
361
|
-
clusterApp.close();
|
|
362
|
-
}
|
|
363
|
-
});
|
package/src/lib/context.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { utils } from '@eggjs/core';
|
|
2
|
-
|
|
3
|
-
export const context = {
|
|
4
|
-
runInBackground(scope: any) {
|
|
5
|
-
/* istanbul ignore next */
|
|
6
|
-
const taskName = scope._name || scope.name || utils.getCalleeFromStack(true);
|
|
7
|
-
if (taskName) {
|
|
8
|
-
scope._name = taskName;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const promise = this._runInBackground(scope);
|
|
12
|
-
this.app._backgroundTasks.push(promise);
|
|
13
|
-
},
|
|
14
|
-
} as any;
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { mm, isMocked } from 'mm';
|
|
4
|
-
import { getFrameworkPath } from '@eggjs/utils';
|
|
5
|
-
import { readJSONSync } from 'utility';
|
|
6
|
-
import { MockOptions, MockApplicationOptions } from './types.js';
|
|
7
|
-
import { getSourceDirname } from './utils.js';
|
|
8
|
-
|
|
9
|
-
const debug = debuglog('@eggjs/mock/lib/format_options');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* format the options
|
|
13
|
-
*/
|
|
14
|
-
export function formatOptions(initOptions?: MockOptions) {
|
|
15
|
-
const options = {
|
|
16
|
-
baseDir: process.cwd(),
|
|
17
|
-
cache: true,
|
|
18
|
-
coverage: true,
|
|
19
|
-
clean: true,
|
|
20
|
-
...initOptions,
|
|
21
|
-
} as MockApplicationOptions;
|
|
22
|
-
|
|
23
|
-
// relative path to test/fixtures
|
|
24
|
-
// ```js
|
|
25
|
-
// formatOptions({ baseDir: 'app' }); // baseDir => $PWD/test/fixtures/app
|
|
26
|
-
// ```
|
|
27
|
-
if (!path.isAbsolute(options.baseDir)) {
|
|
28
|
-
options.baseDir = path.join(process.cwd(), 'test/fixtures', options.baseDir);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
let framework = initOptions?.framework ?? initOptions?.customEgg;
|
|
32
|
-
// test for framework
|
|
33
|
-
if (framework === true) {
|
|
34
|
-
framework = process.cwd();
|
|
35
|
-
// disable plugin test when framework test
|
|
36
|
-
options.plugin = false;
|
|
37
|
-
} else {
|
|
38
|
-
if (!framework) {
|
|
39
|
-
framework = '';
|
|
40
|
-
}
|
|
41
|
-
// it will throw when framework is not found
|
|
42
|
-
framework = getFrameworkPath({ framework, baseDir: options.baseDir });
|
|
43
|
-
}
|
|
44
|
-
options.framework = options.customEgg = framework;
|
|
45
|
-
|
|
46
|
-
const plugins = options.plugins = options.plugins || {};
|
|
47
|
-
|
|
48
|
-
// add self as a plugin
|
|
49
|
-
let pluginPath = path.join(getSourceDirname(), '..');
|
|
50
|
-
// for dist directory
|
|
51
|
-
// convert `/eggjs/mock/dist` to `/eggjs/mock`
|
|
52
|
-
if (pluginPath.endsWith('/dist') || pluginPath.endsWith('\\dist')) {
|
|
53
|
-
pluginPath = path.join(pluginPath, '..');
|
|
54
|
-
}
|
|
55
|
-
plugins['egg-mock'] = {
|
|
56
|
-
enable: true,
|
|
57
|
-
path: pluginPath,
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
// test for plugin
|
|
61
|
-
if (options.plugin !== false) {
|
|
62
|
-
// add self to plugin list
|
|
63
|
-
const pluginPath = process.cwd();
|
|
64
|
-
const pkgPath = path.join(pluginPath, 'package.json');
|
|
65
|
-
const pluginName = getPluginName(pkgPath);
|
|
66
|
-
if (options.plugin && !pluginName) {
|
|
67
|
-
throw new Error(`should set "eggPlugin" property in ${pkgPath}`);
|
|
68
|
-
}
|
|
69
|
-
if (pluginName) {
|
|
70
|
-
plugins[pluginName] = {
|
|
71
|
-
enable: true,
|
|
72
|
-
path: pluginPath,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// mock HOME as baseDir, but ignore if it has been mocked
|
|
78
|
-
const env = process.env.EGG_SERVER_ENV;
|
|
79
|
-
if (!isMocked(process.env, 'HOME') &&
|
|
80
|
-
(env === 'default' || env === 'test' || env === 'prod')) {
|
|
81
|
-
mm(process.env, 'HOME', options.baseDir);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// disable cache after call mm.env(),
|
|
85
|
-
// otherwise it will use cache and won't load again.
|
|
86
|
-
if (process.env.EGG_MOCK_SERVER_ENV) {
|
|
87
|
-
options.cache = false;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
debug('format options: %j', options);
|
|
91
|
-
return options;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function getPluginName(pkgPath: string): string | undefined {
|
|
95
|
-
try {
|
|
96
|
-
const pkg = readJSONSync(pkgPath);
|
|
97
|
-
if (pkg.eggPlugin?.name) {
|
|
98
|
-
return pkg.eggPlugin.name;
|
|
99
|
-
}
|
|
100
|
-
} catch (_) {
|
|
101
|
-
// ignore
|
|
102
|
-
}
|
|
103
|
-
}
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import assert from 'node:assert';
|
|
3
|
-
import { getApp } from './app_handler.js';
|
|
4
|
-
|
|
5
|
-
const MOCHA_SUITE_APP = Symbol.for('mocha#suite#app');
|
|
6
|
-
|
|
7
|
-
const debug = debuglog('@eggjs/mock/lib/inject_context');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Monkey patch the mocha instance with egg context.
|
|
11
|
-
*
|
|
12
|
-
* @param {Function} mocha - the module of mocha
|
|
13
|
-
*/
|
|
14
|
-
export function injectContext(mocha: any) {
|
|
15
|
-
if (mocha._injectContextLoaded) {
|
|
16
|
-
debug('mocha already injected context, skip it');
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
const { Runner } = mocha;
|
|
20
|
-
const runSuite = Runner.prototype.runSuite;
|
|
21
|
-
const runTests = Runner.prototype.runTests;
|
|
22
|
-
|
|
23
|
-
function getTestTitle(suite: any, test: any) {
|
|
24
|
-
const suiteTitle = suite.root ? 'root suite' : suite.title;
|
|
25
|
-
if (!test) {
|
|
26
|
-
return `"${suiteTitle}"`;
|
|
27
|
-
}
|
|
28
|
-
return `"${suiteTitle} - ${test.title}"`;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Inject ctx for before/after.
|
|
32
|
-
Runner.prototype.runSuite = async function(suite: any, fn: any) {
|
|
33
|
-
debug('run suite: %s', suite.title);
|
|
34
|
-
let app;
|
|
35
|
-
const self = this;
|
|
36
|
-
try {
|
|
37
|
-
app = await getApp(suite);
|
|
38
|
-
debug('get app: %s', !!app);
|
|
39
|
-
await app.ready();
|
|
40
|
-
} catch {
|
|
41
|
-
// 可能 app.ready 时报错,不使用失败的 app
|
|
42
|
-
app = null;
|
|
43
|
-
}
|
|
44
|
-
if (!app) {
|
|
45
|
-
// app 不存在,直接跳过,在 beforeEach 的 hook 中会报错
|
|
46
|
-
// 确保不打乱 mocha 的顺序,防止 mocha 内部状态错误
|
|
47
|
-
return runSuite.call(self, suite, fn);
|
|
48
|
-
}
|
|
49
|
-
let errSuite;
|
|
50
|
-
try {
|
|
51
|
-
suite.ctx[MOCHA_SUITE_APP] = app;
|
|
52
|
-
const mockContextFun = app.mockModuleContextScope || app.mockContextScope;
|
|
53
|
-
await mockContextFun.call(app, async function() {
|
|
54
|
-
await new Promise<void>(resolve => {
|
|
55
|
-
runSuite.call(self, suite, (aErrSuite: Error) => {
|
|
56
|
-
errSuite = aErrSuite;
|
|
57
|
-
resolve();
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
} catch (err) {
|
|
62
|
-
// mockContext 失败后动态注册一个 beforeAll hook
|
|
63
|
-
// 快速失败,直接阻塞后续用例
|
|
64
|
-
suite.beforeAll('egg-mock-mock-ctx-failed', async () => {
|
|
65
|
-
throw err;
|
|
66
|
-
});
|
|
67
|
-
return runSuite.call(self, suite, (aErrSuite: Error) => {
|
|
68
|
-
return fn(aErrSuite);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
return fn(errSuite);
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
// Inject ctx for beforeEach/it/afterEach.
|
|
75
|
-
// And ctx with before/after is not same as beforeEach/it/afterEach.
|
|
76
|
-
Runner.prototype.runTests = async function(suite: any, fn: any) {
|
|
77
|
-
const tests = suite.tests.slice();
|
|
78
|
-
if (!tests.length) {
|
|
79
|
-
return runTests.call(this, suite, fn);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const app = suite.ctx[MOCHA_SUITE_APP];
|
|
83
|
-
|
|
84
|
-
const self = this;
|
|
85
|
-
if (!app) {
|
|
86
|
-
return runTests.call(self, suite, fn);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function done(errSuite?: Error) {
|
|
90
|
-
suite.tests = tests;
|
|
91
|
-
return fn(errSuite);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async function next(i: number) {
|
|
95
|
-
const test = tests[i];
|
|
96
|
-
if (!test) {
|
|
97
|
-
return done();
|
|
98
|
-
}
|
|
99
|
-
suite.tests = [ test ];
|
|
100
|
-
|
|
101
|
-
let app;
|
|
102
|
-
try {
|
|
103
|
-
app = await getApp(suite, test);
|
|
104
|
-
assert(app, `not found app for test ${getTestTitle(suite, test)}`);
|
|
105
|
-
await app.ready();
|
|
106
|
-
} catch (err) {
|
|
107
|
-
self.fail(test, err);
|
|
108
|
-
return next(i + 1);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
const mockContextFun = app.mockModuleContextScope || app.mockContextScope;
|
|
113
|
-
await mockContextFun.call(app, async function() {
|
|
114
|
-
return await new Promise<void>(resolve => {
|
|
115
|
-
runTests.call(self, suite, () => {
|
|
116
|
-
return resolve();
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
} catch (err) {
|
|
121
|
-
self.fail(test, err);
|
|
122
|
-
return next(i + 1);
|
|
123
|
-
}
|
|
124
|
-
return next(i + 1);
|
|
125
|
-
}
|
|
126
|
-
next(0).catch(err => {
|
|
127
|
-
self.fail(suite, err);
|
|
128
|
-
done(suite);
|
|
129
|
-
});
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
mocha._injectContextLoaded = true;
|
|
133
|
-
debug('inject context success');
|
|
134
|
-
}
|
package/src/lib/mock_agent.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import {
|
|
3
|
-
MockAgent, setGlobalDispatcher, getGlobalDispatcher, Dispatcher,
|
|
4
|
-
HttpClient,
|
|
5
|
-
} from 'urllib';
|
|
6
|
-
|
|
7
|
-
const debug = debuglog('@eggjs/mock/lib/mock_agent');
|
|
8
|
-
|
|
9
|
-
declare namespace globalThis {
|
|
10
|
-
let __mockAgent: MockAgent | null;
|
|
11
|
-
let __globalDispatcher: Dispatcher;
|
|
12
|
-
let __httpClientDispatchers: Map<HttpClient, Dispatcher>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
globalThis.__mockAgent = null;
|
|
16
|
-
globalThis.__httpClientDispatchers = new Map<HttpClient, Dispatcher>();
|
|
17
|
-
|
|
18
|
-
export function getMockAgent(app?: { httpClient?: HttpClient }) {
|
|
19
|
-
debug('getMockAgent');
|
|
20
|
-
if (!globalThis.__globalDispatcher) {
|
|
21
|
-
globalThis.__globalDispatcher = getGlobalDispatcher();
|
|
22
|
-
debug('create global dispatcher');
|
|
23
|
-
}
|
|
24
|
-
if (app?.httpClient && !globalThis.__httpClientDispatchers.has(app.httpClient)) {
|
|
25
|
-
globalThis.__httpClientDispatchers.set(app.httpClient, app.httpClient.getDispatcher());
|
|
26
|
-
debug('add new httpClient, size: %d', globalThis.__httpClientDispatchers.size);
|
|
27
|
-
}
|
|
28
|
-
if (!globalThis.__mockAgent) {
|
|
29
|
-
globalThis.__mockAgent = new MockAgent();
|
|
30
|
-
setGlobalDispatcher(globalThis.__mockAgent);
|
|
31
|
-
if (typeof app?.httpClient?.setDispatcher === 'function') {
|
|
32
|
-
app.httpClient.setDispatcher(globalThis.__mockAgent);
|
|
33
|
-
}
|
|
34
|
-
debug('create new mockAgent');
|
|
35
|
-
}
|
|
36
|
-
return globalThis.__mockAgent;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export async function restoreMockAgent() {
|
|
40
|
-
debug('restoreMockAgent start');
|
|
41
|
-
if (globalThis.__globalDispatcher) {
|
|
42
|
-
setGlobalDispatcher(globalThis.__globalDispatcher);
|
|
43
|
-
debug('restore global dispatcher');
|
|
44
|
-
}
|
|
45
|
-
debug('restore httpClient, size: %d', globalThis.__httpClientDispatchers.size);
|
|
46
|
-
for (const [ httpClient, dispatcher ] of globalThis.__httpClientDispatchers) {
|
|
47
|
-
httpClient.setDispatcher(dispatcher);
|
|
48
|
-
}
|
|
49
|
-
globalThis.__httpClientDispatchers.clear();
|
|
50
|
-
if (globalThis.__mockAgent) {
|
|
51
|
-
const agent = globalThis.__mockAgent;
|
|
52
|
-
globalThis.__mockAgent = null;
|
|
53
|
-
await agent.close();
|
|
54
|
-
debug('close mockAgent');
|
|
55
|
-
}
|
|
56
|
-
debug('restoreMockAgent end');
|
|
57
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
|
|
3
|
-
const debug = debuglog('@eggjs/mock/lib/mock_custom_loader');
|
|
4
|
-
|
|
5
|
-
export function setCustomLoader(app: any) {
|
|
6
|
-
const customLoader = app.config.customLoader;
|
|
7
|
-
if (!customLoader) return;
|
|
8
|
-
|
|
9
|
-
for (const field of Object.keys(customLoader)) {
|
|
10
|
-
const loaderConfig = Object.assign({}, customLoader[field]);
|
|
11
|
-
loaderConfig.field = field;
|
|
12
|
-
addMethod(loaderConfig);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function addMethod(loaderConfig: any) {
|
|
16
|
-
const field = loaderConfig.field as string;
|
|
17
|
-
const appMethodName = 'mock' + field.replace(/^[a-z]/i, s => s.toUpperCase());
|
|
18
|
-
if (app[appMethodName]) {
|
|
19
|
-
app.coreLogger.warn('Can\'t override app.%s', appMethodName);
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
debug('[addMethod] %s => %j', appMethodName, loaderConfig);
|
|
23
|
-
app[appMethodName] = function(service: any, methodName: string, fn: any) {
|
|
24
|
-
if (typeof service === 'string') {
|
|
25
|
-
const arr = service.split('.');
|
|
26
|
-
service = loaderConfig.inject === 'ctx' ? this[field + 'Classes'] : this[field];
|
|
27
|
-
for (const key of arr) {
|
|
28
|
-
service = service[key];
|
|
29
|
-
}
|
|
30
|
-
service = service.prototype || service;
|
|
31
|
-
}
|
|
32
|
-
this._mockFn(service, methodName, fn);
|
|
33
|
-
return this;
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
}
|