@eggjs/mock 7.0.0-beta.35 → 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.
Files changed (63) hide show
  1. package/dist/_virtual/rolldown_runtime.js +7 -0
  2. package/dist/app/extend/agent.d.ts +38 -34
  3. package/dist/app/extend/agent.js +48 -46
  4. package/dist/app/extend/application.d.ts +171 -167
  5. package/dist/app/extend/application.js +382 -442
  6. package/dist/app/middleware/cluster_app_mock.d.ts +5 -2
  7. package/dist/app/middleware/cluster_app_mock.js +95 -100
  8. package/dist/app.d.ts +9 -5
  9. package/dist/app.js +17 -18
  10. package/dist/bootstrap.d.ts +9 -5
  11. package/dist/bootstrap.js +11 -12
  12. package/dist/index.d.ts +20 -17
  13. package/dist/index.js +39 -78
  14. package/dist/inject_mocha.d.ts +1 -1
  15. package/dist/inject_mocha.js +27 -28
  16. package/dist/lib/agent_handler.d.ts +7 -3
  17. package/dist/lib/agent_handler.js +22 -20
  18. package/dist/lib/app.d.ts +7 -3
  19. package/dist/lib/app.js +245 -286
  20. package/dist/lib/app_handler.d.ts +9 -5
  21. package/dist/lib/app_handler.js +48 -61
  22. package/dist/lib/cluster.d.ts +122 -118
  23. package/dist/lib/cluster.js +278 -335
  24. package/dist/lib/context.d.ts +4 -1
  25. package/dist/lib/context.js +10 -11
  26. package/dist/lib/format_options.d.ts +9 -4
  27. package/dist/lib/format_options.js +60 -88
  28. package/dist/lib/inject_context.d.ts +8 -5
  29. package/dist/lib/inject_context.js +103 -123
  30. package/dist/lib/mock_agent.d.ts +8 -4
  31. package/dist/lib/mock_agent.js +42 -42
  32. package/dist/lib/mock_custom_loader.d.ts +4 -1
  33. package/dist/lib/mock_custom_loader.js +34 -33
  34. package/dist/lib/mock_http_server.d.ts +6 -2
  35. package/dist/lib/mock_http_server.js +16 -17
  36. package/dist/lib/mock_httpclient.d.ts +38 -34
  37. package/dist/lib/mock_httpclient.js +106 -142
  38. package/dist/lib/parallel/agent.d.ts +22 -18
  39. package/dist/lib/parallel/agent.js +106 -112
  40. package/dist/lib/parallel/app.d.ts +23 -19
  41. package/dist/lib/parallel/app.js +93 -106
  42. package/dist/lib/parallel/util.d.ts +5 -2
  43. package/dist/lib/parallel/util.js +56 -63
  44. package/dist/lib/prerequire.d.ts +1 -1
  45. package/dist/lib/prerequire.js +1 -25
  46. package/dist/lib/request_call_function.d.ts +1 -1
  47. package/dist/lib/request_call_function.js +34 -47
  48. package/dist/lib/restore.d.ts +4 -1
  49. package/dist/lib/restore.js +14 -11
  50. package/dist/lib/start-cluster.d.ts +1 -2
  51. package/dist/lib/start-cluster.js +15 -14
  52. package/dist/lib/supertest.d.ts +14 -9
  53. package/dist/lib/supertest.js +32 -36
  54. package/dist/lib/tmp/empty.d.ts +1 -1
  55. package/dist/lib/tmp/empty.js +1 -2
  56. package/dist/lib/types.d.ts +71 -67
  57. package/dist/lib/types.js +1 -2
  58. package/dist/lib/utils.d.ts +11 -8
  59. package/dist/lib/utils.js +34 -63
  60. package/dist/register.d.ts +11 -7
  61. package/dist/register.js +36 -40
  62. package/dist/typings/index.d.ts +1 -0
  63. package/package.json +36 -40
@@ -1,69 +1,56 @@
1
- import { debuglog } from 'node:util';
2
- import ApplicationUnittest from "../app/extend/application.js";
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";
5
- import { createApp as createParallelApp } from "./parallel/app.js";
4
+ import { setupAgent } from "./agent_handler.js";
5
+ import { createApp as createApp$1 } from "./parallel/app.js";
6
6
  import { restore } from "./restore.js";
