@eggjs/mock 6.0.0-beta.3

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 (159) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +547 -0
  3. package/README.zh_CN.md +512 -0
  4. package/dist/commonjs/app/extend/agent.d.ts +33 -0
  5. package/dist/commonjs/app/extend/agent.js +49 -0
  6. package/dist/commonjs/app/extend/application.d.ts +175 -0
  7. package/dist/commonjs/app/extend/application.js +448 -0
  8. package/dist/commonjs/app/middleware/cluster_app_mock.d.ts +3 -0
  9. package/dist/commonjs/app/middleware/cluster_app_mock.js +100 -0
  10. package/dist/commonjs/app.d.ts +6 -0
  11. package/dist/commonjs/app.js +20 -0
  12. package/dist/commonjs/bootstrap.d.ts +4 -0
  13. package/dist/commonjs/bootstrap.js +58 -0
  14. package/dist/commonjs/index.d.ts +76 -0
  15. package/dist/commonjs/index.js +99 -0
  16. package/dist/commonjs/index.test-d.d.ts +1 -0
  17. package/dist/commonjs/index.test-d.js +43 -0
  18. package/dist/commonjs/lib/agent_handler.d.ts +3 -0
  19. package/dist/commonjs/lib/agent_handler.js +28 -0
  20. package/dist/commonjs/lib/app.d.ts +28 -0
  21. package/dist/commonjs/lib/app.js +303 -0
  22. package/dist/commonjs/lib/app_handler.d.ts +5 -0
  23. package/dist/commonjs/lib/app_handler.js +67 -0
  24. package/dist/commonjs/lib/cluster.d.ts +114 -0
  25. package/dist/commonjs/lib/cluster.js +337 -0
  26. package/dist/commonjs/lib/context.d.ts +1 -0
  27. package/dist/commonjs/lib/context.js +16 -0
  28. package/dist/commonjs/lib/format_options.d.ts +5 -0
  29. package/dist/commonjs/lib/format_options.js +100 -0
  30. package/dist/commonjs/lib/inject_context.d.ts +6 -0
  31. package/dist/commonjs/lib/inject_context.js +132 -0
  32. package/dist/commonjs/lib/mock_agent.d.ts +5 -0
  33. package/dist/commonjs/lib/mock_agent.js +49 -0
  34. package/dist/commonjs/lib/mock_custom_loader.d.ts +1 -0
  35. package/dist/commonjs/lib/mock_custom_loader.js +37 -0
  36. package/dist/commonjs/lib/mock_http_server.d.ts +2 -0
  37. package/dist/commonjs/lib/mock_http_server.js +24 -0
  38. package/dist/commonjs/lib/mock_httpclient.d.ts +35 -0
  39. package/dist/commonjs/lib/mock_httpclient.js +147 -0
  40. package/dist/commonjs/lib/parallel/agent.d.ts +20 -0
  41. package/dist/commonjs/lib/parallel/agent.js +125 -0
  42. package/dist/commonjs/lib/parallel/app.d.ts +20 -0
  43. package/dist/commonjs/lib/parallel/app.js +115 -0
  44. package/dist/commonjs/lib/parallel/util.d.ts +3 -0
  45. package/dist/commonjs/lib/parallel/util.js +77 -0
  46. package/dist/commonjs/lib/prerequire.d.ts +1 -0
  47. package/dist/commonjs/lib/prerequire.js +26 -0
  48. package/dist/commonjs/lib/request_call_function.d.ts +1 -0
  49. package/dist/commonjs/lib/request_call_function.js +52 -0
  50. package/dist/commonjs/lib/restore.d.ts +1 -0
  51. package/dist/commonjs/lib/restore.js +16 -0
  52. package/dist/commonjs/lib/start-cluster.d.ts +2 -0
  53. package/dist/commonjs/lib/start-cluster.js +23 -0
  54. package/dist/commonjs/lib/supertest.d.ts +11 -0
  55. package/dist/commonjs/lib/supertest.js +48 -0
  56. package/dist/commonjs/lib/tmp/empty.d.ts +1 -0
  57. package/dist/commonjs/lib/tmp/empty.js +3 -0
  58. package/dist/commonjs/lib/types.d.ts +60 -0
  59. package/dist/commonjs/lib/types.js +3 -0
  60. package/dist/commonjs/lib/utils.d.ts +9 -0
  61. package/dist/commonjs/lib/utils.js +80 -0
  62. package/dist/commonjs/package.json +3 -0
  63. package/dist/commonjs/register.d.ts +8 -0
  64. package/dist/commonjs/register.js +80 -0
  65. package/dist/esm/app/extend/agent.d.ts +33 -0
  66. package/dist/esm/app/extend/agent.js +46 -0
  67. package/dist/esm/app/extend/application.d.ts +175 -0
  68. package/dist/esm/app/extend/application.js +442 -0
  69. package/dist/esm/app/middleware/cluster_app_mock.d.ts +3 -0
  70. package/dist/esm/app/middleware/cluster_app_mock.js +98 -0
  71. package/dist/esm/app.d.ts +6 -0
  72. package/dist/esm/app.js +17 -0
  73. package/dist/esm/bootstrap.d.ts +4 -0
  74. package/dist/esm/bootstrap.js +16 -0
  75. package/dist/esm/index.d.ts +76 -0
  76. package/dist/esm/index.js +90 -0
  77. package/dist/esm/index.test-d.d.ts +1 -0
  78. package/dist/esm/index.test-d.js +42 -0
  79. package/dist/esm/lib/agent_handler.d.ts +3 -0
  80. package/dist/esm/lib/agent_handler.js +24 -0
  81. package/dist/esm/lib/app.d.ts +28 -0
  82. package/dist/esm/lib/app.js +295 -0
  83. package/dist/esm/lib/app_handler.d.ts +5 -0
  84. package/dist/esm/lib/app_handler.js +61 -0
  85. package/dist/esm/lib/cluster.d.ts +114 -0
  86. package/dist/esm/lib/cluster.js +328 -0
  87. package/dist/esm/lib/context.d.ts +1 -0
  88. package/dist/esm/lib/context.js +13 -0
  89. package/dist/esm/lib/format_options.d.ts +5 -0
  90. package/dist/esm/lib/format_options.js +94 -0
  91. package/dist/esm/lib/inject_context.d.ts +6 -0
  92. package/dist/esm/lib/inject_context.js +126 -0
  93. package/dist/esm/lib/mock_agent.d.ts +5 -0
  94. package/dist/esm/lib/mock_agent.js +45 -0
  95. package/dist/esm/lib/mock_custom_loader.d.ts +1 -0
  96. package/dist/esm/lib/mock_custom_loader.js +34 -0
  97. package/dist/esm/lib/mock_http_server.d.ts +2 -0
  98. package/dist/esm/lib/mock_http_server.js +18 -0
  99. package/dist/esm/lib/mock_httpclient.d.ts +35 -0
  100. package/dist/esm/lib/mock_httpclient.js +144 -0
  101. package/dist/esm/lib/parallel/agent.d.ts +20 -0
  102. package/dist/esm/lib/parallel/agent.js +117 -0
  103. package/dist/esm/lib/parallel/app.d.ts +20 -0
  104. package/dist/esm/lib/parallel/app.js +110 -0
  105. package/dist/esm/lib/parallel/util.d.ts +3 -0
  106. package/dist/esm/lib/parallel/util.js +73 -0
  107. package/dist/esm/lib/prerequire.d.ts +1 -0
  108. package/dist/esm/lib/prerequire.js +25 -0
  109. package/dist/esm/lib/request_call_function.d.ts +1 -0
  110. package/dist/esm/lib/request_call_function.js +47 -0
  111. package/dist/esm/lib/restore.d.ts +1 -0
  112. package/dist/esm/lib/restore.js +13 -0
  113. package/dist/esm/lib/start-cluster.d.ts +2 -0
  114. package/dist/esm/lib/start-cluster.js +18 -0
  115. package/dist/esm/lib/supertest.d.ts +11 -0
  116. package/dist/esm/lib/supertest.js +40 -0
  117. package/dist/esm/lib/tmp/empty.d.ts +1 -0
  118. package/dist/esm/lib/tmp/empty.js +2 -0
  119. package/dist/esm/lib/types.d.ts +60 -0
  120. package/dist/esm/lib/types.js +2 -0
  121. package/dist/esm/lib/utils.d.ts +9 -0
  122. package/dist/esm/lib/utils.js +69 -0
  123. package/dist/esm/package.json +3 -0
  124. package/dist/esm/register.d.ts +8 -0
  125. package/dist/esm/register.js +75 -0
  126. package/dist/package.json +4 -0
  127. package/package.json +131 -0
  128. package/src/app/extend/agent.ts +56 -0
  129. package/src/app/extend/application.ts +512 -0
  130. package/src/app/middleware/cluster_app_mock.ts +101 -0
  131. package/src/app.ts +18 -0
  132. package/src/bootstrap.ts +25 -0
  133. package/src/index.d.ts +193 -0
  134. package/src/index.test-d.ts +47 -0
  135. package/src/index.ts +110 -0
  136. package/src/lib/agent_handler.ts +28 -0
  137. package/src/lib/app.ts +313 -0
  138. package/src/lib/app_handler.ts +69 -0
  139. package/src/lib/cluster.ts +363 -0
  140. package/src/lib/context.ts +14 -0
  141. package/src/lib/format_options.ts +103 -0
  142. package/src/lib/inject_context.ts +134 -0
  143. package/src/lib/mock_agent.ts +57 -0
  144. package/src/lib/mock_custom_loader.ts +36 -0
  145. package/src/lib/mock_http_server.ts +19 -0
  146. package/src/lib/mock_httpclient.ts +181 -0
  147. package/src/lib/parallel/agent.ts +128 -0
  148. package/src/lib/parallel/app.ts +123 -0
  149. package/src/lib/parallel/util.ts +66 -0
  150. package/src/lib/prerequire.ts +25 -0
  151. package/src/lib/request_call_function.ts +49 -0
  152. package/src/lib/restore.ts +14 -0
  153. package/src/lib/start-cluster.ts +23 -0
  154. package/src/lib/supertest.ts +45 -0
  155. package/src/lib/tmp/.gitkeep +0 -0
  156. package/src/lib/tmp/empty.ts +0 -0
  157. package/src/lib/types.ts +72 -0
  158. package/src/lib/utils.ts +82 -0
  159. package/src/register.ts +80 -0
