@eggjs/mock 6.0.6 → 6.1.0-beta.10

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 (309) hide show
  1. package/README.md +35 -56
  2. package/README.zh_CN.md +30 -50
  3. package/dist/_virtual/rolldown_runtime.js +30 -0
  4. package/dist/app/extend/agent.d.ts +39 -0
  5. package/dist/app/extend/agent.js +48 -0
  6. package/dist/app/extend/application.d.ts +175 -0
  7. package/dist/app/extend/application.js +385 -0
  8. package/dist/app/middleware/cluster_app_mock.d.ts +6 -0
  9. package/dist/app/middleware/cluster_app_mock.js +97 -0
  10. package/dist/app.d.ts +10 -0
  11. package/dist/app.js +17 -0
  12. package/dist/bootstrap.d.ts +9 -0
  13. package/dist/bootstrap.js +14 -0
  14. package/dist/index.d.ts +81 -0
  15. package/dist/index.js +47 -0
  16. package/dist/inject_mocha.d.ts +1 -0
  17. package/dist/inject_mocha.js +35 -0
  18. package/dist/lib/agent_handler.d.ts +7 -0
  19. package/dist/lib/agent_handler.js +26 -0
  20. package/dist/lib/app.d.ts +7 -0
  21. package/dist/lib/app.js +251 -0
  22. package/dist/lib/app_handler.d.ts +9 -0
  23. package/dist/lib/app_handler.js +56 -0
  24. package/dist/lib/cluster.d.ts +126 -0
  25. package/dist/lib/cluster.js +283 -0
  26. package/dist/lib/context.d.ts +4 -0
  27. package/dist/lib/context.js +12 -0
  28. package/dist/lib/format_options.d.ts +10 -0
  29. package/dist/lib/format_options.js +65 -0
  30. package/dist/lib/inject_context.d.ts +9 -0
  31. package/dist/lib/inject_context.js +106 -0
  32. package/dist/lib/mock_agent.d.ts +9 -0
  33. package/dist/lib/mock_agent.js +45 -0
  34. package/dist/lib/mock_custom_loader.d.ts +4 -0
  35. package/dist/lib/mock_custom_loader.js +35 -0
  36. package/dist/lib/mock_http_server.d.ts +6 -0
  37. package/dist/lib/mock_http_server.js +17 -0
  38. package/dist/lib/mock_httpclient.d.ts +40 -0
  39. package/dist/lib/mock_httpclient.js +110 -0
  40. package/dist/lib/parallel/agent.d.ts +24 -0
  41. package/dist/lib/parallel/agent.js +111 -0
  42. package/dist/lib/parallel/app.d.ts +24 -0
  43. package/dist/lib/parallel/app.js +99 -0
  44. package/dist/lib/parallel/util.d.ts +6 -0
  45. package/dist/lib/parallel/util.js +59 -0
  46. package/dist/lib/prerequire.d.ts +1 -0
  47. package/dist/lib/prerequire.js +1 -0
  48. package/dist/lib/request_call_function.d.ts +1 -0
  49. package/dist/lib/request_call_function.js +37 -0
  50. package/dist/lib/restore.d.ts +4 -0
  51. package/dist/lib/restore.js +16 -0
  52. package/dist/lib/start-cluster.d.ts +1 -0
  53. package/dist/lib/start-cluster.js +19 -0
  54. package/dist/lib/supertest.d.ts +16 -0
  55. package/dist/lib/supertest.js +39 -0
  56. package/dist/lib/tmp/empty.d.ts +1 -0
  57. package/dist/lib/tmp/empty.js +1 -0
  58. package/{src/lib/types.ts → dist/lib/types.d.ts} +12 -29
  59. package/dist/lib/types.js +1 -0
  60. package/dist/lib/utils.d.ts +12 -0
  61. package/dist/lib/utils.js +40 -0
  62. package/dist/node_modules/.pnpm/ansi-styles@4.3.0/node_modules/ansi-styles/index.js +142 -0
  63. package/dist/node_modules/.pnpm/chalk@4.1.2/node_modules/chalk/source/index.js +157 -0
  64. package/dist/node_modules/.pnpm/chalk@4.1.2/node_modules/chalk/source/templates.js +97 -0
  65. package/dist/node_modules/.pnpm/chalk@4.1.2/node_modules/chalk/source/util.js +40 -0
  66. package/dist/node_modules/.pnpm/color-convert@2.0.1/node_modules/color-convert/conversions.js +814 -0
  67. package/dist/node_modules/.pnpm/color-convert@2.0.1/node_modules/color-convert/index.js +50 -0
  68. package/dist/node_modules/.pnpm/color-convert@2.0.1/node_modules/color-convert/route.js +68 -0
  69. package/dist/node_modules/.pnpm/color-name@1.1.4/node_modules/color-name/index.js +752 -0
  70. package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/browser.js +203 -0
  71. package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/common.js +209 -0
  72. package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/index.js +18 -0
  73. package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/node.js +215 -0
  74. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/convert/dmp.js +24 -0
  75. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/convert/xml.js +33 -0
  76. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/array.js +29 -0
  77. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/base.js +185 -0
  78. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/character.js +23 -0
  79. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/css.js +26 -0
  80. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/json.js +83 -0
  81. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/line.js +51 -0
  82. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/sentence.js +26 -0
  83. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/word.js +151 -0
  84. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/index.js +178 -0
  85. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/apply.js +168 -0
  86. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/create.js +301 -0
  87. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/line-endings.js +137 -0
  88. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/merge.js +367 -0
  89. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/parse.js +91 -0
  90. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/reverse.js +99 -0
  91. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/array.js +23 -0
  92. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/distance-iterator.js +30 -0
  93. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/params.js +22 -0
  94. package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/string.js +97 -0
  95. package/dist/node_modules/.pnpm/escape-string-regexp@4.0.0/node_modules/escape-string-regexp/index.js +14 -0
  96. package/dist/node_modules/.pnpm/has-flag@4.0.0/node_modules/has-flag/index.js +16 -0
  97. package/dist/node_modules/.pnpm/he@1.2.0/node_modules/he/he.js +4093 -0
  98. package/dist/node_modules/.pnpm/is-unicode-supported@0.1.0/node_modules/is-unicode-supported/index.js +14 -0
  99. package/dist/node_modules/.pnpm/log-symbols@4.1.0/node_modules/log-symbols/index.js +27 -0
  100. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/index.js +12 -0
  101. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/context.js +83 -0
  102. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/error-constants.js +35 -0
  103. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/errors.js +392 -0
  104. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/hook.js +86 -0
  105. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/bdd.js +104 -0
  106. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/common.js +99 -0
  107. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/exports.js +65 -0
  108. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/index.js +18 -0
  109. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/qunit.js +95 -0
  110. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/tdd.js +105 -0
  111. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/mocha.js +1037 -0
  112. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/mocharc.js +24 -0
  113. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/buffered-worker-pool.js +152 -0
  114. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/esm-utils.js +74 -0
  115. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/file-unloader.js +22 -0
  116. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/parallel-buffered-runner.js +329 -0
  117. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/serializer.js +314 -0
  118. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/pending.js +22 -0
  119. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/base.js +417 -0
  120. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/doc.js +79 -0
  121. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/dot.js +75 -0
  122. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/html.js +283 -0
  123. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/index.js +40 -0
  124. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/json-stream.js +90 -0
  125. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/json.js +145 -0
  126. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/landing.js +107 -0
  127. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/list.js +77 -0
  128. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/markdown.js +106 -0
  129. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/min.js +57 -0
  130. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/nyan.js +233 -0
  131. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/progress.js +92 -0
  132. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/spec.js +91 -0
  133. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/tap.js +261 -0
  134. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/xunit.js +166 -0
  135. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/runnable.js +346 -0
  136. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/runner.js +922 -0
  137. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/stats-collector.js +72 -0
  138. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/suite.js +517 -0
  139. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/test.js +105 -0
  140. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/utils.js +505 -0
  141. package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/package.js +225 -0
  142. package/dist/node_modules/.pnpm/ms@2.1.3/node_modules/ms/index.js +124 -0
  143. package/dist/node_modules/.pnpm/randombytes@2.1.0/node_modules/randombytes/index.js +11 -0
  144. package/dist/node_modules/.pnpm/serialize-javascript@6.0.2/node_modules/serialize-javascript/index.js +114 -0
  145. package/dist/node_modules/.pnpm/supports-color@7.2.0/node_modules/supports-color/index.js +76 -0
  146. package/dist/node_modules/.pnpm/supports-color@8.1.1/node_modules/supports-color/index.js +89 -0
  147. package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/Pool.js +366 -0
  148. package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/Promise.js +248 -0
  149. package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/WorkerHandler.js +419 -0
  150. package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/debug-port-allocator.js +27 -0
  151. package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/environment.js +18 -0
  152. package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/generated/embeddedWorker.js +16 -0
  153. package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/index.js +63 -0
  154. package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/transfer.js +21 -0
  155. package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/validateOptions.js +9 -0
  156. package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/worker.js +277 -0
  157. package/dist/register.d.ts +10 -0
  158. package/dist/register.js +40 -0
  159. package/dist/typings/index.d.ts +1 -0
  160. package/package.json +71 -98
  161. package/dist/commonjs/app/extend/agent.d.ts +0 -34
  162. package/dist/commonjs/app/extend/agent.js +0 -49
  163. package/dist/commonjs/app/extend/application.d.ts +0 -171
  164. package/dist/commonjs/app/extend/application.js +0 -450
  165. package/dist/commonjs/app/middleware/cluster_app_mock.d.ts +0 -3
  166. package/dist/commonjs/app/middleware/cluster_app_mock.js +0 -101
  167. package/dist/commonjs/app.d.ts +0 -6
  168. package/dist/commonjs/app.js +0 -20
  169. package/dist/commonjs/bootstrap.d.ts +0 -5
  170. package/dist/commonjs/bootstrap.js +0 -59
  171. package/dist/commonjs/index.d.ts +0 -77
  172. package/dist/commonjs/index.js +0 -114
  173. package/dist/commonjs/lib/agent_handler.d.ts +0 -3
  174. package/dist/commonjs/lib/agent_handler.js +0 -28
  175. package/dist/commonjs/lib/app.d.ts +0 -3
  176. package/dist/commonjs/lib/app.js +0 -301
  177. package/dist/commonjs/lib/app_handler.d.ts +0 -5
  178. package/dist/commonjs/lib/app_handler.js +0 -71
  179. package/dist/commonjs/lib/cluster.d.ts +0 -114
  180. package/dist/commonjs/lib/cluster.js +0 -337
  181. package/dist/commonjs/lib/context.d.ts +0 -1
  182. package/dist/commonjs/lib/context.js +0 -16
  183. package/dist/commonjs/lib/format_options.d.ts +0 -5
  184. package/dist/commonjs/lib/format_options.js +0 -100
  185. package/dist/commonjs/lib/inject_context.d.ts +0 -6
  186. package/dist/commonjs/lib/inject_context.js +0 -132
  187. package/dist/commonjs/lib/mock_agent.d.ts +0 -5
  188. package/dist/commonjs/lib/mock_agent.js +0 -49
  189. package/dist/commonjs/lib/mock_custom_loader.d.ts +0 -1
  190. package/dist/commonjs/lib/mock_custom_loader.js +0 -37
  191. package/dist/commonjs/lib/mock_http_server.d.ts +0 -2
  192. package/dist/commonjs/lib/mock_http_server.js +0 -24
  193. package/dist/commonjs/lib/mock_httpclient.d.ts +0 -36
  194. package/dist/commonjs/lib/mock_httpclient.js +0 -147
  195. package/dist/commonjs/lib/parallel/agent.d.ts +0 -20
  196. package/dist/commonjs/lib/parallel/agent.js +0 -125
  197. package/dist/commonjs/lib/parallel/app.d.ts +0 -20
  198. package/dist/commonjs/lib/parallel/app.js +0 -115
  199. package/dist/commonjs/lib/parallel/util.d.ts +0 -3
  200. package/dist/commonjs/lib/parallel/util.js +0 -77
  201. package/dist/commonjs/lib/prerequire.d.ts +0 -1
  202. package/dist/commonjs/lib/prerequire.js +0 -26
  203. package/dist/commonjs/lib/request_call_function.d.ts +0 -1
  204. package/dist/commonjs/lib/request_call_function.js +0 -52
  205. package/dist/commonjs/lib/restore.d.ts +0 -1
  206. package/dist/commonjs/lib/restore.js +0 -16
  207. package/dist/commonjs/lib/start-cluster.d.ts +0 -2
  208. package/dist/commonjs/lib/start-cluster.js +0 -23
  209. package/dist/commonjs/lib/supertest.d.ts +0 -11
  210. package/dist/commonjs/lib/supertest.js +0 -48
  211. package/dist/commonjs/lib/tmp/empty.d.ts +0 -1
  212. package/dist/commonjs/lib/tmp/empty.js +0 -3
  213. package/dist/commonjs/lib/types.d.ts +0 -68
  214. package/dist/commonjs/lib/types.js +0 -3
  215. package/dist/commonjs/lib/utils.d.ts +0 -9
  216. package/dist/commonjs/lib/utils.js +0 -80
  217. package/dist/commonjs/package.json +0 -3
  218. package/dist/commonjs/register.d.ts +0 -8
  219. package/dist/commonjs/register.js +0 -80
  220. package/dist/esm/app/extend/agent.d.ts +0 -34
  221. package/dist/esm/app/extend/agent.js +0 -46
  222. package/dist/esm/app/extend/application.d.ts +0 -171
  223. package/dist/esm/app/extend/application.js +0 -444
  224. package/dist/esm/app/middleware/cluster_app_mock.d.ts +0 -3
  225. package/dist/esm/app/middleware/cluster_app_mock.js +0 -99
  226. package/dist/esm/app.d.ts +0 -6
  227. package/dist/esm/app.js +0 -17
  228. package/dist/esm/bootstrap.d.ts +0 -5
  229. package/dist/esm/bootstrap.js +0 -15
  230. package/dist/esm/index.d.ts +0 -77
  231. package/dist/esm/index.js +0 -91
  232. package/dist/esm/lib/agent_handler.d.ts +0 -3
  233. package/dist/esm/lib/agent_handler.js +0 -24
  234. package/dist/esm/lib/app.d.ts +0 -3
  235. package/dist/esm/lib/app.js +0 -295
  236. package/dist/esm/lib/app_handler.d.ts +0 -5
  237. package/dist/esm/lib/app_handler.js +0 -65
  238. package/dist/esm/lib/cluster.d.ts +0 -114
  239. package/dist/esm/lib/cluster.js +0 -328
  240. package/dist/esm/lib/context.d.ts +0 -1
  241. package/dist/esm/lib/context.js +0 -13
  242. package/dist/esm/lib/format_options.d.ts +0 -5
  243. package/dist/esm/lib/format_options.js +0 -94
  244. package/dist/esm/lib/inject_context.d.ts +0 -6
  245. package/dist/esm/lib/inject_context.js +0 -126
  246. package/dist/esm/lib/mock_agent.d.ts +0 -5
  247. package/dist/esm/lib/mock_agent.js +0 -45
  248. package/dist/esm/lib/mock_custom_loader.d.ts +0 -1
  249. package/dist/esm/lib/mock_custom_loader.js +0 -34
  250. package/dist/esm/lib/mock_http_server.d.ts +0 -2
  251. package/dist/esm/lib/mock_http_server.js +0 -18
  252. package/dist/esm/lib/mock_httpclient.d.ts +0 -36
  253. package/dist/esm/lib/mock_httpclient.js +0 -144
  254. package/dist/esm/lib/parallel/agent.d.ts +0 -20
  255. package/dist/esm/lib/parallel/agent.js +0 -117
  256. package/dist/esm/lib/parallel/app.d.ts +0 -20
  257. package/dist/esm/lib/parallel/app.js +0 -110
  258. package/dist/esm/lib/parallel/util.d.ts +0 -3
  259. package/dist/esm/lib/parallel/util.js +0 -73
  260. package/dist/esm/lib/prerequire.d.ts +0 -1
  261. package/dist/esm/lib/prerequire.js +0 -25
  262. package/dist/esm/lib/request_call_function.d.ts +0 -1
  263. package/dist/esm/lib/request_call_function.js +0 -47
  264. package/dist/esm/lib/restore.d.ts +0 -1
  265. package/dist/esm/lib/restore.js +0 -13
  266. package/dist/esm/lib/start-cluster.d.ts +0 -2
  267. package/dist/esm/lib/start-cluster.js +0 -18
  268. package/dist/esm/lib/supertest.d.ts +0 -11
  269. package/dist/esm/lib/supertest.js +0 -40
  270. package/dist/esm/lib/tmp/empty.d.ts +0 -1
  271. package/dist/esm/lib/tmp/empty.js +0 -2
  272. package/dist/esm/lib/types.d.ts +0 -68
  273. package/dist/esm/lib/types.js +0 -2
  274. package/dist/esm/lib/utils.d.ts +0 -9
  275. package/dist/esm/lib/utils.js +0 -69
  276. package/dist/esm/package.json +0 -3
  277. package/dist/esm/register.d.ts +0 -8
  278. package/dist/esm/register.js +0 -75
  279. package/dist/package.json +0 -4
  280. package/src/app/extend/agent.ts +0 -57
  281. package/src/app/extend/application.ts +0 -510
  282. package/src/app/middleware/cluster_app_mock.ts +0 -102
  283. package/src/app.ts +0 -18
  284. package/src/bootstrap.ts +0 -25
  285. package/src/index.ts +0 -112
  286. package/src/lib/agent_handler.ts +0 -28
  287. package/src/lib/app.ts +0 -314
  288. package/src/lib/app_handler.ts +0 -77
  289. package/src/lib/cluster.ts +0 -363
  290. package/src/lib/context.ts +0 -14
  291. package/src/lib/format_options.ts +0 -103
  292. package/src/lib/inject_context.ts +0 -134
  293. package/src/lib/mock_agent.ts +0 -57
  294. package/src/lib/mock_custom_loader.ts +0 -36
  295. package/src/lib/mock_http_server.ts +0 -19
  296. package/src/lib/mock_httpclient.ts +0 -183
  297. package/src/lib/parallel/agent.ts +0 -128
  298. package/src/lib/parallel/app.ts +0 -123
  299. package/src/lib/parallel/util.ts +0 -66
  300. package/src/lib/prerequire.ts +0 -25
  301. package/src/lib/request_call_function.ts +0 -49
  302. package/src/lib/restore.ts +0 -14
  303. package/src/lib/start-cluster.ts +0 -23
  304. package/src/lib/supertest.ts +0 -45
  305. package/src/lib/tmp/.gitkeep +0 -0
  306. package/src/lib/tmp/empty.ts +0 -0
  307. package/src/lib/utils.ts +0 -82
  308. package/src/register.ts +0 -80
  309. package/src/typings/index.d.ts +0 -4