7
- import { getEggOptions } from "./utils.js";
8
- const debug = debuglog('egg/mock/lib/app_handler');
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
- export function setupApp() {
11
- let app = globalThis.__eggMockAppInstance;
12
- if (app) {
13
- debug('return exists app');
14
- return app;
15
- }
16
- const options = getEggOptions();
17
- debug('env.ENABLE_MOCHA_PARALLEL: %s, process.env.AUTO_AGENT: %s', process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT);
18
- if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
19
- // setup agent first
20
- app = createParallelApp({
21
- ...options,
22
- beforeInit: async (parallelApp) => {
23
- const agent = await setupAgent();
24
- parallelApp.options.clusterPort = agent.options.clusterPort;
25
- debug('mockParallelApp beforeInit get clusterPort: %s', parallelApp.options.clusterPort);
26
- },
27
- });
28
- debug('mockParallelApp app: %s', !!app);
29
- }
30
- else {
31
- app = createApp(options);
32
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
33
- // @ts-ignore
34
- if (typeof beforeAll === 'function') {
35
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
36
- // @ts-ignore
37
- // jest
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
- export function setGetAppCallback(cb) {
54
- getAppCallback = cb;
42
+ function setGetAppCallback(cb) {
43
+ getAppCallback = cb;
55
44
  }
56
- export async function getApp(suite, test) {
57
- if (getAppCallback) {
58
- return getAppCallback(suite, test);
59
- }
60
- const app = globalThis.__eggMockAppInstance;
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
- export function getBootstrapApp() {
67
- return globalThis.__eggMockAppInstance;
51
+ function getBootstrapApp() {
52
+ return globalThis.__eggMockAppInstance;
68
53
  }
69
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwX2hhbmRsZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvbGliL2FwcF9oYW5kbGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxRQUFRLEVBQUUsTUFBTSxXQUFXLENBQUM7QUFFckMsT0FBTyxtQkFBbUIsTUFBTSw4QkFBOEIsQ0FBQztBQUMvRCxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sb0JBQW9CLENBQUM7QUFDaEQsT0FBTyxFQUFFLFNBQVMsRUFBRSxNQUFNLFVBQVUsQ0FBQztBQUNyQyxPQUFPLEVBQUUsU0FBUyxJQUFJLGlCQUFpQixFQUFFLE1BQU0sbUJBQW1CLENBQUM7QUFDbkUsT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BQyxLQUFLLEVBQUUsSUFBSSxDQUFDLENBQUM7SUFDckMsQ0FBQztJQUNELE1BQU0sR0FBRyxHQUFHLFVBQVUsQ0FBQyxvQkFBcUIsQ0FBQztJQUM3QyxJQUFJLEdBQUcsRUFBRSxDQUFDO1FBQ1IsTUFBTSxHQUFHLENBQUMsS0FBSyxFQUFFLENBQUM7SUFDcEIsQ0FBQztJQUNELE9BQU8sR0FBRyxDQUFDO0FBQ2IsQ0FBQztBQUVELE1BQU0sVUFBVSxlQUFlO0lBQzdCLE9BQU8sVUFBVSxDQUFDLG9CQUFxQixDQUFDO0FBQzFDLENBQUMifQ==
54
+
55
+ //#endregion
56
+ export { getApp, getBootstrapApp, setGetAppCallback, setupApp };
@@ -1,123 +1,127 @@
1
- import childProcess from 'node:child_process';
2
- import { Coffee } from 'coffee';
3
- import type ApplicationUnittest from '../app/extend/application.ts';
4
- import { request as supertestRequest } from './supertest.ts';
5
- import type { MockClusterOptions, MockClusterApplicationOptions } from './types.ts';
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
- var eggMockMasterPort: number;
9
+ var eggMockMasterPort: number;
8
10
  }
9
11
  /**
10
- * A cluster version of egg.Application, you can test with supertest
11
- * @example
12
- * ```js
13
- * const mm = require('mm');
14
- * const request = require('supertest');
15
- *
16
- * describe('ClusterApplication', () => {
17
- * let app;
18
- * before(function (done) {
19
- * app = mm.cluster({ baseDir });
20
- * app.ready(done);
21
- * });
22
- *
23
- * after(function () {
24
- * app.close();
25
- * });
26
- *
27
- * it('should 200', function (done) {
28
- * request(app.callback())
29
- * .get('/')
30
- * .expect(200, done);
31
- * });
32
- * });
33
- */
34
- export declare class ClusterApplication extends Coffee {
35
- [key: string | symbol]: any;
36
- options: MockClusterApplicationOptions;
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
- baseDir: string;
39
- closed: boolean;
40
- private _address;
41
- /**
42
- * @class
43
- * @param {Object} options
44
- * - {String} baseDir - The directory of the application
45
- * - {Object} plugins - Custom you plugins
46
- * - {String} framework - The directory of the egg framework
47
- * - {Boolean} [cache=true] - Cache application based on baseDir
48
- * - {Boolean} [coverage=true] - Switch on process coverage, but it'll be slower
49
- * - {Boolean} [clean=true] - Remove $baseDir/logs
50
- * - {Object} [opt] - opt pass to coffee, such as { execArgv: ['--debug'] }
51
- * ```
52
- */
53
- constructor(options: MockClusterApplicationOptions);
54
- /**
55
- * the process that forked
56
- * @member {ChildProcess}
57
- */
58
- get process(): childProcess.ChildProcess;
59
- /**
60
- * Compatible API for supertest
61
- */
62
- callback(): this;
63
- /**
64
- * Compatible API for supertest
65
- * @member {String} url
66
- * @private
67
- */
68
- get url(): string;
69
- /**
70
- * Compatible API for supertest
71
- */
72
- address(): {
73
- port: number;
74
- address: string | undefined;
75
- };
76
- /**
77
- * Compatible API for supertest
78
- */
79
- listen(): this;
80
- /**
81
- * kill the process
82
- */
83
- close(): Promise<void>;
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
- export type MockClusterApplication = ClusterApplication & ApplicationUnittest;
122
- export declare function createCluster(initOptions?: MockClusterOptions): MockClusterApplication;
123
- export declare function restore(): Promise<void>;
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 };