@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/context.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9saWIvY29udGV4dC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxNQUFNLENBQUMsTUFBTSxPQUFPLEdBQUc7SUFDckIsZUFBZSxDQUFDLEtBQVU7UUFDeEIsTUFBTSxRQUFRLEdBQUcsS0FBSyxDQUFDLEtBQUssSUFBSSxLQUFLLENBQUMsSUFBSSxDQUFDO1FBQzNDLElBQUksUUFBUSxFQUFFLENBQUM7WUFDYixLQUFLLENBQUMsS0FBSyxHQUFHLFFBQVEsQ0FBQztRQUN6QixDQUFDO1FBRUQsTUFBTSxPQUFPLEdBQUcsSUFBSSxDQUFDLGdCQUFnQixDQUFDLEtBQUssQ0FBQyxDQUFDO1FBQzdDLElBQUksQ0FBQyxHQUFHLENBQUMsZ0JBQWdCLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxDQUFDO0lBQzFDLENBQUM7Q0FDSyxDQUFDIn0=
|
|
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 };
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { MockApplicationOptions, MockOptions } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/format_options.d.ts
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
+
* format the options
|
|
7
|
+
*/
|
|
8
|
+
declare function formatOptions(initOptions?: MockOptions): MockApplicationOptions;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { formatOptions };
|
|
@@ -1,93 +1,65 @@
|
|
|
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
1
|
import { getSourceDirname } from "./utils.js";
|
|
7
|
-
|
|
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");
|
|
8
10
|
/**
|
|
9
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (options.plugin !== false) {
|
|
55
|
-
// add self to plugin list
|
|
56
|
-
const pluginPath = process.cwd();
|
|
57
|
-
const pkgPath = path.join(pluginPath, 'package.json');
|
|
58
|
-
const pluginName = getPluginName(pkgPath);
|
|
59
|
-
if (options.plugin && !pluginName) {
|
|
60
|
-
throw new Error(`should set "eggPlugin" property in ${pkgPath}`);
|
|
61
|
-
}
|
|
62
|
-
if (pluginName) {
|
|
63
|
-
plugins[pluginName] = {
|
|
64
|
-
enable: true,
|
|
65
|
-
path: pluginPath,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
// mock HOME as baseDir, but ignore if it has been mocked
|
|
70
|
-
const env = process.env.EGG_SERVER_ENV;
|
|
71
|
-
if (!isMocked(process.env, 'HOME') && (env === 'default' || env === 'test' || env === 'prod')) {
|
|
72
|
-
mm(process.env, 'HOME', options.baseDir);
|
|
73
|
-
}
|
|
74
|
-
// disable cache after call mm.env(),
|
|
75
|
-
// otherwise it will use cache and won't load again.
|
|
76
|
-
if (process.env.EGG_MOCK_SERVER_ENV) {
|
|
77
|
-
options.cache = false;
|
|
78
|
-
}
|
|
79
|
-
debug('[formatOptions] options: %j', options);
|
|
80
|
-
return options;
|
|
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;
|
|
81
56
|
}
|
|
82
57
|
function getPluginName(pkgPath) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
catch {
|
|
90
|
-
// ignore
|
|
91
|
-
}
|
|
58
|
+
try {
|
|
59
|
+
const pkg = readJSONSync(pkgPath);
|
|
60
|
+
if (pkg.eggPlugin?.name) return pkg.eggPlugin.name;
|
|
61
|
+
} catch {}
|
|
92
62
|
}
|
|
93
|
-
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
export { formatOptions };
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
//#region src/lib/inject_context.d.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
* Monkey patch the mocha instance with egg context.
|
|
4
|
+
*
|
|
5
|
+
* @param {Function} mocha - the module of mocha
|
|
6
|
+
*/
|
|
7
|
+
declare function injectContext(mocha: any): void;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { injectContext };
|
|
@@ -1,126 +1,106 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import assert from 'node:assert';
|
|
3
1
|
import { getApp } from "./app_handler.js";
|
|
4
|
-
|
|
5
|
-
|
|
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");
|
|
6
8
|
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
try {
|
|
103
|
-
const mockContextFun = app.mockModuleContextScope || app.mockContextScope;
|
|
104
|
-
await mockContextFun.call(app, async function () {
|
|
105
|
-
return await new Promise((resolve) => {
|
|
106
|
-
runTests.call(self, suite, () => {
|
|
107
|
-
return resolve();
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
catch (err) {
|
|
113
|
-
self.fail(test, err);
|
|
114
|
-
return next(i + 1);
|
|
115
|
-
}
|
|
116
|
-
return next(i + 1);
|
|
117
|
-
}
|
|
118
|
-
next(0).catch((err) => {
|
|
119
|
-
self.fail(suite, err);
|
|
120
|
-
done(suite);
|
|
121
|
-
});
|
|
122
|
-
};
|
|
123
|
-
mocha._injectContextLoaded = true;
|
|
124
|
-
debug('inject context success');
|
|
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");
|
|
125
103
|
}
|
|
126
|
-
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
export { injectContext };
|
package/dist/lib/mock_agent.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { HttpClient, MockAgent } from "urllib";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/mock_agent.d.ts
|
|
4
|
+
declare function getMockAgent(app?: {
|
|
5
|
+
httpClient?: HttpClient;
|
|
4
6
|
}): MockAgent;
|
|
5
|
-
|
|
7
|
+
declare function restoreMockAgent(): Promise<void>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { getMockAgent, restoreMockAgent };
|
package/dist/lib/mock_agent.js
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import { debuglog } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { debuglog } from "node:util";
|
|
2
|
+
import { Dispatcher, HttpClient, MockAgent, getGlobalDispatcher, setGlobalDispatcher } from "urllib";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/mock_agent.ts
|
|
5
|
+
const debug = debuglog("egg/mock/lib/mock_agent");
|
|
4
6
|
globalThis.__mockAgent = null;
|
|
5
|
-
globalThis.__httpClientDispatchers = new Map();
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
return globalThis.__mockAgent;
|
|
7
|
+
globalThis.__httpClientDispatchers = /* @__PURE__ */ new Map();
|
|
8
|
+
function getMockAgent(app) {
|
|
9
|
+
debug("getMockAgent");
|
|
10
|
+
if (!globalThis.__globalDispatcher) {
|
|
11
|
+
globalThis.__globalDispatcher = getGlobalDispatcher();
|
|
12
|
+
debug("create global dispatcher");
|
|
13
|
+
}
|
|
14
|
+
if (app?.httpClient && !globalThis.__httpClientDispatchers.has(app.httpClient)) {
|
|
15
|
+
globalThis.__httpClientDispatchers.set(app.httpClient, app.httpClient.getDispatcher());
|
|
16
|
+
debug("add new httpClient, size: %d", globalThis.__httpClientDispatchers.size);
|
|
17
|
+
}
|
|
18
|
+
if (!globalThis.__mockAgent) {
|
|
19
|
+
globalThis.__mockAgent = new MockAgent();
|
|
20
|
+
setGlobalDispatcher(globalThis.__mockAgent);
|
|
21
|
+
if (typeof app?.httpClient?.setDispatcher === "function") app.httpClient.setDispatcher(globalThis.__mockAgent);
|
|
22
|
+
debug("create new mockAgent");
|
|
23
|
+
}
|
|
24
|
+
return globalThis.__mockAgent;
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
debug('restoreMockAgent end');
|
|
26
|
+
async function restoreMockAgent() {
|
|
27
|
+
debug("restoreMockAgent start");
|
|
28
|
+
if (globalThis.__globalDispatcher) {
|
|
29
|
+
setGlobalDispatcher(globalThis.__globalDispatcher);
|
|
30
|
+
debug("restore global dispatcher");
|
|
31
|
+
}
|
|
32
|
+
debug("restore httpClient, size: %d", globalThis.__httpClientDispatchers.size);
|
|
33
|
+
for (const [httpClient$1, dispatcher] of globalThis.__httpClientDispatchers) httpClient$1.setDispatcher(dispatcher);
|
|
34
|
+
globalThis.__httpClientDispatchers.clear();
|
|
35
|
+
if (globalThis.__mockAgent) {
|
|
36
|
+
const agent = globalThis.__mockAgent;
|
|
37
|
+
globalThis.__mockAgent = null;
|
|
38
|
+
await agent.close();
|
|
39
|
+
debug("close mockAgent");
|
|
40
|
+
}
|
|
41
|
+
debug("restoreMockAgent end");
|
|
44
42
|
}
|
|
45
|
-
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
export { getMockAgent, restoreMockAgent };
|