@eggjs/mock 7.0.0-beta.18 → 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.
- package/dist/agent-BKYkjoCx.js +111 -0
- package/dist/agent-CHCe8tnW.d.ts +24 -0
- package/dist/agent-DZ_fHoxJ.js +48 -0
- package/dist/agent_handler-DeUtoRVT.js +26 -0
- package/dist/app/extend/agent.d.ts +1 -1
- package/dist/app/extend/agent.js +3 -46
- package/dist/app/extend/application.d.ts +4 -174
- package/dist/app/extend/application.js +7 -383
- package/dist/app-6Bn3F5Uw.js +252 -0
- package/dist/app-CoypamK1.js +99 -0
- package/dist/app-l47a-gqT.d.ts +7 -0
- package/dist/app_handler-CkiK6fyC.js +55 -0
- package/dist/app_handler-Gp5J9X-v.d.ts +9 -0
- package/dist/application-BPpvw5dg.d.ts +175 -0
- package/dist/application-LrKwuOKn.js +385 -0
- package/dist/bootstrap.d.ts +9 -3
- package/dist/bootstrap.js +20 -3
- package/dist/cluster-CA_hr9eV.js +283 -0
- package/dist/cluster-RTfUwmUd.d.ts +126 -0
- package/dist/context-BIduDy1M.js +10 -0
- package/dist/format_options-LRnReKde.js +65 -0
- package/dist/index-BtBMDZg8.d.ts +78 -0
- package/dist/index.d.ts +10 -81
- package/dist/index.js +20 -45
- package/dist/inject_context-BxpcF-ds.js +106 -0
- package/dist/inject_mocha-BVf8v6El.js +38 -0
- package/dist/inject_mocha-Daf1Aj8M.d.ts +1 -0
- package/dist/inject_mocha.d.ts +1 -1
- package/dist/inject_mocha.js +21 -32
- package/dist/lib/agent_handler.d.ts +4 -1
- package/dist/lib/agent_handler.js +7 -24
- package/dist/lib/app.d.ts +5 -6
- package/dist/lib/app.js +12 -252
- package/dist/lib/app_handler.d.ts +5 -8
- package/dist/lib/app_handler.js +19 -54
- package/dist/lib/cluster.d.ts +5 -125
- package/dist/lib/cluster.js +5 -281
- package/dist/lib/context.js +1 -8
- package/dist/lib/format_options.d.ts +2 -2
- package/dist/lib/format_options.js +2 -63
- package/dist/lib/inject_context.js +20 -104
- package/dist/lib/mock_agent.js +1 -43
- package/dist/lib/mock_custom_loader.js +1 -33
- package/dist/lib/mock_http_server.js +1 -15
- package/dist/lib/mock_httpclient.d.ts +1 -39
- package/dist/lib/mock_httpclient.js +2 -108
- package/dist/lib/parallel/agent.d.ts +4 -23
- package/dist/lib/parallel/agent.js +6 -109
- package/dist/lib/parallel/app.d.ts +3 -2
- package/dist/lib/parallel/app.js +7 -97
- package/dist/lib/parallel/util.d.ts +1 -5
- package/dist/lib/parallel/util.js +2 -57
- package/dist/lib/restore.d.ts +1 -3
- package/dist/lib/restore.js +7 -14
- package/dist/lib/supertest.d.ts +1 -15
- package/dist/lib/supertest.js +3 -37
- package/dist/lib/types.d.ts +3 -76
- package/dist/lib/types.js +2 -0
- package/dist/lib/utils.js +1 -38
- package/dist/mock_agent-CxT7Q0_N.js +45 -0
- package/dist/mock_custom_loader-f_tHRojG.js +35 -0
- package/dist/mock_http_server-De0r07gr.js +17 -0
- package/dist/mock_httpclient-1EgQkMxi.d.ts +40 -0
- package/dist/mock_httpclient-B6UVI7GS.js +110 -0
- package/dist/register.d.ts +2 -0
- package/dist/register.js +22 -4
- package/dist/restore-BL0cE0KG.d.ts +4 -0
- package/dist/restore-CVQYXquh.js +16 -0
- package/dist/src-D22EjdGo.js +47 -0
- package/dist/supertest-BIds28yL.d.ts +16 -0
- package/dist/supertest-Cg412Los.js +39 -0
- package/dist/types-CPNMyF89.js +1 -0
- package/dist/types-CjZ-JeCo.d.ts +75 -0
- package/dist/util-1xYBaaoh.d.ts +6 -0
- package/dist/util-BpppqTXv.js +59 -0
- package/dist/utils-MhP_Krx1.js +40 -0
- package/package.json +7 -9
- package/dist/_virtual/rolldown_runtime.js +0 -7
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { EggTestRequest } from "./supertest-BIds28yL.js";
|
|
2
|
+
import { MockClusterApplicationOptions, MockClusterOptions } from "./types-CjZ-JeCo.js";
|
|
3
|
+
import { ApplicationUnittest } from "./application-BPpvw5dg.js";
|
|
4
|
+
import { Coffee } from "coffee";
|
|
5
|
+
|
|
6
|
+
//#region src/lib/cluster.d.ts
|
|
7
|
+
declare global {
|
|
8
|
+
var eggMockMasterPort: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A cluster version of egg.Application, you can test with supertest
|
|
12
|
+
* @example
|
|
13
|
+
* ```js
|
|
14
|
+
* const mm = require('mm');
|
|
15
|
+
* const request = require('supertest');
|
|
16
|
+
*
|
|
17
|
+
* describe('ClusterApplication', () => {
|
|
18
|
+
* let app;
|
|
19
|
+
* before(function (done) {
|
|
20
|
+
* app = mm.cluster({ baseDir });
|
|
21
|
+
* app.ready(done);
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* after(function () {
|
|
25
|
+
* app.close();
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* it('should 200', function (done) {
|
|
29
|
+
* request(app.callback())
|
|
30
|
+
* .get('/')
|
|
31
|
+
* .expect(200, done);
|
|
32
|
+
* });
|
|
33
|
+
* });
|
|
34
|
+
*/
|
|
35
|
+
declare class ClusterApplication extends Coffee {
|
|
36
|
+
[key: string | symbol]: any;
|
|
37
|
+
options: MockClusterApplicationOptions;
|
|
38
|
+
port: number;
|
|
39
|
+
baseDir: string;
|
|
40
|
+
closed: boolean;
|
|
41
|
+
private _address;
|
|
42
|
+
/**
|
|
43
|
+
* @class
|
|
44
|
+
* @param {Object} options
|
|
45
|
+
* - {String} baseDir - The directory of the application
|
|
46
|
+
* - {Object} plugins - Custom you plugins
|
|
47
|
+
* - {String} framework - The directory of the egg framework
|
|
48
|
+
* - {Boolean} [cache=true] - Cache application based on baseDir
|
|
49
|
+
* - {Boolean} [coverage=true] - Switch on process coverage, but it'll be slower
|
|
50
|
+
* - {Boolean} [clean=true] - Remove $baseDir/logs
|
|
51
|
+
* - {Object} [opt] - opt pass to coffee, such as { execArgv: ['--debug'] }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
constructor(options: MockClusterApplicationOptions);
|
|
55
|
+
/**
|
|
56
|
+
* the process that forked
|
|
57
|
+
* @member {ChildProcess}
|
|
58
|
+
*/
|
|
59
|
+
get process(): any;
|
|
60
|
+
/**
|
|
61
|
+
* Compatible API for supertest
|
|
62
|
+
*/
|
|
63
|
+
callback(): this;
|
|
64
|
+
/**
|
|
65
|
+
* Compatible API for supertest
|
|
66
|
+
* @member {String} url
|
|
67
|
+
* @private
|
|
68
|
+
*/
|
|
69
|
+
get url(): string;
|
|
70
|
+
/**
|
|
71
|
+
* Compatible API for supertest
|
|
72
|
+
*/
|
|
73
|
+
address(): {
|
|
74
|
+
port: number;
|
|
75
|
+
address: string | undefined;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Compatible API for supertest
|
|
79
|
+
*/
|
|
80
|
+
listen(): this;
|
|
81
|
+
/**
|
|
82
|
+
* kill the process
|
|
83
|
+
*/
|
|
84
|
+
close(): Promise<void>;
|
|
85
|
+
get isClosed(): boolean;
|
|
86
|
+
get router(): {
|
|
87
|
+
pathFor(url: string): any;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* get app[property] value in app worker
|
|
91
|
+
*/
|
|
92
|
+
getAppInstanceProperty(property: string): any;
|
|
93
|
+
/**
|
|
94
|
+
* collection logger message, then can be use on `expectLog()`
|
|
95
|
+
* it's different from `app.expectLog()`, only support string params.
|
|
96
|
+
*
|
|
97
|
+
* @param {String} [logger] - logger instance name, default is `logger`
|
|
98
|
+
* @function ClusterApplication#expectLog
|
|
99
|
+
*/
|
|
100
|
+
mockLog(logger?: string): void;
|
|
101
|
+
/**
|
|
102
|
+
* expect str in the logger
|
|
103
|
+
* it's different from `app.expectLog()`, only support string params.
|
|
104
|
+
*
|
|
105
|
+
* @param {String} str - test str
|
|
106
|
+
* @param {String} [logger] - logger instance name, default is `logger`
|
|
107
|
+
* @function ClusterApplication#expectLog
|
|
108
|
+
*/
|
|
109
|
+
expectLog(str: string, logger?: string): void;
|
|
110
|
+
/**
|
|
111
|
+
* not expect str in the logger
|
|
112
|
+
* it's different from `app.notExpectLog()`, only support string params.
|
|
113
|
+
*
|
|
114
|
+
* @param {String} str - test str
|
|
115
|
+
* @param {String} [logger] - logger instance name, default is `logger`
|
|
116
|
+
* @function ClusterApplication#notExpectLog
|
|
117
|
+
*/
|
|
118
|
+
notExpectLog(str: string, logger?: string): void;
|
|
119
|
+
httpRequest(): EggTestRequest;
|
|
120
|
+
_callFunctionOnAppWorker(method: string, args?: any[], property?: any, needResult?: boolean): any;
|
|
121
|
+
}
|
|
122
|
+
type MockClusterApplication = ClusterApplication & ApplicationUnittest;
|
|
123
|
+
declare function createCluster(initOptions?: MockClusterOptions): MockClusterApplication;
|
|
124
|
+
declare function restore(): Promise<void>;
|
|
125
|
+
//#endregion
|
|
126
|
+
export { ClusterApplication, MockClusterApplication, createCluster, restore };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/lib/context.ts
|
|
2
|
+
const context = { runInBackground(scope) {
|
|
3
|
+
const taskName = scope._name || scope.name;
|
|
4
|
+
if (taskName) scope._name = taskName;
|
|
5
|
+
const promise = this._runInBackground(scope);
|
|
6
|
+
this.app._backgroundTasks.push(promise);
|
|
7
|
+
} };
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
export { context };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { getSourceDirname } from "./utils-MhP_Krx1.js";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { readJSONSync } from "utility";
|
|
4
|
+
import { isMocked, mm } from "mm";
|
|
5
|
+
import { debuglog } from "node:util";
|
|
6
|
+
import { getFrameworkPath } from "@eggjs/utils";
|
|
7
|
+
|
|
8
|
+
//#region src/lib/format_options.ts
|
|
9
|
+
const debug = debuglog("egg/mock/lib/format_options");
|
|
10
|
+
/**
|
|
11
|
+
* format the options
|
|
12
|
+
*/
|
|
13
|
+
function formatOptions(initOptions) {
|
|
14
|
+
const options = {
|
|
15
|
+
baseDir: process.cwd(),
|
|
16
|
+
cache: true,
|
|
17
|
+
coverage: true,
|
|
18
|
+
clean: true,
|
|
19
|
+
...initOptions
|
|
20
|
+
};
|
|
21
|
+
if (!path.isAbsolute(options.baseDir)) options.baseDir = path.join(process.cwd(), "test/fixtures", options.baseDir);
|
|
22
|
+
let framework = initOptions?.framework ?? initOptions?.customEgg;
|
|
23
|
+
if (framework === true) {
|
|
24
|
+
framework = process.cwd();
|
|
25
|
+
options.plugin = false;
|
|
26
|
+
} else {
|
|
27
|
+
if (!framework) framework = "";
|
|
28
|
+
framework = getFrameworkPath({
|
|
29
|
+
framework,
|
|
30
|
+
baseDir: options.baseDir
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
options.framework = options.customEgg = framework;
|
|
34
|
+
const plugins = options.plugins = options.plugins || {};
|
|
35
|
+
let pluginPath = path.join(getSourceDirname(), "..");
|
|
36
|
+
if (pluginPath.endsWith("/dist") || pluginPath.endsWith("\\dist")) pluginPath = path.join(pluginPath, "..");
|
|
37
|
+
plugins["egg-mock"] = {
|
|
38
|
+
enable: true,
|
|
39
|
+
path: pluginPath
|
|
40
|
+
};
|
|
41
|
+
if (options.plugin !== false) {
|
|
42
|
+
const pluginPath$1 = process.cwd();
|
|
43
|
+
const pkgPath = path.join(pluginPath$1, "package.json");
|
|
44
|
+
const pluginName = getPluginName(pkgPath);
|
|
45
|
+
if (options.plugin && !pluginName) throw new Error(`should set "eggPlugin" property in ${pkgPath}`);
|
|
46
|
+
if (pluginName) plugins[pluginName] = {
|
|
47
|
+
enable: true,
|
|
48
|
+
path: pluginPath$1
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const env = process.env.EGG_SERVER_ENV;
|
|
52
|
+
if (!isMocked(process.env, "HOME") && (env === "default" || env === "test" || env === "prod")) mm(process.env, "HOME", options.baseDir);
|
|
53
|
+
if (process.env.EGG_MOCK_SERVER_ENV) options.cache = false;
|
|
54
|
+
debug("[formatOptions] options: %j", options);
|
|
55
|
+
return options;
|
|
56
|
+
}
|
|
57
|
+
function getPluginName(pkgPath) {
|
|
58
|
+
try {
|
|
59
|
+
const pkg = readJSONSync(pkgPath);
|
|
60
|
+
if (pkg.eggPlugin?.name) return pkg.eggPlugin.name;
|
|
61
|
+
} catch {}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
export { formatOptions };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createCluster } from "./cluster-RTfUwmUd.js";
|
|
2
|
+
import { createApp } from "./app-l47a-gqT.js";
|
|
3
|
+
import { restore as restore$1 } from "./restore-BL0cE0KG.js";
|
|
4
|
+
import { setGetAppCallback } from "./app_handler-Gp5J9X-v.js";
|
|
5
|
+
import * as mm0 from "mm";
|
|
6
|
+
import { mock } from "mm";
|
|
7
|
+
|
|
8
|
+
//#region src/index.d.ts
|
|
9
|
+
declare const mock$1: {
|
|
10
|
+
restore: typeof restore$1;
|
|
11
|
+
/**
|
|
12
|
+
* Create a egg mocked application
|
|
13
|
+
* @function mm#app
|
|
14
|
+
* @param {Object} [options]
|
|
15
|
+
* - {String} baseDir - The directory of the application
|
|
16
|
+
* - {Object} plugins - Custom you plugins
|
|
17
|
+
* - {String} framework - The directory of the egg framework
|
|
18
|
+
* - {Boolean} [true] cache - Cache application based on baseDir
|
|
19
|
+
* - {Boolean} [true] coverage - Switch on process coverage, but it'll be slower
|
|
20
|
+
* - {Boolean} [true] clean - Remove $baseDir/logs
|
|
21
|
+
* @return {App} return {@link Application}
|
|
22
|
+
* @example
|
|
23
|
+
* ```js
|
|
24
|
+
* const app = mm.app();
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
app: typeof createApp;
|
|
28
|
+
/**
|
|
29
|
+
* Create a egg mocked cluster application
|
|
30
|
+
* @function mm#cluster
|
|
31
|
+
* @see ClusterApplication
|
|
32
|
+
*/
|
|
33
|
+
cluster: typeof createCluster;
|
|
34
|
+
/**
|
|
35
|
+
* mock the serverEnv of Egg
|
|
36
|
+
* @member {Function} mm#env
|
|
37
|
+
* @param {String} env - contain default, test, prod, local, unittest
|
|
38
|
+
* @see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L78
|
|
39
|
+
*/
|
|
40
|
+
env(env: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* mock console level
|
|
43
|
+
* @param {String} level - logger level
|
|
44
|
+
*/
|
|
45
|
+
consoleLevel(level: string): void;
|
|
46
|
+
home(homePath?: string): void;
|
|
47
|
+
setGetAppCallback: typeof setGetAppCallback;
|
|
48
|
+
isMocked: typeof mm0.isMocked;
|
|
49
|
+
mock: typeof mock;
|
|
50
|
+
mm: typeof mock;
|
|
51
|
+
datas: typeof mm0.datas;
|
|
52
|
+
mockDatas: typeof mm0.datas;
|
|
53
|
+
data: typeof mm0.data;
|
|
54
|
+
mockData: typeof mm0.data;
|
|
55
|
+
dataWithAsyncDispose: typeof mm0.dataWithAsyncDispose;
|
|
56
|
+
empty: typeof mm0.empty;
|
|
57
|
+
mockEmpty: typeof mm0.empty;
|
|
58
|
+
error: typeof mm0.error;
|
|
59
|
+
mockError: typeof mm0.error;
|
|
60
|
+
spy: typeof mm0.spy;
|
|
61
|
+
errorOnce: typeof mm0.errorOnce;
|
|
62
|
+
syncError: typeof mm0.syncError;
|
|
63
|
+
syncEmpty: typeof mm0.syncEmpty;
|
|
64
|
+
syncData: typeof mm0.syncData;
|
|
65
|
+
http: {
|
|
66
|
+
request: (url: mm0.RequestURL, data: mm0.ResponseData, headers?: Record<string, any>, delay?: number) => void;
|
|
67
|
+
requestError: (url: mm0.RequestURL, reqError?: mm0.MockError, resError?: mm0.MockError, delay?: number) => void;
|
|
68
|
+
};
|
|
69
|
+
https: {
|
|
70
|
+
request: (url: mm0.RequestURL, data: mm0.ResponseData, headers?: Record<string, any>, delay?: number) => void;
|
|
71
|
+
requestError: (url: mm0.RequestURL, reqError?: mm0.MockError, resError?: mm0.MockError, delay?: number) => void;
|
|
72
|
+
};
|
|
73
|
+
spawn: typeof mm0.spawn;
|
|
74
|
+
classMethod: typeof mm0.classMethod;
|
|
75
|
+
};
|
|
76
|
+
declare const proxyMock: ((target: any, property: PropertyKey, value?: any) => void) & typeof mock$1;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { proxyMock };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,81 +1,10 @@
|
|
|
1
|
-
import { MockHttpClientMethod, MockResponseCallbackOptions, MockResultFunction, MockResultOptions, ResultObject } from "./
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
//#region src/index.d.ts
|
|
12
|
-
declare const mock$1: {
|
|
13
|
-
restore: typeof restore$1;
|
|
14
|
-
/**
|
|
15
|
-
* Create a egg mocked application
|
|
16
|
-
* @function mm#app
|
|
17
|
-
* @param {Object} [options]
|
|
18
|
-
* - {String} baseDir - The directory of the application
|
|
19
|
-
* - {Object} plugins - Custom you plugins
|
|
20
|
-
* - {String} framework - The directory of the egg framework
|
|
21
|
-
* - {Boolean} [true] cache - Cache application based on baseDir
|
|
22
|
-
* - {Boolean} [true] coverage - Switch on process coverage, but it'll be slower
|
|
23
|
-
* - {Boolean} [true] clean - Remove $baseDir/logs
|
|
24
|
-
* @return {App} return {@link Application}
|
|
25
|
-
* @example
|
|
26
|
-
* ```js
|
|
27
|
-
* const app = mm.app();
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
app: typeof createApp;
|
|
31
|
-
/**
|
|
32
|
-
* Create a egg mocked cluster application
|
|
33
|
-
* @function mm#cluster
|
|
34
|
-
* @see ClusterApplication
|
|
35
|
-
*/
|
|
36
|
-
cluster: typeof createCluster;
|
|
37
|
-
/**
|
|
38
|
-
* mock the serverEnv of Egg
|
|
39
|
-
* @member {Function} mm#env
|
|
40
|
-
* @param {String} env - contain default, test, prod, local, unittest
|
|
41
|
-
* @see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L78
|
|
42
|
-
*/
|
|
43
|
-
env(env: string): void;
|
|
44
|
-
/**
|
|
45
|
-
* mock console level
|
|
46
|
-
* @param {String} level - logger level
|
|
47
|
-
*/
|
|
48
|
-
consoleLevel(level: string): void;
|
|
49
|
-
home(homePath?: string): void;
|
|
50
|
-
setGetAppCallback: typeof setGetAppCallback;
|
|
51
|
-
isMocked: typeof mm0.isMocked;
|
|
52
|
-
mock: typeof mock;
|
|
53
|
-
mm: typeof mock;
|
|
54
|
-
datas: typeof mm0.datas;
|
|
55
|
-
mockDatas: typeof mm0.datas;
|
|
56
|
-
data: typeof mm0.data;
|
|
57
|
-
mockData: typeof mm0.data;
|
|
58
|
-
dataWithAsyncDispose: typeof mm0.dataWithAsyncDispose;
|
|
59
|
-
empty: typeof mm0.empty;
|
|
60
|
-
mockEmpty: typeof mm0.empty;
|
|
61
|
-
error: typeof mm0.error;
|
|
62
|
-
mockError: typeof mm0.error;
|
|
63
|
-
spy: typeof mm0.spy;
|
|
64
|
-
errorOnce: typeof mm0.errorOnce;
|
|
65
|
-
syncError: typeof mm0.syncError;
|
|
66
|
-
syncEmpty: typeof mm0.syncEmpty;
|
|
67
|
-
syncData: typeof mm0.syncData;
|
|
68
|
-
http: {
|
|
69
|
-
request: (url: mm0.RequestURL, data: mm0.ResponseData, headers?: Record<string, any>, delay?: number) => void;
|
|
70
|
-
requestError: (url: mm0.RequestURL, reqError?: mm0.MockError, resError?: mm0.MockError, delay?: number) => void;
|
|
71
|
-
};
|
|
72
|
-
https: {
|
|
73
|
-
request: (url: mm0.RequestURL, data: mm0.ResponseData, headers?: Record<string, any>, delay?: number) => void;
|
|
74
|
-
requestError: (url: mm0.RequestURL, reqError?: mm0.MockError, resError?: mm0.MockError, delay?: number) => void;
|
|
75
|
-
};
|
|
76
|
-
spawn: typeof mm0.spawn;
|
|
77
|
-
classMethod: typeof mm0.classMethod;
|
|
78
|
-
};
|
|
79
|
-
declare const proxyMock: ((target: any, property: PropertyKey, value?: any) => void) & typeof mock$1;
|
|
80
|
-
//#endregion
|
|
81
|
-
export { MockAgent, type ApplicationUnittest as MockApplication, MockApplicationOptions, type MockClusterApplication, MockClusterApplicationOptions, MockClusterOptions, MockHttpClientMethod, MockOption, MockOptions, MockResponseCallbackOptions, MockResultFunction, MockResultOptions, ResultObject, createApp, createCluster, proxyMock as default, proxyMock as mm, proxyMock as mock, setGetAppCallback };
|
|
1
|
+
import { MockHttpClientMethod, MockResponseCallbackOptions, MockResultFunction, MockResultOptions, ResultObject } from "./mock_httpclient-1EgQkMxi.js";
|
|
2
|
+
import "./supertest-BIds28yL.js";
|
|
3
|
+
import { MockAgent, MockApplicationOptions, MockClusterApplicationOptions, MockClusterOptions, MockOption, MockOptions } from "./types-CjZ-JeCo.js";
|
|
4
|
+
import { ApplicationUnittest } from "./application-BPpvw5dg.js";
|
|
5
|
+
import { MockClusterApplication, createCluster } from "./cluster-RTfUwmUd.js";
|
|
6
|
+
import { createApp } from "./app-l47a-gqT.js";
|
|
7
|
+
import "./restore-BL0cE0KG.js";
|
|
8
|
+
import { setGetAppCallback } from "./app_handler-Gp5J9X-v.js";
|
|
9
|
+
import { proxyMock } from "./index-BtBMDZg8.js";
|
|
10
|
+
export { MockAgent, ApplicationUnittest as MockApplication, MockApplicationOptions, MockClusterApplication, MockClusterApplicationOptions, MockClusterOptions, MockHttpClientMethod, MockOption, MockOptions, MockResponseCallbackOptions, MockResultFunction, MockResultOptions, ResultObject, createApp, createCluster, proxyMock as default, proxyMock as mm, proxyMock as mock, setGetAppCallback };
|
package/dist/index.js
CHANGED
|
@@ -1,47 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
import "./mock_http_server-De0r07gr.js";
|
|
2
|
+
import "./utils-MhP_Krx1.js";
|
|
3
|
+
import "./supertest-Cg412Los.js";
|
|
4
|
+
import "./format_options-LRnReKde.js";
|
|
5
|
+
import { createCluster } from "./cluster-CA_hr9eV.js";
|
|
6
|
+
import "./context-BIduDy1M.js";
|
|
7
|
+
import "./mock_custom_loader-f_tHRojG.js";
|
|
8
|
+
import "./mock_agent-CxT7Q0_N.js";
|
|
9
|
+
import "./mock_httpclient-B6UVI7GS.js";
|
|
10
|
+
import "./types-CPNMyF89.js";
|
|
11
|
+
import "./application-LrKwuOKn.js";
|
|
12
|
+
import "./agent-DZ_fHoxJ.js";
|
|
13
|
+
import { createApp } from "./app-6Bn3F5Uw.js";
|
|
14
|
+
import "./restore-CVQYXquh.js";
|
|
15
|
+
import "./util-BpppqTXv.js";
|
|
16
|
+
import "./app-CoypamK1.js";
|
|
17
|
+
import "./agent-BKYkjoCx.js";
|
|
18
|
+
import "./agent_handler-DeUtoRVT.js";
|
|
19
|
+
import { setGetAppCallback } from "./app_handler-CkiK6fyC.js";
|
|
20
|
+
import { proxyMock, src_default } from "./src-D22EjdGo.js";
|
|
6
21
|
|
|
7
|
-
//#region src/index.ts
|
|
8
|
-
const mock$1 = {
|
|
9
|
-
...mm$1,
|
|
10
|
-
restore: restore$1,
|
|
11
|
-
app: createApp,
|
|
12
|
-
cluster: createCluster,
|
|
13
|
-
env(env) {
|
|
14
|
-
mock(process.env, "EGG_MOCK_SERVER_ENV", env);
|
|
15
|
-
mock(process.env, "EGG_SERVER_ENV", env);
|
|
16
|
-
},
|
|
17
|
-
consoleLevel(level) {
|
|
18
|
-
level = (level || "").toUpperCase();
|
|
19
|
-
mock(process.env, "EGG_LOG", level);
|
|
20
|
-
},
|
|
21
|
-
home(homePath) {
|
|
22
|
-
if (homePath) mock(process.env, "EGG_HOME", homePath);
|
|
23
|
-
},
|
|
24
|
-
setGetAppCallback
|
|
25
|
-
};
|
|
26
|
-
const proxyMock = new Proxy(mock, {
|
|
27
|
-
apply(target, _, args) {
|
|
28
|
-
return target(args[0], args[1], args[2]);
|
|
29
|
-
},
|
|
30
|
-
get(_target, property, receiver) {
|
|
31
|
-
return Reflect.get(mock$1, property, receiver);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
var src_default = proxyMock;
|
|
35
|
-
process.setMaxListeners(100);
|
|
36
|
-
process.once("SIGQUIT", () => {
|
|
37
|
-
process.exit(0);
|
|
38
|
-
});
|
|
39
|
-
process.once("SIGTERM", () => {
|
|
40
|
-
process.exit(0);
|
|
41
|
-
});
|
|
42
|
-
process.once("SIGINT", () => {
|
|
43
|
-
process.exit(0);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
//#endregion
|
|
47
22
|
export { createApp, createCluster, src_default as default, proxyMock as mm, proxyMock as mock, setGetAppCallback };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { getApp } from "./app_handler-CkiK6fyC.js";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { debuglog } from "node:util";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/inject_context.ts
|
|
6
|
+
const debug = debuglog("egg/mock/lib/inject_context");
|
|
7
|
+
const MOCHA_SUITE_APP = Symbol.for("mocha#suite#app");
|
|
8
|
+
/**
|
|
9
|
+
* Monkey patch the mocha instance with egg context.
|
|
10
|
+
*
|
|
11
|
+
* @param {Function} mocha - the module of mocha
|
|
12
|
+
*/
|
|
13
|
+
function injectContext(mocha) {
|
|
14
|
+
if (mocha._injectContextLoaded) {
|
|
15
|
+
debug("mocha already injected context, skip it");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const { Runner } = mocha;
|
|
19
|
+
const runSuite = Runner.prototype.runSuite;
|
|
20
|
+
const runTests = Runner.prototype.runTests;
|
|
21
|
+
function getTestTitle(suite, test) {
|
|
22
|
+
const suiteTitle = suite.root ? "root suite" : suite.title;
|
|
23
|
+
if (!test) return `"${suiteTitle}"`;
|
|
24
|
+
return `"${suiteTitle} - ${test.title}"`;
|
|
25
|
+
}
|
|
26
|
+
Runner.prototype.runSuite = async function(suite, fn) {
|
|
27
|
+
debug("run suite: %s", suite.title);
|
|
28
|
+
let app;
|
|
29
|
+
const self = this;
|
|
30
|
+
try {
|
|
31
|
+
app = await getApp(suite);
|
|
32
|
+
debug("get app: %s", !!app);
|
|
33
|
+
await app.ready();
|
|
34
|
+
} catch {
|
|
35
|
+
app = null;
|
|
36
|
+
}
|
|
37
|
+
if (!app) return runSuite.call(self, suite, fn);
|
|
38
|
+
let errSuite;
|
|
39
|
+
try {
|
|
40
|
+
suite.ctx[MOCHA_SUITE_APP] = app;
|
|
41
|
+
await (app.mockModuleContextScope || app.mockContextScope).call(app, async function() {
|
|
42
|
+
await new Promise((resolve) => {
|
|
43
|
+
runSuite.call(self, suite, (aErrSuite) => {
|
|
44
|
+
errSuite = aErrSuite;
|
|
45
|
+
resolve();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
} catch (err) {
|
|
50
|
+
suite.beforeAll("egg-mock-mock-ctx-failed", async () => {
|
|
51
|
+
throw err;
|
|
52
|
+
});
|
|
53
|
+
return runSuite.call(self, suite, (aErrSuite) => {
|
|
54
|
+
return fn(aErrSuite);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return fn(errSuite);
|
|
58
|
+
};
|
|
59
|
+
Runner.prototype.runTests = async function(suite, fn) {
|
|
60
|
+
const tests = suite.tests.slice();
|
|
61
|
+
if (!tests.length) return runTests.call(this, suite, fn);
|
|
62
|
+
const app = suite.ctx[MOCHA_SUITE_APP];
|
|
63
|
+
const self = this;
|
|
64
|
+
if (!app) return runTests.call(self, suite, fn);
|
|
65
|
+
function done(errSuite) {
|
|
66
|
+
suite.tests = tests;
|
|
67
|
+
return fn(errSuite);
|
|
68
|
+
}
|
|
69
|
+
async function next(i) {
|
|
70
|
+
const test = tests[i];
|
|
71
|
+
if (!test) return done();
|
|
72
|
+
suite.tests = [test];
|
|
73
|
+
let app$1;
|
|
74
|
+
try {
|
|
75
|
+
app$1 = await getApp(suite, test);
|
|
76
|
+
assert(app$1, `not found app for test ${getTestTitle(suite, test)}`);
|
|
77
|
+
await app$1.ready();
|
|
78
|
+
} catch (err) {
|
|
79
|
+
self.fail(test, err);
|
|
80
|
+
return next(i + 1);
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
await (app$1.mockModuleContextScope || app$1.mockContextScope).call(app$1, async function() {
|
|
84
|
+
return await new Promise((resolve) => {
|
|
85
|
+
runTests.call(self, suite, () => {
|
|
86
|
+
return resolve();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
} catch (err) {
|
|
91
|
+
self.fail(test, err);
|
|
92
|
+
return next(i + 1);
|
|
93
|
+
}
|
|
94
|
+
return next(i + 1);
|
|
95
|
+
}
|
|
96
|
+
next(0).catch((err) => {
|
|
97
|
+
self.fail(suite, err);
|
|
98
|
+
done(suite);
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
mocha._injectContextLoaded = true;
|
|
102
|
+
debug("inject context success");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
export { injectContext };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { injectContext } from "./inject_context-BxpcF-ds.js";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { debuglog } from "node:util";
|
|
4
|
+
import { getRequire } from "@eggjs/utils";
|
|
5
|
+
import "mocha";
|
|
6
|
+
|
|
7
|
+
//#region rolldown:runtime
|
|
8
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/inject_mocha.ts
|
|
12
|
+
const debug = debuglog("egg/mock/inject_mocha");
|
|
13
|
+
/**
|
|
14
|
+
* Find active node mocha instances.
|
|
15
|
+
*/
|
|
16
|
+
function findNodeJSMocha() {
|
|
17
|
+
let children;
|
|
18
|
+
if (typeof __require === "function") children = __require.cache || {};
|
|
19
|
+
else {
|
|
20
|
+
children = getRequire().cache || {};
|
|
21
|
+
debug("createRequire on esm");
|
|
22
|
+
}
|
|
23
|
+
return Object.keys(children).filter(function(child) {
|
|
24
|
+
const val = children[child].exports;
|
|
25
|
+
return typeof val === "function" && val.name === "Mocha";
|
|
26
|
+
}).map(function(child) {
|
|
27
|
+
return children[child].exports;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
const modules = findNodeJSMocha();
|
|
31
|
+
debug("modules length: %s", modules.length);
|
|
32
|
+
for (const module of modules) {
|
|
33
|
+
if (!module) continue;
|
|
34
|
+
injectContext(module);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
export { };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "mocha";
|
package/dist/inject_mocha.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import "
|
|
1
|
+
import "./inject_mocha-Daf1Aj8M.js";
|
package/dist/inject_mocha.js
CHANGED
|
@@ -1,34 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import "
|
|
1
|
+
import "./inject_mocha-BVf8v6El.js";
|
|
2
|
+
import "./mock_http_server-De0r07gr.js";
|
|
3
|
+
import "./utils-MhP_Krx1.js";
|
|
4
|
+
import "./supertest-Cg412Los.js";
|
|
5
|
+
import "./format_options-LRnReKde.js";
|
|
6
|
+
import "./cluster-CA_hr9eV.js";
|
|
7
|
+
import "./context-BIduDy1M.js";
|
|
8
|
+
import "./mock_custom_loader-f_tHRojG.js";
|
|
9
|
+
import "./mock_agent-CxT7Q0_N.js";
|
|
10
|
+
import "./mock_httpclient-B6UVI7GS.js";
|
|
11
|
+
import "./types-CPNMyF89.js";
|
|
12
|
+
import "./application-LrKwuOKn.js";
|
|
13
|
+
import "./agent-DZ_fHoxJ.js";
|
|
14
|
+
import "./app-6Bn3F5Uw.js";
|
|
15
|
+
import "./restore-CVQYXquh.js";
|
|
16
|
+
import "./util-BpppqTXv.js";
|
|
17
|
+
import "./app-CoypamK1.js";
|
|
18
|
+
import "./agent-BKYkjoCx.js";
|
|
19
|
+
import "./agent_handler-DeUtoRVT.js";
|
|
20
|
+
import "./app_handler-CkiK6fyC.js";
|
|
21
|
+
import "./inject_context-BxpcF-ds.js";
|
|
6
22
|
|
|
7
|
-
//#region src/inject_mocha.ts
|
|
8
|
-
const debug = debuglog("egg/mock/inject_mocha");
|
|
9
|
-
/**
|
|
10
|
-
* Find active node mocha instances.
|
|
11
|
-
*/
|
|
12
|
-
function findNodeJSMocha() {
|
|
13
|
-
let children;
|
|
14
|
-
if (typeof __require === "function") children = __require.cache || {};
|
|
15
|
-
else {
|
|
16
|
-
children = getRequire().cache || {};
|
|
17
|
-
debug("createRequire on esm");
|
|
18
|
-
}
|
|
19
|
-
return Object.keys(children).filter(function(child) {
|
|
20
|
-
const val = children[child].exports;
|
|
21
|
-
return typeof val === "function" && val.name === "Mocha";
|
|
22
|
-
}).map(function(child) {
|
|
23
|
-
return children[child].exports;
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
const modules = findNodeJSMocha();
|
|
27
|
-
debug("modules length: %s", modules.length);
|
|
28
|
-
for (const module of modules) {
|
|
29
|
-
if (!module) continue;
|
|
30
|
-
injectContext(module);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
//#endregion
|
|
34
23
|
export { };
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "../mock_httpclient-1EgQkMxi.js";
|
|
2
|
+
import "../types-CjZ-JeCo.js";
|
|
3
|
+
import "../util-1xYBaaoh.js";
|
|
4
|
+
import { MockAgent } from "../agent-CHCe8tnW.js";
|
|
2
5
|
|
|
3
6
|
//#region src/lib/agent_handler.d.ts
|
|
4
7
|
declare function setupAgent(): Promise<MockAgent>;
|