@@ -0,0 +1,14 @@
1
+ import { debuglog } from 'node:util';
2
+ import { restore as mmRestore } from 'mm';
3
+ import { restoreMockAgent } from './mock_agent.js';
4
+ import { restore as clusterRestore } from './cluster.js';
5
+
6
+ const debug = debuglog('@eggjs/mock/lib/restore');
7
+
8
+ export async function restore() {
9
+ // keep mm.restore execute in the current event loop
10
+ mmRestore();
11
+ await clusterRestore();
12
+ await restoreMockAgent();
13
+ debug('restore all');
14
+ }
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env node
2
+
3
+ import assert from 'node:assert';
4
+ import { debuglog } from 'node:util';
5
+ import { importModule } from '@eggjs/utils';
6
+ import { isAsyncFunction } from 'is-type-of';
7
+
8
+ const debug = debuglog('@eggjs/mock/lib/start-cluster');
9
+
10
+ // if (process.env.EGG_BIN_PREREQUIRE) {
11
+ // require('./prerequire');
12
+ // }
13
+
14
+ async function main() {
15
+ const options = JSON.parse(process.argv[2]);
16
+ debug('startCluster with options: %o', options);
17
+ const { startCluster } = await importModule(options.framework);
18
+ assert(isAsyncFunction(startCluster),
19
+ `framework(${options.framework}) should export startCluster as an async function`);
20
+ await startCluster(options);
21
+ }
22
+
23
+ main();
@@ -0,0 +1,45 @@
1
+ import path from 'node:path';
2
+ import { readJSONSync } from 'utility';
3
+ import { Request, Test } from '@eggjs/supertest';
4
+ import { createServer } from './mock_http_server.js';
5
+ import { getSourceDirname } from './utils.js';
6
+
7
+ // patch from https://github.com/visionmedia/supertest/blob/199506d8dbfe0bb1434fc07c38cdcd1ab4c7c926/index.js#L19
8
+
9
+ let pkgVersion = '';
10
+
11
+ /**
12
+ * Test against the given `app`,
13
+ * returning a new `Test`.
14
+ */
15
+ export class EggTestRequest extends Request {
16
+ #app: any;
17
+
18
+ constructor(app: any) {
19
+ super(createServer(app));
20
+ this.#app = app;
21
+ }
22
+
23
+ protected _testRequest(method: string, url: string): Test {
24
+ // support pathFor(url)
25
+ if (url[0] !== '/') {
26
+ const realUrl = this.#app.router.pathFor(url);
27
+ if (!realUrl) {
28
+ throw new Error(`Can\'t find router:${url}, please check your \'app/router.js\'`);
29
+ }
30
+ url = realUrl;
31
+ }
32
+ const test = super._testRequest(method, url);
33
+ if (!pkgVersion) {
34
+ const pkgFile = path.join(getSourceDirname(), '../package.json');
35
+ const pkg = readJSONSync(pkgFile);
36
+ pkgVersion = pkg.version;
37
+ }
38
+ test.set('User-Agent', `@eggjs/mock/${pkgVersion} Node.js/${process.version}`);
39
+ return test;
40
+ }
41
+ }
42
+
43
+ export function request(app: any) {
44
+ return new EggTestRequest(app);
45
+ }
File without changes
File without changes
@@ -0,0 +1,72 @@
1
+ export interface MockOptions {
2
+ /**
3
+ * The directory of the application
4
+ */
5
+ baseDir?: string;
6
+
7
+ /**
8
+ * Custom you plugins
9
+ */
10
+ plugins?: any;
11
+
12
+ /**
13
+ * The directory of the egg framework
14
+ *
15
+ * Set to `true` to use the current directory as framework directory
16
+ */
17
+ framework?: string | boolean;
18
+
19
+ /**
20
+ * current test on plugin
21
+ */
22
+ plugin?: boolean;
23
+
24
+ /**
25
+ * @deprecated please use framework instead
26
+ */
27
+ customEgg?: string | boolean;
28
+
29
+ /**
30
+ * Cache application based on baseDir
31
+ */
32
+ cache?: boolean;
33
+
34
+ /**
35
+ * Switch on process coverage, but it'll be slower
36
+ */
37
+ coverage?: boolean;
38
+
39
+ /**
40
+ * Remove $baseDir/logs and $baseDir/run before start, default is `true`
41
+ */
42
+ clean?: boolean;
43
+
44
+ /**
45
+ * default options.mockCtxStorage value on each mockContext
46
+ */
47
+ mockCtxStorage?: boolean;
48
+
49
+ beforeInit?: (app: any) => Promise<void>;
50
+ }
51
+
52
+ export interface MockClusterOptions extends MockOptions {
53
+ workers?: number | string;
54
+ cache?: boolean;
55
+ port?: number;
56
+ /**
57
+ * opt pass to coffee, such as { execArgv: ['--debug'] }
58
+ */
59
+ opt?: object;
60
+ }
61
+
62
+ export interface MockApplicationOptions extends MockOptions {
63
+ baseDir: string;
64
+ framework: string;
65
+ clusterPort?: number;
66
+ }
67
+
68
+ export interface MockClusterApplicationOptions extends MockClusterOptions {
69
+ baseDir: string;
70
+ framework: string;
71
+ port: number;
72
+ }
@@ -0,0 +1,82 @@
1
+ import { rm } from 'node:fs/promises';
2
+ import { rmSync } from 'node:fs';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { scheduler } from 'node:timers/promises';
6
+
7
+ export function getSourceDirname() {
8
+ if (typeof __dirname !== 'undefined') {
9
+ return path.dirname(__dirname);
10
+ }
11
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
12
+ // @ts-ignore
13
+ return path.dirname(path.dirname(fileURLToPath(import.meta.url)));
14
+ }
15
+
16
+ export async function sleep(delay: number) {
17
+ await scheduler.wait(delay);
18
+ }
19
+
20
+ export async function rimraf(filepath: string) {
21
+ await rm(filepath, { force: true, recursive: true });
22
+ }
23
+
24
+ export function rimrafSync(filepath: string) {
25
+ rmSync(filepath, { force: true, recursive: true });
26
+ }
27
+
28
+ export function getProperty(target: any, prop: PropertyKey) {
29
+ const member = target[prop];
30
+ if (typeof member === 'function') {
31
+ return member.bind(target);
32
+ }
33
+ return member;
34
+ }
35
+
36
+ export function getEggOptions() {
37
+ const options = {
38
+ baseDir: process.env.EGG_BASE_DIR ?? process.cwd(),
39
+ framework: process.env.EGG_FRAMEWORK,
40
+ };
41
+ return options;
42
+ }
43
+
44
+ // const hasOwnProperty = Object.prototype.hasOwnProperty;
45
+
46
+ // /**
47
+ // * Merge the property descriptors of `src` into `dest`
48
+ // *
49
+ // * @param {object} dest Object to add descriptors to
50
+ // * @param {object} src Object to clone descriptors from
51
+ // * @param {boolean} [redefine=true] Redefine `dest` properties with `src` properties
52
+ // * @return {object} Reference to dest
53
+ // * @public
54
+ // */
55
+
56
+ // function merge(dest, src, redefine) {
57
+ // if (!dest) {
58
+ // throw new TypeError('argument dest is required');
59
+ // }
60
+
61
+ // if (!src) {
62
+ // throw new TypeError('argument src is required');
63
+ // }
64
+
65
+ // if (redefine === undefined) {
66
+ // // Default to true
67
+ // redefine = true;
68
+ // }
69
+
70
+ // Object.getOwnPropertyNames(src).forEach(function forEachOwnPropertyName(name) {
71
+ // if (!redefine && hasOwnProperty.call(dest, name)) {
72
+ // // Skip descriptor
73
+ // return;
74
+ // }
75
+
76
+ // // Copy descriptor
77
+ // const descriptor = Object.getOwnPropertyDescriptor(src, name);
78
+ // Object.defineProperty(dest, name, descriptor);
79
+ // });
80
+
81
+ // return dest;
82
+ // }
@@ -0,0 +1,80 @@
1
+ import { debuglog } from 'node:util';
2
+ import { createRequire } from 'node:module';
3
+ import { mock } from './index.js';
4
+ import { setupAgent, closeAgent } from './lib/agent_handler.js';
5
+ import { getApp } from './lib/app_handler.js';
6
+ import { injectContext } from './lib/inject_context.js';
7
+
8
+ const debug = debuglog('@eggjs/mock/register');
9
+
10
+ export async function mochaGlobalSetup() {
11
+ debug('mochaGlobalSetup, agent.setupAgent() start');
12
+ await setupAgent();
13
+ debug('mochaGlobalSetup, agent.setupAgent() end');
14
+ }
15
+
16
+ export async function mochaGlobalTeardown() {
17
+ debug('mochaGlobalTeardown, agent.closeAgent() start');
18
+ await closeAgent();
19
+ debug('mochaGlobalTeardown, agent.closeAgent() end');
20
+ }
21
+
22
+ export const mochaHooks = {
23
+ async beforeAll() {
24
+ const app = await getApp();
25
+ debug('mochaHooks.beforeAll call, _app: %s', app);
26
+ if (app) {
27
+ await app.ready();
28
+ }
29
+ },
30
+ async afterEach() {
31
+ const app = await getApp();
32
+ debug('mochaHooks.afterEach call, _app: %s', app);
33
+ if (app) {
34
+ await app.backgroundTasksFinished();
35
+ }
36
+ await mock.restore();
37
+ },
38
+ async afterAll() {
39
+ // skip auto app close on parallel
40
+ if (process.env.ENABLE_MOCHA_PARALLEL) return;
41
+ const app = await getApp();
42
+ debug('mochaHooks.afterAll call, _app: %s', app);
43
+ if (app) {
44
+ await app.close();
45
+ }
46
+ },
47
+ };
48
+
49
+ /**
50
+ * Find active node mocha instances.
51
+ */
52
+ function findNodeJSMocha() {
53
+ let children: any;
54
+ if (typeof require === 'function') {
55
+ children = require.cache || {};
56
+ } else {
57
+ // FIXME: not work on ESM
58
+ children = createRequire(process.cwd()).cache || {};
59
+ debug('createRequire on esm');
60
+ }
61
+
62
+ return Object.keys(children)
63
+ .filter(function(child) {
64
+ const val = children[child].exports;
65
+ return typeof val === 'function' && val.name === 'Mocha';
66
+ })
67
+ .map(function(child) {
68
+ return children[child].exports;
69
+ });
70
+ }
71
+
72
+ import 'mocha';
73
+
74
+ const modules = findNodeJSMocha();
75
+ // console.error('modules length: %s', modules.length);
76
+
77
+ for (const module of modules) {
78
+ if (!module) continue;
79
+ injectContext(module);
80
+ }