@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.
- package/README.md +35 -56
- package/README.zh_CN.md +30 -50
- package/dist/_virtual/rolldown_runtime.js +30 -0
- package/dist/app/extend/agent.d.ts +39 -0
- package/dist/app/extend/agent.js +48 -0
- package/dist/app/extend/application.d.ts +175 -0
- package/dist/app/extend/application.js +385 -0
- package/dist/app/middleware/cluster_app_mock.d.ts +6 -0
- package/dist/app/middleware/cluster_app_mock.js +97 -0
- package/dist/app.d.ts +10 -0
- package/dist/app.js +17 -0
- package/dist/bootstrap.d.ts +9 -0
- package/dist/bootstrap.js +14 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.js +47 -0
- package/dist/inject_mocha.d.ts +1 -0
- package/dist/inject_mocha.js +35 -0
- package/dist/lib/agent_handler.d.ts +7 -0
- package/dist/lib/agent_handler.js +26 -0
- package/dist/lib/app.d.ts +7 -0
- package/dist/lib/app.js +251 -0
- package/dist/lib/app_handler.d.ts +9 -0
- package/dist/lib/app_handler.js +56 -0
- package/dist/lib/cluster.d.ts +126 -0
- package/dist/lib/cluster.js +283 -0
- package/dist/lib/context.d.ts +4 -0
- package/dist/lib/context.js +12 -0
- package/dist/lib/format_options.d.ts +10 -0
- package/dist/lib/format_options.js +65 -0
- package/dist/lib/inject_context.d.ts +9 -0
- package/dist/lib/inject_context.js +106 -0
- package/dist/lib/mock_agent.d.ts +9 -0
- package/dist/lib/mock_agent.js +45 -0
- package/dist/lib/mock_custom_loader.d.ts +4 -0
- package/dist/lib/mock_custom_loader.js +35 -0
- package/dist/lib/mock_http_server.d.ts +6 -0
- package/dist/lib/mock_http_server.js +17 -0
- package/dist/lib/mock_httpclient.d.ts +40 -0
- package/dist/lib/mock_httpclient.js +110 -0
- package/dist/lib/parallel/agent.d.ts +24 -0
- package/dist/lib/parallel/agent.js +111 -0
- package/dist/lib/parallel/app.d.ts +24 -0
- package/dist/lib/parallel/app.js +99 -0
- package/dist/lib/parallel/util.d.ts +6 -0
- package/dist/lib/parallel/util.js +59 -0
- package/dist/lib/prerequire.d.ts +1 -0
- package/dist/lib/prerequire.js +1 -0
- package/dist/lib/request_call_function.d.ts +1 -0
- package/dist/lib/request_call_function.js +37 -0
- package/dist/lib/restore.d.ts +4 -0
- package/dist/lib/restore.js +16 -0
- package/dist/lib/start-cluster.d.ts +1 -0
- package/dist/lib/start-cluster.js +19 -0
- package/dist/lib/supertest.d.ts +16 -0
- package/dist/lib/supertest.js +39 -0
- package/dist/lib/tmp/empty.d.ts +1 -0
- package/dist/lib/tmp/empty.js +1 -0
- package/{src/lib/types.ts → dist/lib/types.d.ts} +12 -29
- package/dist/lib/types.js +1 -0
- package/dist/lib/utils.d.ts +12 -0
- package/dist/lib/utils.js +40 -0
- package/dist/node_modules/.pnpm/ansi-styles@4.3.0/node_modules/ansi-styles/index.js +142 -0
- package/dist/node_modules/.pnpm/chalk@4.1.2/node_modules/chalk/source/index.js +157 -0
- package/dist/node_modules/.pnpm/chalk@4.1.2/node_modules/chalk/source/templates.js +97 -0
- package/dist/node_modules/.pnpm/chalk@4.1.2/node_modules/chalk/source/util.js +40 -0
- package/dist/node_modules/.pnpm/color-convert@2.0.1/node_modules/color-convert/conversions.js +814 -0
- package/dist/node_modules/.pnpm/color-convert@2.0.1/node_modules/color-convert/index.js +50 -0
- package/dist/node_modules/.pnpm/color-convert@2.0.1/node_modules/color-convert/route.js +68 -0
- package/dist/node_modules/.pnpm/color-name@1.1.4/node_modules/color-name/index.js +752 -0
- package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/browser.js +203 -0
- package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/common.js +209 -0
- package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/index.js +18 -0
- package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/node.js +215 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/convert/dmp.js +24 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/convert/xml.js +33 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/array.js +29 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/base.js +185 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/character.js +23 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/css.js +26 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/json.js +83 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/line.js +51 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/sentence.js +26 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/word.js +151 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/index.js +178 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/apply.js +168 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/create.js +301 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/line-endings.js +137 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/merge.js +367 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/parse.js +91 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/reverse.js +99 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/array.js +23 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/distance-iterator.js +30 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/params.js +22 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/string.js +97 -0
- package/dist/node_modules/.pnpm/escape-string-regexp@4.0.0/node_modules/escape-string-regexp/index.js +14 -0
- package/dist/node_modules/.pnpm/has-flag@4.0.0/node_modules/has-flag/index.js +16 -0
- package/dist/node_modules/.pnpm/he@1.2.0/node_modules/he/he.js +4093 -0
- package/dist/node_modules/.pnpm/is-unicode-supported@0.1.0/node_modules/is-unicode-supported/index.js +14 -0
- package/dist/node_modules/.pnpm/log-symbols@4.1.0/node_modules/log-symbols/index.js +27 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/index.js +12 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/context.js +83 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/error-constants.js +35 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/errors.js +392 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/hook.js +86 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/bdd.js +104 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/common.js +99 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/exports.js +65 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/index.js +18 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/qunit.js +95 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/tdd.js +105 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/mocha.js +1037 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/mocharc.js +24 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/buffered-worker-pool.js +152 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/esm-utils.js +74 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/file-unloader.js +22 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/parallel-buffered-runner.js +329 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/serializer.js +314 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/pending.js +22 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/base.js +417 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/doc.js +79 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/dot.js +75 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/html.js +283 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/index.js +40 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/json-stream.js +90 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/json.js +145 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/landing.js +107 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/list.js +77 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/markdown.js +106 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/min.js +57 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/nyan.js +233 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/progress.js +92 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/spec.js +91 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/tap.js +261 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/xunit.js +166 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/runnable.js +346 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/runner.js +922 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/stats-collector.js +72 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/suite.js +517 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/test.js +105 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/utils.js +505 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/package.js +225 -0
- package/dist/node_modules/.pnpm/ms@2.1.3/node_modules/ms/index.js +124 -0
- package/dist/node_modules/.pnpm/randombytes@2.1.0/node_modules/randombytes/index.js +11 -0
- package/dist/node_modules/.pnpm/serialize-javascript@6.0.2/node_modules/serialize-javascript/index.js +114 -0
- package/dist/node_modules/.pnpm/supports-color@7.2.0/node_modules/supports-color/index.js +76 -0
- package/dist/node_modules/.pnpm/supports-color@8.1.1/node_modules/supports-color/index.js +89 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/Pool.js +366 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/Promise.js +248 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/WorkerHandler.js +419 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/debug-port-allocator.js +27 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/environment.js +18 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/generated/embeddedWorker.js +16 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/index.js +63 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/transfer.js +21 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/validateOptions.js +9 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/worker.js +277 -0
- package/dist/register.d.ts +10 -0
- package/dist/register.js +40 -0
- package/dist/typings/index.d.ts +1 -0
- package/package.json +71 -98
- package/dist/commonjs/app/extend/agent.d.ts +0 -34
- package/dist/commonjs/app/extend/agent.js +0 -49
- package/dist/commonjs/app/extend/application.d.ts +0 -171
- package/dist/commonjs/app/extend/application.js +0 -450
- package/dist/commonjs/app/middleware/cluster_app_mock.d.ts +0 -3
- package/dist/commonjs/app/middleware/cluster_app_mock.js +0 -101
- package/dist/commonjs/app.d.ts +0 -6
- package/dist/commonjs/app.js +0 -20
- package/dist/commonjs/bootstrap.d.ts +0 -5
- package/dist/commonjs/bootstrap.js +0 -59
- package/dist/commonjs/index.d.ts +0 -77
- package/dist/commonjs/index.js +0 -114
- package/dist/commonjs/lib/agent_handler.d.ts +0 -3
- package/dist/commonjs/lib/agent_handler.js +0 -28
- package/dist/commonjs/lib/app.d.ts +0 -3
- package/dist/commonjs/lib/app.js +0 -301
- package/dist/commonjs/lib/app_handler.d.ts +0 -5
- package/dist/commonjs/lib/app_handler.js +0 -71
- package/dist/commonjs/lib/cluster.d.ts +0 -114
- package/dist/commonjs/lib/cluster.js +0 -337
- package/dist/commonjs/lib/context.d.ts +0 -1
- package/dist/commonjs/lib/context.js +0 -16
- package/dist/commonjs/lib/format_options.d.ts +0 -5
- package/dist/commonjs/lib/format_options.js +0 -100
- package/dist/commonjs/lib/inject_context.d.ts +0 -6
- package/dist/commonjs/lib/inject_context.js +0 -132
- package/dist/commonjs/lib/mock_agent.d.ts +0 -5
- package/dist/commonjs/lib/mock_agent.js +0 -49
- package/dist/commonjs/lib/mock_custom_loader.d.ts +0 -1
- package/dist/commonjs/lib/mock_custom_loader.js +0 -37
- package/dist/commonjs/lib/mock_http_server.d.ts +0 -2
- package/dist/commonjs/lib/mock_http_server.js +0 -24
- package/dist/commonjs/lib/mock_httpclient.d.ts +0 -36
- package/dist/commonjs/lib/mock_httpclient.js +0 -147
- package/dist/commonjs/lib/parallel/agent.d.ts +0 -20
- package/dist/commonjs/lib/parallel/agent.js +0 -125
- package/dist/commonjs/lib/parallel/app.d.ts +0 -20
- package/dist/commonjs/lib/parallel/app.js +0 -115
- package/dist/commonjs/lib/parallel/util.d.ts +0 -3
- package/dist/commonjs/lib/parallel/util.js +0 -77
- package/dist/commonjs/lib/prerequire.d.ts +0 -1
- package/dist/commonjs/lib/prerequire.js +0 -26
- package/dist/commonjs/lib/request_call_function.d.ts +0 -1
- package/dist/commonjs/lib/request_call_function.js +0 -52
- package/dist/commonjs/lib/restore.d.ts +0 -1
- package/dist/commonjs/lib/restore.js +0 -16
- package/dist/commonjs/lib/start-cluster.d.ts +0 -2
- package/dist/commonjs/lib/start-cluster.js +0 -23
- package/dist/commonjs/lib/supertest.d.ts +0 -11
- package/dist/commonjs/lib/supertest.js +0 -48
- package/dist/commonjs/lib/tmp/empty.d.ts +0 -1
- package/dist/commonjs/lib/tmp/empty.js +0 -3
- package/dist/commonjs/lib/types.d.ts +0 -68
- package/dist/commonjs/lib/types.js +0 -3
- package/dist/commonjs/lib/utils.d.ts +0 -9
- package/dist/commonjs/lib/utils.js +0 -80
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/register.d.ts +0 -8
- package/dist/commonjs/register.js +0 -80
- package/dist/esm/app/extend/agent.d.ts +0 -34
- package/dist/esm/app/extend/agent.js +0 -46
- package/dist/esm/app/extend/application.d.ts +0 -171
- package/dist/esm/app/extend/application.js +0 -444
- package/dist/esm/app/middleware/cluster_app_mock.d.ts +0 -3
- package/dist/esm/app/middleware/cluster_app_mock.js +0 -99
- package/dist/esm/app.d.ts +0 -6
- package/dist/esm/app.js +0 -17
- package/dist/esm/bootstrap.d.ts +0 -5
- package/dist/esm/bootstrap.js +0 -15
- package/dist/esm/index.d.ts +0 -77
- package/dist/esm/index.js +0 -91
- package/dist/esm/lib/agent_handler.d.ts +0 -3
- package/dist/esm/lib/agent_handler.js +0 -24
- package/dist/esm/lib/app.d.ts +0 -3
- package/dist/esm/lib/app.js +0 -295
- package/dist/esm/lib/app_handler.d.ts +0 -5
- package/dist/esm/lib/app_handler.js +0 -65
- package/dist/esm/lib/cluster.d.ts +0 -114
- package/dist/esm/lib/cluster.js +0 -328
- package/dist/esm/lib/context.d.ts +0 -1
- package/dist/esm/lib/context.js +0 -13
- package/dist/esm/lib/format_options.d.ts +0 -5
- package/dist/esm/lib/format_options.js +0 -94
- package/dist/esm/lib/inject_context.d.ts +0 -6
- package/dist/esm/lib/inject_context.js +0 -126
- package/dist/esm/lib/mock_agent.d.ts +0 -5
- package/dist/esm/lib/mock_agent.js +0 -45
- package/dist/esm/lib/mock_custom_loader.d.ts +0 -1
- package/dist/esm/lib/mock_custom_loader.js +0 -34
- package/dist/esm/lib/mock_http_server.d.ts +0 -2
- package/dist/esm/lib/mock_http_server.js +0 -18
- package/dist/esm/lib/mock_httpclient.d.ts +0 -36
- package/dist/esm/lib/mock_httpclient.js +0 -144
- package/dist/esm/lib/parallel/agent.d.ts +0 -20
- package/dist/esm/lib/parallel/agent.js +0 -117
- package/dist/esm/lib/parallel/app.d.ts +0 -20
- package/dist/esm/lib/parallel/app.js +0 -110
- package/dist/esm/lib/parallel/util.d.ts +0 -3
- package/dist/esm/lib/parallel/util.js +0 -73
- package/dist/esm/lib/prerequire.d.ts +0 -1
- package/dist/esm/lib/prerequire.js +0 -25
- package/dist/esm/lib/request_call_function.d.ts +0 -1
- package/dist/esm/lib/request_call_function.js +0 -47
- package/dist/esm/lib/restore.d.ts +0 -1
- package/dist/esm/lib/restore.js +0 -13
- package/dist/esm/lib/start-cluster.d.ts +0 -2
- package/dist/esm/lib/start-cluster.js +0 -18
- package/dist/esm/lib/supertest.d.ts +0 -11
- package/dist/esm/lib/supertest.js +0 -40
- package/dist/esm/lib/tmp/empty.d.ts +0 -1
- package/dist/esm/lib/tmp/empty.js +0 -2
- package/dist/esm/lib/types.d.ts +0 -68
- package/dist/esm/lib/types.js +0 -2
- package/dist/esm/lib/utils.d.ts +0 -9
- package/dist/esm/lib/utils.js +0 -69
- package/dist/esm/package.json +0 -3
- package/dist/esm/register.d.ts +0 -8
- package/dist/esm/register.js +0 -75
- package/dist/package.json +0 -4
- package/src/app/extend/agent.ts +0 -57
- package/src/app/extend/application.ts +0 -510
- package/src/app/middleware/cluster_app_mock.ts +0 -102
- package/src/app.ts +0 -18
- package/src/bootstrap.ts +0 -25
- package/src/index.ts +0 -112
- package/src/lib/agent_handler.ts +0 -28
- package/src/lib/app.ts +0 -314
- package/src/lib/app_handler.ts +0 -77
- package/src/lib/cluster.ts +0 -363
- package/src/lib/context.ts +0 -14
- package/src/lib/format_options.ts +0 -103
- package/src/lib/inject_context.ts +0 -134
- package/src/lib/mock_agent.ts +0 -57
- package/src/lib/mock_custom_loader.ts +0 -36
- package/src/lib/mock_http_server.ts +0 -19
- package/src/lib/mock_httpclient.ts +0 -183
- package/src/lib/parallel/agent.ts +0 -128
- package/src/lib/parallel/app.ts +0 -123
- package/src/lib/parallel/util.ts +0 -66
- package/src/lib/prerequire.ts +0 -25
- package/src/lib/request_call_function.ts +0 -49
- package/src/lib/restore.ts +0 -14
- package/src/lib/start-cluster.ts +0 -23
- package/src/lib/supertest.ts +0 -45
- package/src/lib/tmp/.gitkeep +0 -0
- package/src/lib/tmp/empty.ts +0 -0
- package/src/lib/utils.ts +0 -82
- package/src/register.ts +0 -80
- package/src/typings/index.d.ts +0 -4
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { MockHttpClientMethod, MockResultFunction, MockResultOptions } from "../../lib/mock_httpclient.js";
|
|
2
|
+
import { EggTestRequest } from "../../lib/supertest.js";
|
|
3
|
+
import { MockOptions } from "../../lib/types.js";
|
|
4
|
+
import { Application, Context } from "egg";
|
|
5
|
+
import { mock } from "mm";
|
|
6
|
+
import { IncomingMessage } from "node:http";
|
|
7
|
+
import { Logger } from "egg-logger";
|
|
8
|
+
import { MockAgent } from "urllib";
|
|
9
|
+
|
|
10
|
+
//#region src/app/extend/application.d.ts
|
|
11
|
+
interface MockContextOptions {
|
|
12
|
+
/**
|
|
13
|
+
* mock ctxStorage or not, default is `true`
|
|
14
|
+
*/
|
|
15
|
+
mockCtxStorage?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* reuse ctxStorage or not, default is `true`
|
|
18
|
+
*/
|
|
19
|
+
reuseCtxStorage?: boolean;
|
|
20
|
+
}
|
|
21
|
+
interface MockContextData {
|
|
22
|
+
headers?: Record<string, string | string[]>;
|
|
23
|
+
[key: string]: any;
|
|
24
|
+
}
|
|
25
|
+
interface MockContext extends Context {
|
|
26
|
+
service: any;
|
|
27
|
+
}
|
|
28
|
+
declare abstract class ApplicationUnittest extends Application {
|
|
29
|
+
[key: string]: any;
|
|
30
|
+
options: MockOptions & Application['options'];
|
|
31
|
+
_mockHttpClient?: MockHttpClientMethod;
|
|
32
|
+
agent: NonNullable<Application['agent']>;
|
|
33
|
+
/**
|
|
34
|
+
* mock Context
|
|
35
|
+
* @function App#mockContext
|
|
36
|
+
* @param {Object} data - ctx data
|
|
37
|
+
* @param {Object} [options] - mock ctx options
|
|
38
|
+
* @example
|
|
39
|
+
* ```js
|
|
40
|
+
* const ctx = app.mockContext({
|
|
41
|
+
* user: {
|
|
42
|
+
* name: 'Jason'
|
|
43
|
+
* }
|
|
44
|
+
* });
|
|
45
|
+
* console.log(ctx.user.name); // Jason
|
|
46
|
+
*
|
|
47
|
+
* // controller
|
|
48
|
+
* module.exports = function*() {
|
|
49
|
+
* this.body = this.user.name;
|
|
50
|
+
* };
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
mockContext(data?: MockContextData, options?: MockContextOptions): MockContext;
|
|
54
|
+
mockContextScope(fn: (ctx?: MockContext) => Promise<any>, data?: MockContextData): Promise<any>;
|
|
55
|
+
/**
|
|
56
|
+
* mock cookie session
|
|
57
|
+
* @function App#mockSession
|
|
58
|
+
* @param {Object} data - session object
|
|
59
|
+
*/
|
|
60
|
+
mockSession(data: any): this;
|
|
61
|
+
/**
|
|
62
|
+
* Mock service
|
|
63
|
+
* @function App#mockService
|
|
64
|
+
* @param {String} service - name
|
|
65
|
+
* @param {String} methodName - method
|
|
66
|
+
* @param {Object|Function|Error} fn - mock you data
|
|
67
|
+
*/
|
|
68
|
+
mockService(service: string | any, methodName: string, fn: any): this;
|
|
69
|
+
/**
|
|
70
|
+
* mock service that return error
|
|
71
|
+
* @function App#mockServiceError
|
|
72
|
+
* @param {String} service - name
|
|
73
|
+
* @param {String} methodName - method
|
|
74
|
+
* @param {Error} [err] - error information
|
|
75
|
+
*/
|
|
76
|
+
mockServiceError(service: string | any, methodName: string, err?: string | Error): this;
|
|
77
|
+
_mockFn(obj: any, name: string, data: any): void;
|
|
78
|
+
/**
|
|
79
|
+
* mock request
|
|
80
|
+
* @function App#mockRequest
|
|
81
|
+
* @param {Request} req - mock request
|
|
82
|
+
*/
|
|
83
|
+
mockRequest(req: MockContextData): IncomingMessage;
|
|
84
|
+
/**
|
|
85
|
+
* mock cookies
|
|
86
|
+
* @function App#mockCookies
|
|
87
|
+
*/
|
|
88
|
+
mockCookies(cookies: Record<string, string | string[]>): this;
|
|
89
|
+
/**
|
|
90
|
+
* mock header
|
|
91
|
+
* @function App#mockHeaders
|
|
92
|
+
*/
|
|
93
|
+
mockHeaders(headers: Record<string, string | string[]>): this;
|
|
94
|
+
/**
|
|
95
|
+
* mock csrf
|
|
96
|
+
* @function App#mockCsrf
|
|
97
|
+
* @since 1.11
|
|
98
|
+
*/
|
|
99
|
+
mockCsrf(): this;
|
|
100
|
+
/**
|
|
101
|
+
* mock httpclient
|
|
102
|
+
* @alias mockHttpClient
|
|
103
|
+
* @function App#mockHttpclient
|
|
104
|
+
*/
|
|
105
|
+
mockHttpclient(mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string): this;
|
|
106
|
+
/**
|
|
107
|
+
* mock httpclient
|
|
108
|
+
* @function App#mockHttpClient
|
|
109
|
+
*/
|
|
110
|
+
mockHttpClient(mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string): this;
|
|
111
|
+
/**
|
|
112
|
+
* @deprecated Please use app.mockHttpClient instead of app.mockUrllib
|
|
113
|
+
*/
|
|
114
|
+
mockUrllib(mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string): this;
|
|
115
|
+
/**
|
|
116
|
+
* get mock httpclient agent
|
|
117
|
+
* @function App#mockHttpclientAgent
|
|
118
|
+
*/
|
|
119
|
+
mockAgent(): MockAgent;
|
|
120
|
+
mockAgentRestore(): Promise<void>;
|
|
121
|
+
/**
|
|
122
|
+
* @see mm#restore
|
|
123
|
+
* @function App#mockRestore
|
|
124
|
+
*/
|
|
125
|
+
mockRestore(): Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* @see mm
|
|
128
|
+
* @function App#mm
|
|
129
|
+
*/
|
|
130
|
+
get mm(): typeof mock;
|
|
131
|
+
/**
|
|
132
|
+
* override loadAgent
|
|
133
|
+
* @function App#loadAgent
|
|
134
|
+
*/
|
|
135
|
+
loadAgent(): void;
|
|
136
|
+
/**
|
|
137
|
+
* mock serverEnv
|
|
138
|
+
* @function App#mockEnv
|
|
139
|
+
* @param {String} env - serverEnv
|
|
140
|
+
*/
|
|
141
|
+
mockEnv(env: string): this;
|
|
142
|
+
/**
|
|
143
|
+
* http request helper
|
|
144
|
+
* @function App#httpRequest
|
|
145
|
+
* @return {SupertestRequest} req - supertest request
|
|
146
|
+
* @see https://github.com/visionmedia/supertest
|
|
147
|
+
*/
|
|
148
|
+
httpRequest(): EggTestRequest;
|
|
149
|
+
/**
|
|
150
|
+
* collection logger message, then can be use on `expectLog()`
|
|
151
|
+
* @param {String|Logger} [logger] - logger instance, default is `app.logger`
|
|
152
|
+
* @function App#mockLog
|
|
153
|
+
*/
|
|
154
|
+
mockLog(logger?: string | Logger): void;
|
|
155
|
+
__checkExpectLog(expectOrNot: boolean, str: string | RegExp, logger?: string | Logger): void;
|
|
156
|
+
/**
|
|
157
|
+
* expect str/regexp in the logger, if your server disk is slow, please call `mockLog()` first.
|
|
158
|
+
* @param {String|RegExp} str - test str or regexp
|
|
159
|
+
* @param {String|Logger} [logger] - logger instance, default is `ctx.logger`
|
|
160
|
+
* @function App#expectLog
|
|
161
|
+
*/
|
|
162
|
+
expectLog(str: string | RegExp, logger?: string | Logger): void;
|
|
163
|
+
/**
|
|
164
|
+
* not expect str/regexp in the logger, if your server disk is slow, please call `mockLog()` first.
|
|
165
|
+
* @param {String|RegExp} str - test str or regexp
|
|
166
|
+
* @param {String|Logger} [logger] - logger instance, default is `ctx.logger`
|
|
167
|
+
* @function App#notExpectLog
|
|
168
|
+
*/
|
|
169
|
+
notExpectLog(str: string | RegExp, logger?: string | Logger): void;
|
|
170
|
+
backgroundTasksFinished(): Promise<void>;
|
|
171
|
+
get _backgroundTasks(): Promise<any>[];
|
|
172
|
+
set _backgroundTasks(tasks: Promise<any>[]);
|
|
173
|
+
}
|
|
174
|
+
//#endregion
|
|
175
|
+
export { MockContext, MockContextData, MockContextOptions, ApplicationUnittest as default };
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import { request } from "../../lib/supertest.js";
|
|
2
|
+
import { getMockAgent, restoreMockAgent } from "../../lib/mock_agent.js";
|
|
3
|
+
import { createMockHttpClient } from "../../lib/mock_httpclient.js";
|
|
4
|
+
import { Application } from "egg";
|
|
5
|
+
import assert from "node:assert";
|
|
6
|
+
import { mock, restore } from "mm";
|
|
7
|
+
import { debuglog } from "node:util";
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import http, { IncomingMessage } from "node:http";
|
|
10
|
+
import mergeDescriptors from "merge-descriptors";
|
|
11
|
+
import { isAsyncFunction, isObject } from "is-type-of";
|
|
12
|
+
import { Logger, Transport } from "egg-logger";
|
|
13
|
+
|
|
14
|
+
//#region src/app/extend/application.ts
|
|
15
|
+
const debug = debuglog("egg/mock/app/extend/application");
|
|
16
|
+
const ORIGIN_TYPES = Symbol("@eggjs/mock originTypes");
|
|
17
|
+
const BACKGROUND_TASKS = Symbol("Application#backgroundTasks");
|
|
18
|
+
const REUSED_CTX = Symbol("Context#reusedInSuite");
|
|
19
|
+
var ApplicationUnittest = class extends Application {
|
|
20
|
+
_mockHttpClient;
|
|
21
|
+
/**
|
|
22
|
+
* mock Context
|
|
23
|
+
* @function App#mockContext
|
|
24
|
+
* @param {Object} data - ctx data
|
|
25
|
+
* @param {Object} [options] - mock ctx options
|
|
26
|
+
* @example
|
|
27
|
+
* ```js
|
|
28
|
+
* const ctx = app.mockContext({
|
|
29
|
+
* user: {
|
|
30
|
+
* name: 'Jason'
|
|
31
|
+
* }
|
|
32
|
+
* });
|
|
33
|
+
* console.log(ctx.user.name); // Jason
|
|
34
|
+
*
|
|
35
|
+
* // controller
|
|
36
|
+
* module.exports = function*() {
|
|
37
|
+
* this.body = this.user.name;
|
|
38
|
+
* };
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
mockContext(data, options) {
|
|
42
|
+
data = data ?? {};
|
|
43
|
+
function mockRequest(req$1) {
|
|
44
|
+
for (const key in data?.headers) {
|
|
45
|
+
mock(req$1.headers, key, data.headers[key]);
|
|
46
|
+
mock(req$1.headers, key.toLowerCase(), data.headers[key]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const mockCtxStorage = this.options.mockCtxStorage ?? true;
|
|
50
|
+
options = Object.assign({ mockCtxStorage }, options);
|
|
51
|
+
if ("_customMockContext" in this && typeof this._customMockContext === "function") this._customMockContext(data);
|
|
52
|
+
for (const key in data) mock(this.context, key, data[key]);
|
|
53
|
+
const req = this.mockRequest(data);
|
|
54
|
+
const res = new http.ServerResponse(req);
|
|
55
|
+
if (options.reuseCtxStorage !== false) {
|
|
56
|
+
if (this.currentContext && !this.currentContext[REUSED_CTX]) {
|
|
57
|
+
mockRequest(this.currentContext.request.req);
|
|
58
|
+
this.currentContext[REUSED_CTX] = true;
|
|
59
|
+
return this.currentContext;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const ctx = this.createContext(req, res);
|
|
63
|
+
if (options.mockCtxStorage) mock(this.ctxStorage, "getStore", () => ctx);
|
|
64
|
+
return ctx;
|
|
65
|
+
}
|
|
66
|
+
async mockContextScope(fn, data) {
|
|
67
|
+
const ctx = this.mockContext(data, {
|
|
68
|
+
mockCtxStorage: false,
|
|
69
|
+
reuseCtxStorage: false
|
|
70
|
+
});
|
|
71
|
+
return await this.ctxStorage.run(ctx, async () => {
|
|
72
|
+
return await fn(ctx);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* mock cookie session
|
|
77
|
+
* @function App#mockSession
|
|
78
|
+
* @param {Object} data - session object
|
|
79
|
+
*/
|
|
80
|
+
mockSession(data) {
|
|
81
|
+
if (!data) return this;
|
|
82
|
+
if (isObject(data) && !("save" in data)) Object.defineProperty(data, "save", {
|
|
83
|
+
value: () => {},
|
|
84
|
+
enumerable: false
|
|
85
|
+
});
|
|
86
|
+
mock(this.context, "session", data);
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Mock service
|
|
91
|
+
* @function App#mockService
|
|
92
|
+
* @param {String} service - name
|
|
93
|
+
* @param {String} methodName - method
|
|
94
|
+
* @param {Object|Function|Error} fn - mock you data
|
|
95
|
+
*/
|
|
96
|
+
mockService(service, methodName, fn) {
|
|
97
|
+
if (typeof service === "string") {
|
|
98
|
+
const splits = service.split(".");
|
|
99
|
+
service = this.serviceClasses;
|
|
100
|
+
for (const key of splits) service = service[key];
|
|
101
|
+
service = service.prototype || service;
|
|
102
|
+
}
|
|
103
|
+
this._mockFn(service, methodName, fn);
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* mock service that return error
|
|
108
|
+
* @function App#mockServiceError
|
|
109
|
+
* @param {String} service - name
|
|
110
|
+
* @param {String} methodName - method
|
|
111
|
+
* @param {Error} [err] - error information
|
|
112
|
+
*/
|
|
113
|
+
mockServiceError(service, methodName, err) {
|
|
114
|
+
if (typeof err === "string") err = new Error(err);
|
|
115
|
+
if (!err) err = /* @__PURE__ */ new Error(`mock ${methodName} error`);
|
|
116
|
+
this.mockService(service, methodName, err);
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
_mockFn(obj, name, data) {
|
|
120
|
+
const origin = obj[name];
|
|
121
|
+
assert(typeof origin === "function", `property ${name} in original object must be function`);
|
|
122
|
+
if (!obj[ORIGIN_TYPES]) obj[ORIGIN_TYPES] = {};
|
|
123
|
+
let type = obj[ORIGIN_TYPES][name];
|
|
124
|
+
if (!type) type = obj[ORIGIN_TYPES][name] = isAsyncFunction(origin) ? "async" : "sync";
|
|
125
|
+
if (typeof data === "function") {
|
|
126
|
+
const fn = data;
|
|
127
|
+
if (type === "async" && !isAsyncFunction(fn)) {
|
|
128
|
+
mock(obj, name, function(...args) {
|
|
129
|
+
return new Promise((resolve) => {
|
|
130
|
+
resolve(fn.apply(this, args));
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
mock(obj, name, fn);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (type === "async") {
|
|
139
|
+
mock(obj, name, () => {
|
|
140
|
+
return new Promise((resolve, reject) => {
|
|
141
|
+
if (data instanceof Error) return reject(data);
|
|
142
|
+
resolve(data);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
mock(obj, name, () => {
|
|
148
|
+
if (data instanceof Error) throw data;
|
|
149
|
+
return data;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* mock request
|
|
154
|
+
* @function App#mockRequest
|
|
155
|
+
* @param {Request} req - mock request
|
|
156
|
+
*/
|
|
157
|
+
mockRequest(req) {
|
|
158
|
+
req = { ...req };
|
|
159
|
+
const headers = req.headers ?? {};
|
|
160
|
+
for (const key in req.headers) headers[key.toLowerCase()] = req.headers[key];
|
|
161
|
+
if (!headers["x-forwarded-for"]) headers["x-forwarded-for"] = "127.0.0.1";
|
|
162
|
+
headers["x-mock-request-from"] = "@eggjs/mock";
|
|
163
|
+
req.headers = headers;
|
|
164
|
+
mergeDescriptors(req, {
|
|
165
|
+
query: {},
|
|
166
|
+
querystring: "",
|
|
167
|
+
host: "127.0.0.1",
|
|
168
|
+
hostname: "127.0.0.1",
|
|
169
|
+
protocol: "http",
|
|
170
|
+
secure: "false",
|
|
171
|
+
method: "GET",
|
|
172
|
+
url: "/",
|
|
173
|
+
path: "/",
|
|
174
|
+
socket: {
|
|
175
|
+
remoteAddress: "127.0.0.1",
|
|
176
|
+
remotePort: 7001
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
return req;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* mock cookies
|
|
183
|
+
* @function App#mockCookies
|
|
184
|
+
*/
|
|
185
|
+
mockCookies(cookies) {
|
|
186
|
+
if (!cookies) return this;
|
|
187
|
+
const createContext = this.createContext;
|
|
188
|
+
mock(this, "createContext", function(req, res) {
|
|
189
|
+
const ctx = createContext.call(this, req, res);
|
|
190
|
+
const getCookie = ctx.cookies.get;
|
|
191
|
+
mock(ctx.cookies, "get", function(key, opts) {
|
|
192
|
+
if (cookies[key]) return cookies[key];
|
|
193
|
+
return getCookie.call(this, key, opts);
|
|
194
|
+
});
|
|
195
|
+
return ctx;
|
|
196
|
+
});
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* mock header
|
|
201
|
+
* @function App#mockHeaders
|
|
202
|
+
*/
|
|
203
|
+
mockHeaders(headers) {
|
|
204
|
+
if (!headers) return this;
|
|
205
|
+
const getHeader = this.request.get;
|
|
206
|
+
mock(this.request, "get", function(field) {
|
|
207
|
+
const value = findHeaders(headers, field);
|
|
208
|
+
if (value) return value;
|
|
209
|
+
return getHeader.call(this, field);
|
|
210
|
+
});
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* mock csrf
|
|
215
|
+
* @function App#mockCsrf
|
|
216
|
+
* @since 1.11
|
|
217
|
+
*/
|
|
218
|
+
mockCsrf() {
|
|
219
|
+
mock(this.context, "assertCSRF", () => {});
|
|
220
|
+
mock(this.context, "assertCsrf", () => {});
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* mock httpclient
|
|
225
|
+
* @alias mockHttpClient
|
|
226
|
+
* @function App#mockHttpclient
|
|
227
|
+
*/
|
|
228
|
+
mockHttpclient(mockUrl, mockMethod, mockResult) {
|
|
229
|
+
return this.mockHttpClient(mockUrl, mockMethod, mockResult);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* mock httpclient
|
|
233
|
+
* @function App#mockHttpClient
|
|
234
|
+
*/
|
|
235
|
+
mockHttpClient(mockUrl, mockMethod, mockResult) {
|
|
236
|
+
if (!this._mockHttpClient) this._mockHttpClient = createMockHttpClient(this);
|
|
237
|
+
this._mockHttpClient(mockUrl, mockMethod, mockResult);
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* @deprecated Please use app.mockHttpClient instead of app.mockUrllib
|
|
242
|
+
*/
|
|
243
|
+
mockUrllib(mockUrl, mockMethod, mockResult) {
|
|
244
|
+
this.deprecate("[@eggjs/mock] Please use app.mockHttpClient instead of app.mockUrllib");
|
|
245
|
+
return this.mockHttpClient(mockUrl, mockMethod, mockResult);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* get mock httpclient agent
|
|
249
|
+
* @function App#mockHttpclientAgent
|
|
250
|
+
*/
|
|
251
|
+
mockAgent() {
|
|
252
|
+
return getMockAgent(this);
|
|
253
|
+
}
|
|
254
|
+
async mockAgentRestore() {
|
|
255
|
+
await restoreMockAgent();
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* @see mm#restore
|
|
259
|
+
* @function App#mockRestore
|
|
260
|
+
*/
|
|
261
|
+
async mockRestore() {
|
|
262
|
+
await this.mockAgentRestore();
|
|
263
|
+
restore();
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* @see mm
|
|
267
|
+
* @function App#mm
|
|
268
|
+
*/
|
|
269
|
+
get mm() {
|
|
270
|
+
return mock;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* override loadAgent
|
|
274
|
+
* @function App#loadAgent
|
|
275
|
+
*/
|
|
276
|
+
loadAgent() {}
|
|
277
|
+
/**
|
|
278
|
+
* mock serverEnv
|
|
279
|
+
* @function App#mockEnv
|
|
280
|
+
* @param {String} env - serverEnv
|
|
281
|
+
*/
|
|
282
|
+
mockEnv(env) {
|
|
283
|
+
mock(this.config, "env", env);
|
|
284
|
+
mock(this.config, "serverEnv", env);
|
|
285
|
+
debug("mock env: %o", env);
|
|
286
|
+
return this;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* http request helper
|
|
290
|
+
* @function App#httpRequest
|
|
291
|
+
* @return {SupertestRequest} req - supertest request
|
|
292
|
+
* @see https://github.com/visionmedia/supertest
|
|
293
|
+
*/
|
|
294
|
+
httpRequest() {
|
|
295
|
+
return request(this);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* collection logger message, then can be use on `expectLog()`
|
|
299
|
+
* @param {String|Logger} [logger] - logger instance, default is `app.logger`
|
|
300
|
+
* @function App#mockLog
|
|
301
|
+
*/
|
|
302
|
+
mockLog(logger) {
|
|
303
|
+
logger = logger ?? this.logger;
|
|
304
|
+
if (typeof logger === "string") logger = this.getLogger(logger);
|
|
305
|
+
if ("_mockLogs" in logger && logger._mockLogs) return;
|
|
306
|
+
const transport = new Transport(logger.options);
|
|
307
|
+
const log = logger.log;
|
|
308
|
+
const mockLogs = [];
|
|
309
|
+
mock(logger, "_mockLogs", mockLogs);
|
|
310
|
+
mock(logger, "log", (level, args, meta) => {
|
|
311
|
+
const message = transport.log(level, args, meta);
|
|
312
|
+
mockLogs.push(message);
|
|
313
|
+
log.apply(logger, [
|
|
314
|
+
level,
|
|
315
|
+
args,
|
|
316
|
+
meta
|
|
317
|
+
]);
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
__checkExpectLog(expectOrNot, str, logger) {
|
|
321
|
+
logger = logger || this.logger;
|
|
322
|
+
if (typeof logger === "string") logger = this.getLogger(logger);
|
|
323
|
+
const filepath = logger.options.file;
|
|
324
|
+
let content;
|
|
325
|
+
if ("_mockLogs" in logger && logger._mockLogs) content = logger._mockLogs.join("\n");
|
|
326
|
+
else content = fs.readFileSync(filepath, "utf8");
|
|
327
|
+
let match;
|
|
328
|
+
let type;
|
|
329
|
+
if (str instanceof RegExp) {
|
|
330
|
+
match = str.test(content);
|
|
331
|
+
type = "RegExp";
|
|
332
|
+
} else {
|
|
333
|
+
match = content.includes(String(str));
|
|
334
|
+
type = "String";
|
|
335
|
+
}
|
|
336
|
+
if (expectOrNot) assert(match, `Can't find ${type}:"${str}" in ${filepath}, log content: ...${content.substring(content.length - 500)}`);
|
|
337
|
+
else assert(!match, `Find ${type}:"${str}" in ${filepath}, log content: ...${content.substring(content.length - 500)}`);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* expect str/regexp in the logger, if your server disk is slow, please call `mockLog()` first.
|
|
341
|
+
* @param {String|RegExp} str - test str or regexp
|
|
342
|
+
* @param {String|Logger} [logger] - logger instance, default is `ctx.logger`
|
|
343
|
+
* @function App#expectLog
|
|
344
|
+
*/
|
|
345
|
+
expectLog(str, logger) {
|
|
346
|
+
this.__checkExpectLog(true, str, logger);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* not expect str/regexp in the logger, if your server disk is slow, please call `mockLog()` first.
|
|
350
|
+
* @param {String|RegExp} str - test str or regexp
|
|
351
|
+
* @param {String|Logger} [logger] - logger instance, default is `ctx.logger`
|
|
352
|
+
* @function App#notExpectLog
|
|
353
|
+
*/
|
|
354
|
+
notExpectLog(str, logger) {
|
|
355
|
+
this.__checkExpectLog(false, str, logger);
|
|
356
|
+
}
|
|
357
|
+
async backgroundTasksFinished() {
|
|
358
|
+
const tasks = this._backgroundTasks;
|
|
359
|
+
debug("waiting %d background tasks", tasks.length);
|
|
360
|
+
if (tasks.length === 0) return;
|
|
361
|
+
this._backgroundTasks = [];
|
|
362
|
+
await Promise.all(tasks);
|
|
363
|
+
debug("finished %d background tasks", tasks.length);
|
|
364
|
+
if (this._backgroundTasks.length) {
|
|
365
|
+
debug("new background tasks created: %s", this._backgroundTasks.length);
|
|
366
|
+
await this.backgroundTasksFinished();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
get _backgroundTasks() {
|
|
370
|
+
if (!this[BACKGROUND_TASKS]) this[BACKGROUND_TASKS] = [];
|
|
371
|
+
return this[BACKGROUND_TASKS];
|
|
372
|
+
}
|
|
373
|
+
set _backgroundTasks(tasks) {
|
|
374
|
+
this[BACKGROUND_TASKS] = tasks;
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
function findHeaders(headers, key) {
|
|
378
|
+
if (!headers || !key) return null;
|
|
379
|
+
key = key.toLowerCase();
|
|
380
|
+
for (const headerKey in headers) if (key === headerKey.toLowerCase()) return headers[headerKey];
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
//#endregion
|
|
385
|
+
export { ApplicationUnittest as default };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Context } from "egg";
|
|
2
|
+
import { debuglog } from "node:util";
|
|
3
|
+
|
|
4
|
+
//#region src/app/middleware/cluster_app_mock.ts
|
|
5
|
+
const debug = debuglog("egg/mock/app/middleware/cluster_app_mock");
|
|
6
|
+
var cluster_app_mock_default = () => {
|
|
7
|
+
return async function clusterAppMock(ctx, next) {
|
|
8
|
+
if (ctx.originalUrl !== "/__egg_mock_call_function") return next();
|
|
9
|
+
const body = ctx.request.body;
|
|
10
|
+
debug("%s %s, body: %j", ctx.method, ctx.url, body);
|
|
11
|
+
const { method, property, args, needResult } = body;
|
|
12
|
+
if (!method) {
|
|
13
|
+
ctx.status = 422;
|
|
14
|
+
ctx.body = {
|
|
15
|
+
success: false,
|
|
16
|
+
error: "Missing method"
|
|
17
|
+
};
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (args && !Array.isArray(args)) {
|
|
21
|
+
ctx.status = 422;
|
|
22
|
+
ctx.body = {
|
|
23
|
+
success: false,
|
|
24
|
+
error: "args should be an Array instance"
|
|
25
|
+
};
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (property) {
|
|
29
|
+
if (method === "__getter__") {
|
|
30
|
+
if (!ctx.app[property]) {
|
|
31
|
+
debug("property %s not exists on app", property);
|
|
32
|
+
ctx.status = 422;
|
|
33
|
+
ctx.body = {
|
|
34
|
+
success: false,
|
|
35
|
+
error: `property "${property}" not exists on app`
|
|
36
|
+
};
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
ctx.body = {
|
|
40
|
+
success: true,
|
|
41
|
+
result: ctx.app[property]
|
|
42
|
+
};
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (!ctx.app[property] || typeof ctx.app[property][method] !== "function") {
|
|
46
|
+
debug("property %s.%s not exists on app", property, method);
|
|
47
|
+
ctx.status = 422;
|
|
48
|
+
ctx.body = {
|
|
49
|
+
success: false,
|
|
50
|
+
error: `method "${method}" not exists on app.${property}`
|
|
51
|
+
};
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
} else if (typeof ctx.app[method] !== "function") {
|
|
55
|
+
debug("method %s not exists on app", method);
|
|
56
|
+
ctx.status = 422;
|
|
57
|
+
ctx.body = {
|
|
58
|
+
success: false,
|
|
59
|
+
error: `method "${method}" not exists on app`
|
|
60
|
+
};
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
debug("call %s with %j", method, args);
|
|
64
|
+
for (let i = 0; i < args.length; i++) {
|
|
65
|
+
const arg = args[i];
|
|
66
|
+
if (arg && typeof arg === "object") {
|
|
67
|
+
if (arg.__egg_mock_type === "function") args[i] = eval(`(function() { return ${arg.value} })()`);
|
|
68
|
+
else if (arg.__egg_mock_type === "error") {
|
|
69
|
+
const err = new Error(arg.message);
|
|
70
|
+
err.name = arg.name;
|
|
71
|
+
err.stack = arg.stack;
|
|
72
|
+
for (const key in arg) if (key !== "name" && key !== "message" && key !== "stack" && key !== "__egg_mock_type") err[key] = arg[key];
|
|
73
|
+
args[i] = err;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const target = property ? ctx.app[property] : ctx.app;
|
|
78
|
+
const fn = target[method];
|
|
79
|
+
try {
|
|
80
|
+
Promise.resolve(fn.call(target, ...args)).then((result) => {
|
|
81
|
+
ctx.body = needResult ? {
|
|
82
|
+
success: true,
|
|
83
|
+
result
|
|
84
|
+
} : { success: true };
|
|
85
|
+
});
|
|
86
|
+
} catch (err) {
|
|
87
|
+
ctx.status = 500;
|
|
88
|
+
ctx.body = {
|
|
89
|
+
success: false,
|
|
90
|
+
error: err.message
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
//#endregion
|
|
97
|
+
export { cluster_app_mock_default as default };
|
package/dist/app.d.ts
ADDED
package/dist/app.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Application } from "egg";
|
|
2
|
+
|
|
3
|
+
//#region src/app.ts
|
|
4
|
+
var Boot = class {
|
|
5
|
+
#app;
|
|
6
|
+
constructor(app) {
|
|
7
|
+
this.#app = app;
|
|
8
|
+
}
|
|
9
|
+
configWillLoad() {
|
|
10
|
+
const index = this.#app.config.coreMiddleware.indexOf("securities");
|
|
11
|
+
if (index >= 0) this.#app.config.coreMiddleware.splice(index, 0, "clusterAppMock");
|
|
12
|
+
else this.#app.config.coreMiddleware.push("clusterAppMock");
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { Boot as default };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import ApplicationUnittest from "./app/extend/application.js";
|
|
2
|
+
import { getBootstrapApp } from "./lib/app_handler.js";
|
|
3
|
+
import proxyMock from "./index.js";
|
|
4
|
+
import { strict as assert } from "node:assert";
|
|
5
|
+
|
|
6
|
+
//#region src/bootstrap.d.ts
|
|
7
|
+
declare const app: ApplicationUnittest;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { type ApplicationUnittest as MockApplication, app, assert, getBootstrapApp, proxyMock as mm, proxyMock as mock };
|