@eggjs/mock 7.0.0-beta.34 → 7.0.0-beta.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/rolldown_runtime.js +7 -0
- package/dist/app/extend/agent.d.ts +38 -34
- package/dist/app/extend/agent.js +48 -46
- package/dist/app/extend/application.d.ts +171 -167
- package/dist/app/extend/application.js +382 -442
- package/dist/app/middleware/cluster_app_mock.d.ts +5 -2
- package/dist/app/middleware/cluster_app_mock.js +95 -100
- package/dist/app.d.ts +9 -5
- package/dist/app.js +17 -18
- package/dist/bootstrap.d.ts +9 -5
- package/dist/bootstrap.js +11 -12
- package/dist/index.d.ts +20 -17
- package/dist/index.js +39 -78
- package/dist/inject_mocha.d.ts +1 -1
- package/dist/inject_mocha.js +27 -28
- package/dist/lib/agent_handler.d.ts +7 -3
- package/dist/lib/agent_handler.js +22 -20
- package/dist/lib/app.d.ts +7 -3
- package/dist/lib/app.js +246 -287
- package/dist/lib/app_handler.d.ts +9 -5
- package/dist/lib/app_handler.js +48 -61
- package/dist/lib/cluster.d.ts +122 -118
- package/dist/lib/cluster.js +278 -335
- package/dist/lib/context.d.ts +4 -1
- package/dist/lib/context.js +10 -11
- package/dist/lib/format_options.d.ts +9 -4
- package/dist/lib/format_options.js +60 -88
- package/dist/lib/inject_context.d.ts +8 -5
- package/dist/lib/inject_context.js +103 -123
- package/dist/lib/mock_agent.d.ts +8 -4
- package/dist/lib/mock_agent.js +42 -42
- package/dist/lib/mock_custom_loader.d.ts +4 -1
- package/dist/lib/mock_custom_loader.js +34 -33
- package/dist/lib/mock_http_server.d.ts +6 -2
- package/dist/lib/mock_http_server.js +16 -17
- package/dist/lib/mock_httpclient.d.ts +38 -34
- package/dist/lib/mock_httpclient.js +106 -142
- package/dist/lib/parallel/agent.d.ts +22 -18
- package/dist/lib/parallel/agent.js +106 -112
- package/dist/lib/parallel/app.d.ts +23 -19
- package/dist/lib/parallel/app.js +93 -106
- package/dist/lib/parallel/util.d.ts +5 -2
- package/dist/lib/parallel/util.js +56 -63
- package/dist/lib/prerequire.d.ts +1 -1
- package/dist/lib/prerequire.js +1 -25
- package/dist/lib/request_call_function.d.ts +1 -1
- package/dist/lib/request_call_function.js +34 -47
- package/dist/lib/restore.d.ts +4 -1
- package/dist/lib/restore.js +14 -11
- package/dist/lib/start-cluster.d.ts +1 -2
- package/dist/lib/start-cluster.js +15 -14
- package/dist/lib/supertest.d.ts +14 -9
- package/dist/lib/supertest.js +32 -36
- package/dist/lib/tmp/empty.d.ts +1 -1
- package/dist/lib/tmp/empty.js +1 -2
- package/dist/lib/types.d.ts +71 -67
- package/dist/lib/types.js +1 -2
- package/dist/lib/utils.d.ts +11 -8
- package/dist/lib/utils.js +34 -63
- package/dist/register.d.ts +11 -7
- package/dist/register.js +36 -40
- package/dist/typings/index.d.ts +1 -0
- package/package.json +36 -39
package/dist/lib/app_handler.js
CHANGED
|
@@ -1,69 +1,56 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import { setupAgent } from "./agent_handler.js";
|
|
1
|
+
import { getEggOptions } from "./utils.js";
|
|
2
|
+
import "../app/extend/application.js";
|
|
4
3
|
import { createApp } from "./app.js";
|
|
4
|
+
import { setupAgent } from "./agent_handler.js";
|
|
5
|
+
import { createApp as createApp$1 } from "./parallel/app.js";
|
|
5
6
|
import { restore } from "./restore.js";
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
import { debuglog } from "node:util";
|
|
8
|
+
|
|
9
|
+
//#region src/lib/app_handler.ts
|
|
10
|
+
const debug = debuglog("egg/mock/lib/app_handler");
|
|
9
11
|
globalThis.__eggMockAppInstance = null;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
beforeAll(() => app.ready());
|
|
39
|
-
}
|
|
40
|
-
// @ts-ignore mocha tsd
|
|
41
|
-
if (typeof afterEach === 'function') {
|
|
42
|
-
// mocha and jest
|
|
43
|
-
// @ts-ignore mocha tsd
|
|
44
|
-
afterEach(() => app.backgroundTasksFinished());
|
|
45
|
-
// @ts-ignore mocha tsd
|
|
46
|
-
afterEach(restore);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
globalThis.__eggMockAppInstance = app;
|
|
50
|
-
return app;
|
|
12
|
+
function setupApp() {
|
|
13
|
+
let app = globalThis.__eggMockAppInstance;
|
|
14
|
+
if (app) {
|
|
15
|
+
debug("return exists app");
|
|
16
|
+
return app;
|
|
17
|
+
}
|
|
18
|
+
const options = getEggOptions();
|
|
19
|
+
debug("env.ENABLE_MOCHA_PARALLEL: %s, process.env.AUTO_AGENT: %s", process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT);
|
|
20
|
+
if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
|
|
21
|
+
app = createApp$1({
|
|
22
|
+
...options,
|
|
23
|
+
beforeInit: async (parallelApp) => {
|
|
24
|
+
const agent = await setupAgent();
|
|
25
|
+
parallelApp.options.clusterPort = agent.options.clusterPort;
|
|
26
|
+
debug("mockParallelApp beforeInit get clusterPort: %s", parallelApp.options.clusterPort);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
debug("mockParallelApp app: %s", !!app);
|
|
30
|
+
} else {
|
|
31
|
+
app = createApp(options);
|
|
32
|
+
if (typeof beforeAll === "function") beforeAll(() => app.ready());
|
|
33
|
+
if (typeof afterEach === "function") {
|
|
34
|
+
afterEach(() => app.backgroundTasksFinished());
|
|
35
|
+
afterEach(restore);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
globalThis.__eggMockAppInstance = app;
|
|
39
|
+
return app;
|
|
51
40
|
}
|
|
52
41
|
let getAppCallback;
|
|
53
|
-
|
|
54
|
-
|
|
42
|
+
function setGetAppCallback(cb) {
|
|
43
|
+
getAppCallback = cb;
|
|
55
44
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (app) {
|
|
62
|
-
await app.ready();
|
|
63
|
-
}
|
|
64
|
-
return app;
|
|
45
|
+
async function getApp(suite, test) {
|
|
46
|
+
if (getAppCallback) return getAppCallback(suite, test);
|
|
47
|
+
const app = globalThis.__eggMockAppInstance;
|
|
48
|
+
if (app) await app.ready();
|
|
49
|
+
return app;
|
|
65
50
|
}
|
|
66
|
-
|
|
67
|
-
|
|
51
|
+
function getBootstrapApp() {
|
|
52
|
+
return globalThis.__eggMockAppInstance;
|
|
68
53
|
}
|
|
69
|
-
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
export { getApp, getBootstrapApp, setGetAppCallback, setupApp };
|
package/dist/lib/cluster.d.ts
CHANGED
|
@@ -1,123 +1,127 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
import { request } from "./supertest.js";
|
|
2
|
+
import { MockClusterApplicationOptions, MockClusterOptions } from "./types.js";
|
|
3
|
+
import ApplicationUnittest from "../app/extend/application.js";
|
|
4
|
+
import childProcess from "node:child_process";
|
|
5
|
+
import { Coffee } from "coffee";
|
|
6
|
+
|
|
7
|
+
//#region src/lib/cluster.d.ts
|
|
6
8
|
declare global {
|
|
7
|
-
|
|
9
|
+
var eggMockMasterPort: number;
|
|
8
10
|
}
|
|
9
11
|
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
12
|
+
* A cluster version of egg.Application, you can test with supertest
|
|
13
|
+
* @example
|
|
14
|
+
* ```js
|
|
15
|
+
* const mm = require('mm');
|
|
16
|
+
* const request = require('supertest');
|
|
17
|
+
*
|
|
18
|
+
* describe('ClusterApplication', () => {
|
|
19
|
+
* let app;
|
|
20
|
+
* before(function (done) {
|
|
21
|
+
* app = mm.cluster({ baseDir });
|
|
22
|
+
* app.ready(done);
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* after(function () {
|
|
26
|
+
* app.close();
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* it('should 200', function (done) {
|
|
30
|
+
* request(app.callback())
|
|
31
|
+
* .get('/')
|
|
32
|
+
* .expect(200, done);
|
|
33
|
+
* });
|
|
34
|
+
* });
|
|
35
|
+
*/
|
|
36
|
+
declare class ClusterApplication extends Coffee {
|
|
37
|
+
[key: string | symbol]: any;
|
|
38
|
+
options: MockClusterApplicationOptions;
|
|
39
|
+
port: number;
|
|
40
|
+
baseDir: string;
|
|
41
|
+
closed: boolean;
|
|
42
|
+
private _address;
|
|
43
|
+
/**
|
|
44
|
+
* @class
|
|
45
|
+
* @param {Object} options
|
|
46
|
+
* - {String} baseDir - The directory of the application
|
|
47
|
+
* - {Object} plugins - Custom you plugins
|
|
48
|
+
* - {String} framework - The directory of the egg framework
|
|
49
|
+
* - {Boolean} [cache=true] - Cache application based on baseDir
|
|
50
|
+
* - {Boolean} [coverage=true] - Switch on process coverage, but it'll be slower
|
|
51
|
+
* - {Boolean} [clean=true] - Remove $baseDir/logs
|
|
52
|
+
* - {Object} [opt] - opt pass to coffee, such as { execArgv: ['--debug'] }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
constructor(options: MockClusterApplicationOptions);
|
|
56
|
+
/**
|
|
57
|
+
* the process that forked
|
|
58
|
+
* @member {ChildProcess}
|
|
59
|
+
*/
|
|
60
|
+
get process(): childProcess.ChildProcess;
|
|
61
|
+
/**
|
|
62
|
+
* Compatible API for supertest
|
|
63
|
+
*/
|
|
64
|
+
callback(): this;
|
|
65
|
+
/**
|
|
66
|
+
* Compatible API for supertest
|
|
67
|
+
* @member {String} url
|
|
68
|
+
* @private
|
|
69
|
+
*/
|
|
70
|
+
get url(): string;
|
|
71
|
+
/**
|
|
72
|
+
* Compatible API for supertest
|
|
73
|
+
*/
|
|
74
|
+
address(): {
|
|
37
75
|
port: number;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
get isClosed(): boolean;
|
|
85
|
-
get router(): {
|
|
86
|
-
pathFor: (url: string) => any;
|
|
87
|
-
};
|
|
88
|
-
/**
|
|
89
|
-
* get app[property] value in app worker
|
|
90
|
-
*/
|
|
91
|
-
getAppInstanceProperty(property: string): any;
|
|
92
|
-
/**
|
|
93
|
-
* collection logger message, then can be use on `expectLog()`
|
|
94
|
-
* it's different from `app.expectLog()`, only support string params.
|
|
95
|
-
*
|
|
96
|
-
* @param {String} [logger] - logger instance name, default is `logger`
|
|
97
|
-
* @function ClusterApplication#expectLog
|
|
98
|
-
*/
|
|
99
|
-
mockLog(logger?: string): void;
|
|
100
|
-
/**
|
|
101
|
-
* expect str in the logger
|
|
102
|
-
* it's different from `app.expectLog()`, only support string params.
|
|
103
|
-
*
|
|
104
|
-
* @param {String} str - test str
|
|
105
|
-
* @param {String} [logger] - logger instance name, default is `logger`
|
|
106
|
-
* @function ClusterApplication#expectLog
|
|
107
|
-
*/
|
|
108
|
-
expectLog(str: string, logger?: string): void;
|
|
109
|
-
/**
|
|
110
|
-
* not expect str in the logger
|
|
111
|
-
* it's different from `app.notExpectLog()`, only support string params.
|
|
112
|
-
*
|
|
113
|
-
* @param {String} str - test str
|
|
114
|
-
* @param {String} [logger] - logger instance name, default is `logger`
|
|
115
|
-
* @function ClusterApplication#notExpectLog
|
|
116
|
-
*/
|
|
117
|
-
notExpectLog(str: string, logger?: string): void;
|
|
118
|
-
httpRequest(): ReturnType<typeof supertestRequest>;
|
|
119
|
-
_callFunctionOnAppWorker(method: string, args?: any[], property?: any, needResult?: boolean): any;
|
|
76
|
+
address: string | undefined;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Compatible API for supertest
|
|
80
|
+
*/
|
|
81
|
+
listen(): this;
|
|
82
|
+
/**
|
|
83
|
+
* kill the process
|
|
84
|
+
*/
|
|
85
|
+
close(): Promise<void>;
|
|
86
|
+
get isClosed(): boolean;
|
|
87
|
+
get router(): {
|
|
88
|
+
pathFor: (url: string) => any;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* get app[property] value in app worker
|
|
92
|
+
*/
|
|
93
|
+
getAppInstanceProperty(property: string): any;
|
|
94
|
+
/**
|
|
95
|
+
* collection logger message, then can be use on `expectLog()`
|
|
96
|
+
* it's different from `app.expectLog()`, only support string params.
|
|
97
|
+
*
|
|
98
|
+
* @param {String} [logger] - logger instance name, default is `logger`
|
|
99
|
+
* @function ClusterApplication#expectLog
|
|
100
|
+
*/
|
|
101
|
+
mockLog(logger?: string): void;
|
|
102
|
+
/**
|
|
103
|
+
* expect str in the logger
|
|
104
|
+
* it's different from `app.expectLog()`, only support string params.
|
|
105
|
+
*
|
|
106
|
+
* @param {String} str - test str
|
|
107
|
+
* @param {String} [logger] - logger instance name, default is `logger`
|
|
108
|
+
* @function ClusterApplication#expectLog
|
|
109
|
+
*/
|
|
110
|
+
expectLog(str: string, logger?: string): void;
|
|
111
|
+
/**
|
|
112
|
+
* not expect str in the logger
|
|
113
|
+
* it's different from `app.notExpectLog()`, only support string params.
|
|
114
|
+
*
|
|
115
|
+
* @param {String} str - test str
|
|
116
|
+
* @param {String} [logger] - logger instance name, default is `logger`
|
|
117
|
+
* @function ClusterApplication#notExpectLog
|
|
118
|
+
*/
|
|
119
|
+
notExpectLog(str: string, logger?: string): void;
|
|
120
|
+
httpRequest(): ReturnType<typeof request>;
|
|
121
|
+
_callFunctionOnAppWorker(method: string, args?: any[], property?: any, needResult?: boolean): any;
|
|
120
122
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
type MockClusterApplication = ClusterApplication & ApplicationUnittest;
|
|
124
|
+
declare function createCluster(initOptions?: MockClusterOptions): MockClusterApplication;
|
|
125
|
+
declare function restore(): Promise<void>;
|
|
126
|
+
//#endregion
|
|
127
|
+
export { ClusterApplication, MockClusterApplication, createCluster, restore };
|