@@ -1,363 +0,0 @@
1
- import { debuglog } from 'node:util';
2
- import path from 'node:path';
3
- import os from 'node:os';
4
- import childProcess from 'node:child_process';
5
- import { once } from 'node:events';
6
- import { Coffee } from 'coffee';
7
- import { Ready } from 'get-ready';
8
- import { request as supertestRequest } from './supertest.js';
9
- import { sleep, rimrafSync, getSourceDirname } from './utils.js';
10
- import { formatOptions } from './format_options.js';
11
- import type { MockClusterOptions, MockClusterApplicationOptions } from './types.js';
12
-
13
- const debug = debuglog('@eggjs/mock/lib/cluster');
14
-
15
- const clusters = new Map();
16
- const serverBin = path.join(getSourceDirname(), 'lib/start-cluster.js');
17
- const requestCallFunctionFile = path.join(getSourceDirname(), 'lib/request_call_function.js');
18
- let masterPort = 17000;
19
-
20
- /**
21
- * A cluster version of egg.Application, you can test with supertest
22
- * @example
23
- * ```js
24
- * const mm = require('mm');
25
- * const request = require('supertest');
26
- *
27
- * describe('ClusterApplication', () => {
28
- * let app;
29
- * before(function (done) {
30
- * app = mm.cluster({ baseDir });
31
- * app.ready(done);
32
- * });
33
- *
34
- * after(function () {
35
- * app.close();
36
- * });
37
- *
38
- * it('should 200', function (done) {
39
- * request(app.callback())
40
- * .get('/')
41
- * .expect(200, done);
42
- * });
43
- * });
44
- */
45
- export class ClusterApplication extends Coffee {
46
- [key: string | symbol]: any;
47
- options: MockClusterApplicationOptions;
48
- port: number;
49
- baseDir: string;
50
- closed: boolean;
51
-
52
- /**
53
- * @class
54
- * @param {Object} options
55
- * - {String} baseDir - The directory of the application
56
- * - {Object} plugins - Custom you plugins
57
- * - {String} framework - The directory of the egg framework
58
- * - {Boolean} [cache=true] - Cache application based on baseDir
59
- * - {Boolean} [coverage=true] - Switch on process coverage, but it'll be slower
60
- * - {Boolean} [clean=true] - Remove $baseDir/logs
61
- * - {Object} [opt] - opt pass to coffee, such as { execArgv: ['--debug'] }
62
- * ```
63
- */
64
- constructor(options: MockClusterApplicationOptions) {
65
- const opt = options.opt;
66
- delete options.opt;
67
-
68
- // incremental port
69
- options.port = options.port ?? ++masterPort;
70
- // Set 1 worker when test
71
- if (!options.workers) {
72
- options.workers = 1;
73
- }
74
-
75
- const args = [ JSON.stringify(options) ];
76
- debug('fork %s, args: %s, opt: %j', serverBin, args.join(' '), opt);
77
- super({
78
- method: 'fork',
79
- cmd: serverBin,
80
- args,
81
- opt,
82
- });
83
-
84
- Ready.mixin(this);
85
-
86
- this.port = options.port;
87
- this.baseDir = options.baseDir;
88
-
89
- // print stdout and stderr when DEBUG, otherwise stderr.
90
- this.debug(process.env.DEBUG ? 0 : 2);
91
-
92
- // disable coverage
93
- if (options.coverage === false) {
94
- this.coverage(false);
95
- }
96
-
97
- process.nextTick(() => {
98
- this.proc.on('message', (msg: any) => {
99
- // 'egg-ready' and { action: 'egg-ready' }
100
- const action = msg && msg.action ? msg.action : msg;
101
- switch (action) {
102
- case 'egg-ready':
103
- this.emit('close', 0);
104
- break;
105
- case 'app-worker-died':
106
- case 'agent-worker-died':
107
- this.emit('close', 1);
108
- break;
109
- default:
110
- // ignore it
111
- break;
112
- }
113
- });
114
- });
115
-
116
- this.end(() => this.ready(true));
117
- }
118
-
119
- /**
120
- * the process that forked
121
- * @member {ChildProcess}
122
- */
123
- get process() {
124
- return this.proc;
125
- }
126
-
127
- /**
128
- * Compatible API for supertest
129
- */
130
- callback() {
131
- return this;
132
- }
133
-
134
- /**
135
- * Compatible API for supertest
136
- * @member {String} url
137
- * @private
138
- */
139
- get url() {
140
- return 'http://127.0.0.1:' + this.port;
141
- }
142
-
143
- /**
144
- * Compatible API for supertest
145
- */
146
- address() {
147
- return {
148
- port: this.port,
149
- };
150
- }
151
-
152
- /**
153
- * Compatible API for supertest
154
- */
155
- listen() {
156
- return this;
157
- }
158
-
159
- /**
160
- * kill the process
161
- */
162
- async close() {
163
- this.closed = true;
164
-
165
- const proc = this.proc;
166
- const baseDir = this.baseDir;
167
- if (proc.connected) {
168
- proc.kill('SIGTERM');
169
- await once(proc, 'exit');
170
- }
171
-
172
- clusters.delete(baseDir);
173
- debug('delete cluster cache %s, remain %s', baseDir, [ ...clusters.keys() ]);
174
-
175
- if (os.platform() === 'win32') {
176
- await sleep(1000);
177
- }
178
- }
179
-
180
- get isClosed() {
181
- return this.closed;
182
- }
183
-
184
- // mock app.router.pathFor(name) api
185
- get router() {
186
- const self = this;
187
- return {
188
- pathFor(url: string) {
189
- return self._callFunctionOnAppWorker('pathFor', [ url ], 'router', true);
190
- },
191
- };
192
- }
193
-
194
- /**
195
- * get app[property] value in app worker
196
- */
197
- getAppInstanceProperty(property: string) {
198
- return this._callFunctionOnAppWorker('__getter__', [], property, true);
199
- }
200
-
201
- /**
202
- * collection logger message, then can be use on `expectLog()`
203
- * it's different from `app.expectLog()`, only support string params.
204
- *
205
- * @param {String} [logger] - logger instance name, default is `logger`
206
- * @function ClusterApplication#expectLog
207
- */
208
- mockLog(logger?: string) {
209
- logger = logger ?? 'logger';
210
- this._callFunctionOnAppWorker('mockLog', [ logger ], null, true);
211
- }
212
-
213
- /**
214
- * expect str in the logger
215
- * it's different from `app.expectLog()`, only support string params.
216
- *
217
- * @param {String} str - test str
218
- * @param {String} [logger] - logger instance name, default is `logger`
219
- * @function ClusterApplication#expectLog
220
- */
221
- expectLog(str: string, logger?: string) {
222
- logger = logger ?? 'logger';
223
- this._callFunctionOnAppWorker('expectLog', [ str, logger ], null, true);
224
- }
225
-
226
- /**
227
- * not expect str in the logger
228
- * it's different from `app.notExpectLog()`, only support string params.
229
- *
230
- * @param {String} str - test str
231
- * @param {String} [logger] - logger instance name, default is `logger`
232
- * @function ClusterApplication#notExpectLog
233
- */
234
- notExpectLog(str: string, logger?: string) {
235
- logger = logger ?? 'logger';
236
- this._callFunctionOnAppWorker('notExpectLog', [ str, logger ], null, true);
237
- }
238
-
239
- httpRequest() {
240
- return supertestRequest(this);
241
- }
242
-
243
- _callFunctionOnAppWorker(method: string, args: any[] = [], property: any = undefined, needResult = false) {
244
- for (let i = 0; i < args.length; i++) {
245
- const arg = args[i];
246
- if (typeof arg === 'function') {
247
- args[i] = {
248
- __egg_mock_type: 'function',
249
- value: arg.toString(),
250
- };
251
- } else if (arg instanceof Error) {
252
- const errObject: any = {
253
- __egg_mock_type: 'error',
254
- name: arg.name,
255
- message: arg.message,
256
- stack: arg.stack,
257
- };
258
- for (const key in arg) {
259
- if (key !== 'name' && key !== 'message' && key !== 'stack') {
260
- errObject[key] = (arg as any)[key];
261
- }
262
- }
263
- args[i] = errObject;
264
- }
265
- }
266
- const data = {
267
- port: this.port,
268
- method,
269
- args,
270
- property,
271
- needResult,
272
- };
273
- const child = childProcess.spawnSync(process.execPath, [
274
- requestCallFunctionFile,
275
- JSON.stringify(data),
276
- ], {
277
- stdio: 'pipe',
278
- });
279
- // if (child.stderr && child.stderr.length > 0) {
280
- // console.error(child.stderr.toString());
281
- // }
282
- let result;
283
- if (child.stdout && child.stdout.length > 0) {
284
- if (needResult) {
285
- result = JSON.parse(child.stdout.toString());
286
- } else {
287
- console.error(child.stdout.toString());
288
- }
289
- }
290
-
291
- if (child.status !== 0) {
292
- throw new Error(child.stderr.toString());
293
- }
294
- if (child.error) {
295
- throw child.error;
296
- }
297
-
298
- return result;
299
- }
300
- }
301
-
302
- export function createCluster(initOptions?: MockClusterOptions) {
303
- const options = formatOptions(initOptions) as MockClusterApplicationOptions;
304
- if (options.cache && clusters.has(options.baseDir)) {
305
- const clusterApp = clusters.get(options.baseDir);
306
- // return cache when it hasn't been killed
307
- if (!clusterApp.isClosed) {
308
- return clusterApp;
309
- }
310
-
311
- // delete the cache when it's closed
312
- clusters.delete(options.baseDir);
313
- }
314
-
315
- if (options.clean !== false) {
316
- const logDir = path.join(options.baseDir, 'logs');
317
- try {
318
- rimrafSync(logDir);
319
- } catch (err: any) {
320
- console.error(`remove log dir ${logDir} failed: ${err.stack}`);
321
- }
322
- const runDir = path.join(options.baseDir, 'run');
323
- try {
324
- rimrafSync(runDir);
325
- } catch (err: any) {
326
- console.error(`remove run dir ${runDir} failed: ${err.stack}`);
327
- }
328
- }
329
-
330
- let clusterApp = new ClusterApplication(options);
331
- clusterApp = new Proxy(clusterApp, {
332
- get(target, prop) {
333
- debug('proxy handler.get %s', prop);
334
- // proxy mockXXX function to app worker
335
- const method = prop;
336
- if (typeof method === 'string' && /^mock\w+$/.test(method) && target[method] === undefined) {
337
- return function mockProxy(...args: any[]) {
338
- return target._callFunctionOnAppWorker(method, args, null, true);
339
- };
340
- }
341
- return target[prop];
342
- },
343
- });
344
-
345
- clusters.set(options.baseDir, clusterApp);
346
- return clusterApp;
347
- }
348
-
349
- // export to let mm.restore() worked
350
- export async function restore() {
351
- for (const clusterApp of clusters.values()) {
352
- // will proxy to app.mockRestore()
353
- await clusterApp.mockRestore();
354
- }
355
- }
356
-
357
- // ensure to close App process on test exit.
358
- process.on('exit', () => {
359
- for (const clusterApp of clusters.values()) {
360
- debug('on exit close clusterApp, port: %s', clusterApp.port);
361
- clusterApp.close();
362
- }
363
- });
@@ -1,14 +0,0 @@
1
- import { utils } from '@eggjs/core';
2
-
3
- export const context = {
4
- runInBackground(scope: any) {
5
- /* istanbul ignore next */
6
- const taskName = scope._name || scope.name || utils.getCalleeFromStack(true);
7
- if (taskName) {
8
- scope._name = taskName;
9
- }
10
-
11
- const promise = this._runInBackground(scope);
12
- this.app._backgroundTasks.push(promise);
13
- },
14
- } as any;
@@ -1,103 +0,0 @@
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
- import { MockOptions, MockApplicationOptions } from './types.js';
7
- import { getSourceDirname } from './utils.js';
8
-
9
- const debug = debuglog('@eggjs/mock/lib/format_options');
10
-
11
- /**
12
- * format the options
13
- */
14
- export function formatOptions(initOptions?: MockOptions) {
15
- const options = {
16
- baseDir: process.cwd(),
17
- cache: true,
18
- coverage: true,
19
- clean: true,
20
- ...initOptions,
21
- } as MockApplicationOptions;
22
-
23
- // relative path to test/fixtures
24
- // ```js
25
- // formatOptions({ baseDir: 'app' }); // baseDir => $PWD/test/fixtures/app
26
- // ```
27
- if (!path.isAbsolute(options.baseDir)) {
28
- options.baseDir = path.join(process.cwd(), 'test/fixtures', options.baseDir);
29
- }
30
-
31
- let framework = initOptions?.framework ?? initOptions?.customEgg;
32
- // test for framework
33
- if (framework === true) {
34
- framework = process.cwd();
35
- // disable plugin test when framework test
36
- options.plugin = false;
37
- } else {
38
- if (!framework) {
39
- framework = '';
40
- }
41
- // it will throw when framework is not found
42
- framework = getFrameworkPath({ framework, baseDir: options.baseDir });
43
- }
44
- options.framework = options.customEgg = framework;
45
-
46
- const plugins = options.plugins = options.plugins || {};
47
-
48
- // add self as a plugin
49
- let pluginPath = path.join(getSourceDirname(), '..');
50
- // for dist directory
51
- // convert `/eggjs/mock/dist` to `/eggjs/mock`
52
- if (pluginPath.endsWith('/dist') || pluginPath.endsWith('\\dist')) {
53
- pluginPath = path.join(pluginPath, '..');
54
- }
55
- plugins['egg-mock'] = {
56
- enable: true,
57
- path: pluginPath,
58
- };
59
-
60
- // test for plugin
61
- if (options.plugin !== false) {
62
- // add self to plugin list
63
- const pluginPath = process.cwd();
64
- const pkgPath = path.join(pluginPath, 'package.json');
65
- const pluginName = getPluginName(pkgPath);
66
- if (options.plugin && !pluginName) {
67
- throw new Error(`should set "eggPlugin" property in ${pkgPath}`);
68
- }
69
- if (pluginName) {
70
- plugins[pluginName] = {
71
- enable: true,
72
- path: pluginPath,
73
- };
74
- }
75
- }
76
-
77
- // mock HOME as baseDir, but ignore if it has been mocked
78
- const env = process.env.EGG_SERVER_ENV;
79
- if (!isMocked(process.env, 'HOME') &&
80
- (env === 'default' || env === 'test' || env === 'prod')) {
81
- mm(process.env, 'HOME', options.baseDir);
82
- }
83
-
84
- // disable cache after call mm.env(),
85
- // otherwise it will use cache and won't load again.
86
- if (process.env.EGG_MOCK_SERVER_ENV) {
87
- options.cache = false;
88
- }
89
-
90
- debug('format options: %j', options);
91
- return options;
92
- }
93
-
94
- function getPluginName(pkgPath: string): string | undefined {
95
- try {
96
- const pkg = readJSONSync(pkgPath);
97
- if (pkg.eggPlugin?.name) {
98
- return pkg.eggPlugin.name;
99
- }
100
- } catch (_) {
101
- // ignore
102
- }
103
- }
@@ -1,134 +0,0 @@
1
- import { debuglog } from 'node:util';
2
- import assert from 'node:assert';
3
- import { getApp } from './app_handler.js';
4
-
5
- const MOCHA_SUITE_APP = Symbol.for('mocha#suite#app');
6
-
7
- const debug = debuglog('@eggjs/mock/lib/inject_context');
8
-
9
- /**
10
- * Monkey patch the mocha instance with egg context.
11
- *
12
- * @param {Function} mocha - the module of mocha
13
- */
14
- export function injectContext(mocha: any) {
15
- if (mocha._injectContextLoaded) {
16
- debug('mocha already injected context, skip it');
17
- return;
18
- }
19
- const { Runner } = mocha;
20
- const runSuite = Runner.prototype.runSuite;
21
- const runTests = Runner.prototype.runTests;
22
-
23
- function getTestTitle(suite: any, test: any) {
24
- const suiteTitle = suite.root ? 'root suite' : suite.title;
25
- if (!test) {
26
- return `"${suiteTitle}"`;
27
- }
28
- return `"${suiteTitle} - ${test.title}"`;
29
- }
30
-
31
- // Inject ctx for before/after.
32
- Runner.prototype.runSuite = async function(suite: any, fn: any) {
33
- debug('run suite: %s', suite.title);
34
- let app;
35
- const self = this;
36
- try {
37
- app = await getApp(suite);
38
- debug('get app: %s', !!app);
39
- await app.ready();
40
- } catch {
41
- // 可能 app.ready 时报错,不使用失败的 app
42
- app = null;
43
- }
44
- if (!app) {
45
- // app 不存在,直接跳过,在 beforeEach 的 hook 中会报错
46
- // 确保不打乱 mocha 的顺序,防止 mocha 内部状态错误
47
- return runSuite.call(self, suite, fn);
48
- }
49
- let errSuite;
50
- try {
51
- suite.ctx[MOCHA_SUITE_APP] = app;
52
- const mockContextFun = app.mockModuleContextScope || app.mockContextScope;
53
- await mockContextFun.call(app, async function() {
54
- await new Promise<void>(resolve => {
55
- runSuite.call(self, suite, (aErrSuite: Error) => {
56
- errSuite = aErrSuite;
57
- resolve();
58
- });
59
- });
60
- });
61
- } catch (err) {
62
- // mockContext 失败后动态注册一个 beforeAll hook
63
- // 快速失败,直接阻塞后续用例
64
- suite.beforeAll('egg-mock-mock-ctx-failed', async () => {
65
- throw err;
66
- });
67
- return runSuite.call(self, suite, (aErrSuite: Error) => {
68
- return fn(aErrSuite);
69
- });
70
- }
71
- return fn(errSuite);
72
- };
73
-
74
- // Inject ctx for beforeEach/it/afterEach.
75
- // And ctx with before/after is not same as beforeEach/it/afterEach.
76
- Runner.prototype.runTests = async function(suite: any, fn: any) {
77
- const tests = suite.tests.slice();
78
- if (!tests.length) {
79
- return runTests.call(this, suite, fn);
80
- }
81
-
82
- const app = suite.ctx[MOCHA_SUITE_APP];
83
-
84
- const self = this;
85
- if (!app) {
86
- return runTests.call(self, suite, fn);
87
- }
88
-
89
- function done(errSuite?: Error) {
90
- suite.tests = tests;
91
- return fn(errSuite);
92
- }
93
-
94
- async function next(i: number) {
95
- const test = tests[i];
96
- if (!test) {
97
- return done();
98
- }
99
- suite.tests = [ test ];
100
-
101
- let app;
102
- try {
103
- app = await getApp(suite, test);
104
- assert(app, `not found app for test ${getTestTitle(suite, test)}`);
105
- await app.ready();
106
- } catch (err) {
107
- self.fail(test, err);
108
- return next(i + 1);
109
- }
110
-
111
- try {
112
- const mockContextFun = app.mockModuleContextScope || app.mockContextScope;
113
- await mockContextFun.call(app, async function() {
114
- return await new Promise<void>(resolve => {
115
- runTests.call(self, suite, () => {
116
- return resolve();
117
- });
118
- });
119
- });
120
- } catch (err) {
121
- self.fail(test, err);
122
- return next(i + 1);
123
- }
124
- return next(i + 1);
125
- }
126
- next(0).catch(err => {
127
- self.fail(suite, err);
128
- done(suite);
129
- });
130
- };
131
-
132
- mocha._injectContextLoaded = true;
133
- debug('inject context success');
134
- }
@@ -1,57 +0,0 @@
1
- import { debuglog } from 'node:util';
2
- import {
3
- MockAgent, setGlobalDispatcher, getGlobalDispatcher, Dispatcher,
4
- HttpClient,
5
- } from 'urllib';
6
-
7
- const debug = debuglog('@eggjs/mock/lib/mock_agent');
8
-
9
- declare namespace globalThis {
10
- let __mockAgent: MockAgent | null;
11
- let __globalDispatcher: Dispatcher;
12
- let __httpClientDispatchers: Map<HttpClient, Dispatcher>;
13
- }
14
-
15
- globalThis.__mockAgent = null;
16
- globalThis.__httpClientDispatchers = new Map<HttpClient, Dispatcher>();
17
-
18
- export function getMockAgent(app?: { httpClient?: HttpClient }) {
19
- debug('getMockAgent');
20
- if (!globalThis.__globalDispatcher) {
21
- globalThis.__globalDispatcher = getGlobalDispatcher();
22
- debug('create global dispatcher');
23
- }
24
- if (app?.httpClient && !globalThis.__httpClientDispatchers.has(app.httpClient)) {
25
- globalThis.__httpClientDispatchers.set(app.httpClient, app.httpClient.getDispatcher());
26
- debug('add new httpClient, size: %d', globalThis.__httpClientDispatchers.size);
27
- }
28
- if (!globalThis.__mockAgent) {
29
- globalThis.__mockAgent = new MockAgent();
30
- setGlobalDispatcher(globalThis.__mockAgent);
31
- if (typeof app?.httpClient?.setDispatcher === 'function') {
32
- app.httpClient.setDispatcher(globalThis.__mockAgent);
33
- }
34
- debug('create new mockAgent');
35
- }
36
- return globalThis.__mockAgent;
37
- }
38
-
39
- export async function restoreMockAgent() {
40
- debug('restoreMockAgent start');
41
- if (globalThis.__globalDispatcher) {
42
- setGlobalDispatcher(globalThis.__globalDispatcher);
43
- debug('restore global dispatcher');
44
- }
45
- debug('restore httpClient, size: %d', globalThis.__httpClientDispatchers.size);
46
- for (const [ httpClient, dispatcher ] of globalThis.__httpClientDispatchers) {
47
- httpClient.setDispatcher(dispatcher);
48
- }
49
- globalThis.__httpClientDispatchers.clear();
50
- if (globalThis.__mockAgent) {
51
- const agent = globalThis.__mockAgent;
52
- globalThis.__mockAgent = null;
53
- await agent.close();
54
- debug('close mockAgent');
55
- }
56
- debug('restoreMockAgent end');
57
- }
@@ -1,36 +0,0 @@
1
- import { debuglog } from 'node:util';
2
-
3
- const debug = debuglog('@eggjs/mock/lib/mock_custom_loader');
4
-
5
- export function setCustomLoader(app: any) {
6
- const customLoader = app.config.customLoader;
7
- if (!customLoader) return;
8
-
9
- for (const field of Object.keys(customLoader)) {
10
- const loaderConfig = Object.assign({}, customLoader[field]);
11
- loaderConfig.field = field;
12
- addMethod(loaderConfig);
13
- }
14
-
15
- function addMethod(loaderConfig: any) {
16
- const field = loaderConfig.field as string;
17
- const appMethodName = 'mock' + field.replace(/^[a-z]/i, s => s.toUpperCase());
18
- if (app[appMethodName]) {
19
- app.coreLogger.warn('Can\'t override app.%s', appMethodName);
20
- return;
21
- }
22
- debug('[addMethod] %s => %j', appMethodName, loaderConfig);
23
- app[appMethodName] = function(service: any, methodName: string, fn: any) {
24
- if (typeof service === 'string') {
25
- const arr = service.split('.');
26
- service = loaderConfig.inject === 'ctx' ? this[field + 'Classes'] : this[field];
27
- for (const key of arr) {
28
- service = service[key];
29
- }
30
- service = service.prototype || service;
31
- }
32
- this._mockFn(service, methodName, fn);
33
- return this;
34
- };
35
- }
36
- }