@eggjs/mock 6.0.7 → 6.1.0-beta.2

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 (211) hide show
  1. package/README.md +35 -56
  2. package/README.zh_CN.md +30 -50
  3. package/dist/_virtual/rolldown_runtime.js +7 -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 +176 -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 +48 -0
  16. package/dist/lib/agent_handler.d.ts +7 -0
  17. package/dist/lib/agent_handler.js +26 -0
  18. package/dist/lib/app.d.ts +7 -0
  19. package/dist/lib/app.js +251 -0
  20. package/dist/lib/app_handler.d.ts +9 -0
  21. package/dist/lib/app_handler.js +56 -0
  22. package/dist/lib/cluster.d.ts +126 -0
  23. package/dist/lib/cluster.js +283 -0
  24. package/dist/lib/context.d.ts +4 -0
  25. package/dist/lib/context.js +13 -0
  26. package/dist/lib/format_options.d.ts +10 -0
  27. package/dist/lib/format_options.js +65 -0
  28. package/dist/lib/inject_context.d.ts +9 -0
  29. package/dist/lib/inject_context.js +106 -0
  30. package/dist/lib/mock_agent.d.ts +9 -0
  31. package/dist/lib/mock_agent.js +45 -0
  32. package/dist/lib/mock_custom_loader.d.ts +4 -0
  33. package/dist/lib/mock_custom_loader.js +35 -0
  34. package/dist/lib/mock_http_server.d.ts +6 -0
  35. package/dist/lib/mock_http_server.js +17 -0
  36. package/dist/lib/mock_httpclient.d.ts +40 -0
  37. package/dist/lib/mock_httpclient.js +110 -0
  38. package/dist/lib/parallel/agent.d.ts +24 -0
  39. package/dist/lib/parallel/agent.js +111 -0
  40. package/dist/lib/parallel/app.d.ts +24 -0
  41. package/dist/lib/parallel/app.js +98 -0
  42. package/dist/lib/parallel/util.d.ts +6 -0
  43. package/dist/lib/parallel/util.js +59 -0
  44. package/dist/lib/prerequire.d.ts +1 -0
  45. package/dist/lib/prerequire.js +1 -0
  46. package/dist/lib/request_call_function.d.ts +1 -0
  47. package/dist/lib/request_call_function.js +37 -0
  48. package/dist/lib/restore.d.ts +4 -0
  49. package/dist/lib/restore.js +16 -0
  50. package/dist/{commonjs/lib → lib}/start-cluster.d.ts +1 -1
  51. package/dist/lib/start-cluster.js +19 -0
  52. package/dist/lib/supertest.d.ts +16 -0
  53. package/dist/lib/supertest.js +39 -0
  54. package/dist/lib/tmp/empty.d.ts +1 -0
  55. package/dist/lib/tmp/empty.js +1 -0
  56. package/{src/lib/types.ts → dist/lib/types.d.ts} +12 -29
  57. package/dist/lib/types.js +1 -0
  58. package/dist/lib/utils.d.ts +12 -0
  59. package/dist/lib/utils.js +40 -0
  60. package/dist/register.d.ts +12 -0
  61. package/dist/register.js +65 -0
  62. package/dist/typings/index.d.ts +1 -0
  63. package/package.json +68 -100
  64. package/dist/commonjs/app/extend/agent.d.ts +0 -34
  65. package/dist/commonjs/app/extend/agent.js +0 -49
  66. package/dist/commonjs/app/extend/application.d.ts +0 -172
  67. package/dist/commonjs/app/extend/application.js +0 -450
  68. package/dist/commonjs/app/middleware/cluster_app_mock.d.ts +0 -3
  69. package/dist/commonjs/app/middleware/cluster_app_mock.js +0 -101
  70. package/dist/commonjs/app.d.ts +0 -6
  71. package/dist/commonjs/app.js +0 -20
  72. package/dist/commonjs/bootstrap.d.ts +0 -5
  73. package/dist/commonjs/bootstrap.js +0 -59
  74. package/dist/commonjs/index.d.ts +0 -77
  75. package/dist/commonjs/index.js +0 -114
  76. package/dist/commonjs/lib/agent_handler.d.ts +0 -3
  77. package/dist/commonjs/lib/agent_handler.js +0 -28
  78. package/dist/commonjs/lib/app.d.ts +0 -3
  79. package/dist/commonjs/lib/app.js +0 -301
  80. package/dist/commonjs/lib/app_handler.d.ts +0 -5
  81. package/dist/commonjs/lib/app_handler.js +0 -71
  82. package/dist/commonjs/lib/cluster.d.ts +0 -114
  83. package/dist/commonjs/lib/cluster.js +0 -337
  84. package/dist/commonjs/lib/context.d.ts +0 -1
  85. package/dist/commonjs/lib/context.js +0 -16
  86. package/dist/commonjs/lib/format_options.d.ts +0 -5
  87. package/dist/commonjs/lib/format_options.js +0 -100
  88. package/dist/commonjs/lib/inject_context.d.ts +0 -6
  89. package/dist/commonjs/lib/inject_context.js +0 -132
  90. package/dist/commonjs/lib/mock_agent.d.ts +0 -5
  91. package/dist/commonjs/lib/mock_agent.js +0 -49
  92. package/dist/commonjs/lib/mock_custom_loader.d.ts +0 -1
  93. package/dist/commonjs/lib/mock_custom_loader.js +0 -37
  94. package/dist/commonjs/lib/mock_http_server.d.ts +0 -2
  95. package/dist/commonjs/lib/mock_http_server.js +0 -24
  96. package/dist/commonjs/lib/mock_httpclient.d.ts +0 -36
  97. package/dist/commonjs/lib/mock_httpclient.js +0 -147
  98. package/dist/commonjs/lib/parallel/agent.d.ts +0 -20
  99. package/dist/commonjs/lib/parallel/agent.js +0 -125
  100. package/dist/commonjs/lib/parallel/app.d.ts +0 -20
  101. package/dist/commonjs/lib/parallel/app.js +0 -115
  102. package/dist/commonjs/lib/parallel/util.d.ts +0 -3
  103. package/dist/commonjs/lib/parallel/util.js +0 -77
  104. package/dist/commonjs/lib/prerequire.d.ts +0 -1
  105. package/dist/commonjs/lib/prerequire.js +0 -26
  106. package/dist/commonjs/lib/request_call_function.d.ts +0 -1
  107. package/dist/commonjs/lib/request_call_function.js +0 -52
  108. package/dist/commonjs/lib/restore.d.ts +0 -1
  109. package/dist/commonjs/lib/restore.js +0 -16
  110. package/dist/commonjs/lib/start-cluster.js +0 -23
  111. package/dist/commonjs/lib/supertest.d.ts +0 -11
  112. package/dist/commonjs/lib/supertest.js +0 -48
  113. package/dist/commonjs/lib/tmp/empty.d.ts +0 -1
  114. package/dist/commonjs/lib/tmp/empty.js +0 -3
  115. package/dist/commonjs/lib/types.d.ts +0 -68
  116. package/dist/commonjs/lib/types.js +0 -3
  117. package/dist/commonjs/lib/utils.d.ts +0 -9
  118. package/dist/commonjs/lib/utils.js +0 -80
  119. package/dist/commonjs/package.json +0 -3
  120. package/dist/commonjs/register.d.ts +0 -8
  121. package/dist/commonjs/register.js +0 -80
  122. package/dist/esm/app/extend/agent.d.ts +0 -34
  123. package/dist/esm/app/extend/agent.js +0 -46
  124. package/dist/esm/app/extend/application.d.ts +0 -172
  125. package/dist/esm/app/extend/application.js +0 -444
  126. package/dist/esm/app/middleware/cluster_app_mock.d.ts +0 -3
  127. package/dist/esm/app/middleware/cluster_app_mock.js +0 -99
  128. package/dist/esm/app.d.ts +0 -6
  129. package/dist/esm/app.js +0 -17
  130. package/dist/esm/bootstrap.d.ts +0 -5
  131. package/dist/esm/bootstrap.js +0 -15
  132. package/dist/esm/index.d.ts +0 -77
  133. package/dist/esm/index.js +0 -91
  134. package/dist/esm/lib/agent_handler.d.ts +0 -3
  135. package/dist/esm/lib/agent_handler.js +0 -24
  136. package/dist/esm/lib/app.d.ts +0 -3
  137. package/dist/esm/lib/app.js +0 -295
  138. package/dist/esm/lib/app_handler.d.ts +0 -5
  139. package/dist/esm/lib/app_handler.js +0 -65
  140. package/dist/esm/lib/cluster.d.ts +0 -114
  141. package/dist/esm/lib/cluster.js +0 -328
  142. package/dist/esm/lib/context.d.ts +0 -1
  143. package/dist/esm/lib/context.js +0 -13
  144. package/dist/esm/lib/format_options.d.ts +0 -5
  145. package/dist/esm/lib/format_options.js +0 -94
  146. package/dist/esm/lib/inject_context.d.ts +0 -6
  147. package/dist/esm/lib/inject_context.js +0 -126
  148. package/dist/esm/lib/mock_agent.d.ts +0 -5
  149. package/dist/esm/lib/mock_agent.js +0 -45
  150. package/dist/esm/lib/mock_custom_loader.d.ts +0 -1
  151. package/dist/esm/lib/mock_custom_loader.js +0 -34
  152. package/dist/esm/lib/mock_http_server.d.ts +0 -2
  153. package/dist/esm/lib/mock_http_server.js +0 -18
  154. package/dist/esm/lib/mock_httpclient.d.ts +0 -36
  155. package/dist/esm/lib/mock_httpclient.js +0 -144
  156. package/dist/esm/lib/parallel/agent.d.ts +0 -20
  157. package/dist/esm/lib/parallel/agent.js +0 -117
  158. package/dist/esm/lib/parallel/app.d.ts +0 -20
  159. package/dist/esm/lib/parallel/app.js +0 -110
  160. package/dist/esm/lib/parallel/util.d.ts +0 -3
  161. package/dist/esm/lib/parallel/util.js +0 -73
  162. package/dist/esm/lib/prerequire.d.ts +0 -1
  163. package/dist/esm/lib/prerequire.js +0 -25
  164. package/dist/esm/lib/request_call_function.d.ts +0 -1
  165. package/dist/esm/lib/request_call_function.js +0 -47
  166. package/dist/esm/lib/restore.d.ts +0 -1
  167. package/dist/esm/lib/restore.js +0 -13
  168. package/dist/esm/lib/start-cluster.d.ts +0 -2
  169. package/dist/esm/lib/start-cluster.js +0 -18
  170. package/dist/esm/lib/supertest.d.ts +0 -11
  171. package/dist/esm/lib/supertest.js +0 -40
  172. package/dist/esm/lib/tmp/empty.d.ts +0 -1
  173. package/dist/esm/lib/tmp/empty.js +0 -2
  174. package/dist/esm/lib/types.d.ts +0 -68
  175. package/dist/esm/lib/types.js +0 -2
  176. package/dist/esm/lib/utils.d.ts +0 -9
  177. package/dist/esm/lib/utils.js +0 -69
  178. package/dist/esm/package.json +0 -3
  179. package/dist/esm/register.d.ts +0 -8
  180. package/dist/esm/register.js +0 -75
  181. package/dist/package.json +0 -4
  182. package/src/app/extend/agent.ts +0 -57
  183. package/src/app/extend/application.ts +0 -512
  184. package/src/app/middleware/cluster_app_mock.ts +0 -102
  185. package/src/app.ts +0 -18
  186. package/src/bootstrap.ts +0 -25
  187. package/src/index.ts +0 -112
  188. package/src/lib/agent_handler.ts +0 -28
  189. package/src/lib/app.ts +0 -314
  190. package/src/lib/app_handler.ts +0 -77
  191. package/src/lib/cluster.ts +0 -363
  192. package/src/lib/context.ts +0 -14
  193. package/src/lib/format_options.ts +0 -103
  194. package/src/lib/inject_context.ts +0 -134
  195. package/src/lib/mock_agent.ts +0 -57
  196. package/src/lib/mock_custom_loader.ts +0 -36
  197. package/src/lib/mock_http_server.ts +0 -19
  198. package/src/lib/mock_httpclient.ts +0 -183
  199. package/src/lib/parallel/agent.ts +0 -128
  200. package/src/lib/parallel/app.ts +0 -123
  201. package/src/lib/parallel/util.ts +0 -66
  202. package/src/lib/prerequire.ts +0 -25
  203. package/src/lib/request_call_function.ts +0 -49
  204. package/src/lib/restore.ts +0 -14
  205. package/src/lib/start-cluster.ts +0 -23
  206. package/src/lib/supertest.ts +0 -45
  207. package/src/lib/tmp/.gitkeep +0 -0
  208. package/src/lib/tmp/empty.ts +0 -0
  209. package/src/lib/utils.ts +0 -82
  210. package/src/register.ts +0 -80
  211. package/src/typings/index.d.ts +0 -4
package/README.md CHANGED
@@ -1,16 +1,12 @@
1
1
  # @eggjs/mock
2
2
 
3
3
  [![NPM version][npm-image]][npm-url]
4
- [![Node.js CI](https://github.com/eggjs/mock/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/mock/actions/workflows/nodejs.yml)
5
- [![Test coverage][codecov-image]][codecov-url]
6
4
  [![npm download][download-image]][download-url]
7
5
  [![Node.js Version](https://img.shields.io/node/v/@eggjs/mock.svg?style=flat)](https://nodejs.org/en/download/)
8
6
  [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
9
7
 
10
8
  [npm-image]: https://img.shields.io/npm/v/@eggjs/mock.svg?style=flat-square
11
9
  [npm-url]: https://npmjs.org/package/@eggjs/mock
12
- [codecov-image]: https://codecov.io/github/eggjs/mock/coverage.svg?branch=master
13
- [codecov-url]: https://codecov.io/github/eggjs/mock?branch=master
14
10
  [download-image]: https://img.shields.io/npm/dm/@eggjs/mock.svg?style=flat-square
15
11
  [download-url]: https://npmjs.org/package/@eggjs/mock
16
12
 
@@ -38,16 +34,14 @@ describe('some test', () => {
38
34
  let app;
39
35
  before(() => {
40
36
  app = mm.app({
41
- baseDir: 'apps/foo'
37
+ baseDir: 'apps/foo',
42
38
  });
43
39
  return app.ready();
44
- })
40
+ });
45
41
  after(() => app.close());
46
42
 
47
43
  it('should request /', () => {
48
- return app.httpRequest()
49
- .get('/')
50
- .expect(200);
44
+ return app.httpRequest().get('/').expect(200);
51
45
  });
52
46
  });
53
47
  ```
@@ -154,9 +148,7 @@ describe('test/app.js', () => {
154
148
  after(() => app.close());
155
149
 
156
150
  it('some test', () => {
157
- return app.httpRequest()
158
- .get('/config')
159
- .expect(200)
151
+ return app.httpRequest().get('/config').expect(200);
160
152
  });
161
153
  });
162
154
  ```
@@ -213,7 +205,7 @@ The directory of application, default is `process.cwd()`.
213
205
  ```js
214
206
  mm.app({
215
207
  baseDir: path.join(__dirname, 'fixtures/apps/demo'),
216
- })
208
+ });
217
209
  ```
218
210
 
219
211
  You can use a string based on `$CWD/test/fixtures` for short
@@ -221,7 +213,7 @@ You can use a string based on `$CWD/test/fixtures` for short
221
213
  ```js
222
214
  mm.app({
223
215
  baseDir: 'apps/demo',
224
- })
216
+ });
225
217
  ```
226
218
 
227
219
  #### framework {String/Boolean}
@@ -232,7 +224,7 @@ The directory of framework
232
224
  mm.app({
233
225
  baseDir: 'apps/demo',
234
226
  framework: path.join(__dirname, 'fixtures/egg'),
235
- })
227
+ });
236
228
  ```
237
229
 
238
230
  It can be true when test an framework
@@ -244,7 +236,7 @@ The directory of plugin, it's detected automatically.
244
236
  ```js
245
237
  mm.app({
246
238
  baseDir: 'apps/demo',
247
- })
239
+ });
248
240
  ```
249
241
 
250
242
  #### plugins {Object}
@@ -270,10 +262,7 @@ Using `app.expectLog()` or `app.notExpectLog()` alone requires dependency on the
270
262
  ```js
271
263
  it('should work', async () => {
272
264
  app.mockLog();
273
- await app.httpRequest()
274
- .get('/')
275
- .expect('hello world')
276
- .expect(200);
265
+ await app.httpRequest().get('/').expect('hello world').expect(200);
277
266
 
278
267
  app.expectLog('foo in logger');
279
268
  app.expectLog('foo in coreLogger', 'coreLogger');
@@ -291,10 +280,7 @@ Request current app http server.
291
280
 
292
281
  ```js
293
282
  it('should work', () => {
294
- return app.httpRequest()
295
- .get('/')
296
- .expect('hello world')
297
- .expect(200);
283
+ return app.httpRequest().get('/').expect('hello world').expect(200);
298
284
  });
299
285
  ```
300
286
 
@@ -306,10 +292,7 @@ Assert current response not contains the specified header
306
292
 
307
293
  ```js
308
294
  it('should work', () => {
309
- return app.httpRequest()
310
- .get('/')
311
- .unexpectHeader('set-cookie')
312
- .expect(200);
295
+ return app.httpRequest().get('/').unexpectHeader('set-cookie').expect(200);
313
296
  });
314
297
  ```
315
298
 
@@ -319,10 +302,7 @@ Assert current response contains the specified header
319
302
 
320
303
  ```js
321
304
  it('should work', () => {
322
- return app.httpRequest()
323
- .get('/')
324
- .expectHeader('set-cookie')
325
- .expect(200);
305
+ return app.httpRequest().get('/').expectHeader('set-cookie').expect(200);
326
306
  });
327
307
  ```
328
308
 
@@ -331,8 +311,8 @@ it('should work', () => {
331
311
  ```js
332
312
  const ctx = app.mockContext({
333
313
  user: {
334
- name: 'Jason'
335
- }
314
+ name: 'Jason',
315
+ },
336
316
  });
337
317
  console.log(ctx.user.name); // Jason
338
318
  ```
@@ -340,20 +320,23 @@ console.log(ctx.user.name); // Jason
340
320
  ### app.mockContextScope(fn, options)
341
321
 
342
322
  ```js
343
- await app.mockContextScope(async ctx => {
344
- console.log(ctx.user.name); // Jason
345
- }, {
346
- user: {
347
- name: 'Jason'
323
+ await app.mockContextScope(
324
+ async ctx => {
325
+ console.log(ctx.user.name); // Jason
326
+ },
327
+ {
328
+ user: {
329
+ name: 'Jason',
330
+ },
348
331
  }
349
- });
332
+ );
350
333
  ```
351
334
 
352
335
  ### app.mockCookies(data)
353
336
 
354
337
  ```js
355
338
  app.mockCookies({
356
- foo: 'bar'
339
+ foo: 'bar',
357
340
  });
358
341
  const ctx = app.mockContext();
359
342
  console.log(ctx.getCookie('foo'));
@@ -367,7 +350,7 @@ Mock request header
367
350
 
368
351
  ```js
369
352
  app.mockSession({
370
- foo: 'bar'
353
+ foo: 'bar',
371
354
  });
372
355
  const ctx = app.mockContext();
373
356
  console.log(ctx.session.foo);
@@ -398,9 +381,7 @@ app.mockServiceError('user', 'home', new Error('mock error'));
398
381
  ```js
399
382
  app.mockCsrf();
400
383
 
401
- return app.httpRequest()
402
- .post('/login')
403
- .expect(302);
384
+ return app.httpRequest().post('/login').expect(302);
404
385
  ```
405
386
 
406
387
  ### app.mockHttpclient(url, method, data)
@@ -408,7 +389,7 @@ return app.httpRequest()
408
389
  Mock httpclient request, e.g.: `ctx.curl`
409
390
 
410
391
  ```js
411
- app.get('/', async function() {
392
+ app.get('/', async function () {
412
393
  const ret = await this.curl('https://eggjs.org');
413
394
  this.body = ret.data.toString();
414
395
  });
@@ -425,9 +406,7 @@ app.mockHttpclient('https://eggjs.org', {
425
406
  // app.mockHttpclient('https://eggjs.org', mockResponse); // mock all methods by default
426
407
  // app.mockHttpclient('https://eggjs.org', 'get', function(url, opt) { return 'xxx' }); // support fn
427
408
 
428
- return app.httpRequest()
429
- .post('/')
430
- .expect('mock egg');
409
+ return app.httpRequest().post('/').expect('mock egg');
431
410
  ```
432
411
 
433
412
  You can also use Regular Expression for matching url.
@@ -461,7 +440,8 @@ describe('test app', () => {
461
440
  it('should request success', () => {
462
441
  // mock data will be restored each case
463
442
  mock.data(app, 'method', { foo: 'bar' });
464
- return app.httpRequest()
443
+ return app
444
+ .httpRequest()
465
445
  .get('/foo')
466
446
  .expect(res => {
467
447
  assert(!res.headers.foo);
@@ -471,7 +451,7 @@ describe('test app', () => {
471
451
  });
472
452
 
473
453
  describe('test ctx', () => {
474
- it('can use ctx', async function() {
454
+ it('can use ctx', async function () {
475
455
  const res = await this.ctx.service.foo();
476
456
  assert(res === 'foo');
477
457
  });
@@ -512,17 +492,16 @@ then egg-mock will inject ctx for each test case.
512
492
  const mm = require('@eggjs/mock');
513
493
  const path = require('path');
514
494
 
515
- before(async function() {
516
- const app = this.app = mm.app();
495
+ before(async function () {
496
+ const app = (this.app = mm.app());
517
497
  mm.setGetAppCallback(() => {
518
498
  return app;
519
499
  });
520
500
  await app.ready();
521
501
  });
522
502
 
523
-
524
503
  // test/index.test.js
525
- it('should work', function() {
504
+ it('should work', function () {
526
505
  // eslint-disable-next-line no-undef
527
506
  assert(this.app.currentContext);
528
507
  });
@@ -543,6 +522,6 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
543
522
 
544
523
  ## Contributors
545
524
 
546
- [![Contributors](https://contrib.rocks/image?repo=eggjs/mock)](https://github.com/eggjs/mock/graphs/contributors)
525
+ [![Contributors](https://contrib.rocks/image?repo=eggjs/egg)](https://github.com/eggjs/egg/graphs/contributors)
547
526
 
548
527
  Made with [contributors-img](https://contrib.rocks).
package/README.zh_CN.md CHANGED
@@ -1,16 +1,12 @@
1
1
  # @eggjs/mock
2
2
 
3
3
  [![NPM version][npm-image]][npm-url]
4
- [![Node.js CI](https://github.com/eggjs/mock/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/mock/actions/workflows/nodejs.yml)
5
- [![Test coverage][codecov-image]][codecov-url]
6
4
  [![npm download][download-image]][download-url]
7
5
  [![Node.js Version](https://img.shields.io/node/v/@eggjs/mock.svg?style=flat)](https://nodejs.org/en/download/)
8
6
  [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
9
7
 
10
8
  [npm-image]: https://img.shields.io/npm/v/@eggjs/mock.svg?style=flat-square
11
9
  [npm-url]: https://npmjs.org/package/@eggjs/mock
12
- [codecov-image]: https://codecov.io/github/eggjs/mock/coverage.svg?branch=master
13
- [codecov-url]: https://codecov.io/github/eggjs/mock?branch=master
14
10
  [download-image]: https://img.shields.io/npm/dm/@eggjs/mock.svg?style=flat-square
15
11
  [download-url]: https://npmjs.org/package/@eggjs/mock
16
12
 
@@ -38,16 +34,14 @@ describe('some test', () => {
38
34
  let app;
39
35
  before(() => {
40
36
  app = mm.app({
41
- baseDir: 'apps/foo'
37
+ baseDir: 'apps/foo',
42
38
  });
43
39
  return app.ready();
44
- })
40
+ });
45
41
  after(() => app.close());
46
42
 
47
43
  it('should request /', () => {
48
- return app.httpRequest()
49
- .get('/')
50
- .expect(200);
44
+ return app.httpRequest().get('/').expect(200);
51
45
  });
52
46
  });
53
47
  ```
@@ -156,9 +150,7 @@ describe('test/app.js', () => {
156
150
  after(() => app.close());
157
151
 
158
152
  it('some test', () => {
159
- return app.httpRequest()
160
- .get('/config')
161
- .expect(200)
153
+ return app.httpRequest().get('/config').expect(200);
162
154
  });
163
155
  });
164
156
  ```
@@ -249,7 +241,7 @@ mm.app({
249
241
  mm.app({
250
242
  baseDir: 'apps/demo',
251
243
  plugin: true,
252
- })
244
+ });
253
245
  ```
254
246
 
255
247
  #### plugins {Object}
@@ -278,10 +270,7 @@ mm.app({
278
270
  it('should work', async () => {
279
271
  // 将日志记录到内存,用于下面的 expectLog
280
272
  app.mockLog();
281
- await app.httpRequest()
282
- .get('/')
283
- .expect('hello world')
284
- .expect(200);
273
+ await app.httpRequest().get('/').expect('hello world').expect(200);
285
274
 
286
275
  app.expectLog('foo in logger');
287
276
  app.expectLog('foo in coreLogger', 'coreLogger');
@@ -299,10 +288,7 @@ it('should work', async () => {
299
288
 
300
289
  ```js
301
290
  it('should work', () => {
302
- return app.httpRequest()
303
- .get('/')
304
- .expect('hello world')
305
- .expect(200);
291
+ return app.httpRequest().get('/').expect('hello world').expect(200);
306
292
  });
307
293
  ```
308
294
 
@@ -314,10 +300,7 @@ it('should work', () => {
314
300
 
315
301
  ```js
316
302
  it('should work', () => {
317
- return app.httpRequest()
318
- .get('/')
319
- .unexpectHeader('set-cookie')
320
- .expect(200);
303
+ return app.httpRequest().get('/').unexpectHeader('set-cookie').expect(200);
321
304
  });
322
305
  ```
323
306
 
@@ -327,10 +310,7 @@ it('should work', () => {
327
310
 
328
311
  ```js
329
312
  it('should work', () => {
330
- return app.httpRequest()
331
- .get('/')
332
- .expectHeader('set-cookie')
333
- .expect(200);
313
+ return app.httpRequest().get('/').expectHeader('set-cookie').expect(200);
334
314
  });
335
315
  ```
336
316
 
@@ -341,8 +321,8 @@ it('should work', () => {
341
321
  ```js
342
322
  const ctx = app.mockContext({
343
323
  user: {
344
- name: 'Jason'
345
- }
324
+ name: 'Jason',
325
+ },
346
326
  });
347
327
  console.log(ctx.user.name); // Jason
348
328
  ```
@@ -352,20 +332,23 @@ console.log(ctx.user.name); // Jason
352
332
  安全的模拟上下文数据,同一用例用多次调用 mockContext 可能会造成 AsyncLocalStorage 污染
353
333
 
354
334
  ```js
355
- await app.mockContextScope(async ctx => {
356
- console.log(ctx.user.name); // Jason
357
- }, {
358
- user: {
359
- name: 'Jason'
335
+ await app.mockContextScope(
336
+ async ctx => {
337
+ console.log(ctx.user.name); // Jason
338
+ },
339
+ {
340
+ user: {
341
+ name: 'Jason',
342
+ },
360
343
  }
361
- });
344
+ );
362
345
  ```
363
346
 
364
347
  ### app.mockCookies(data)
365
348
 
366
349
  ```js
367
350
  app.mockCookies({
368
- foo: 'bar'
351
+ foo: 'bar',
369
352
  });
370
353
  const ctx = app.mockContext();
371
354
  console.log(ctx.getCookie('foo'));
@@ -379,7 +362,7 @@ console.log(ctx.getCookie('foo'));
379
362
 
380
363
  ```js
381
364
  app.mockSession({
382
- foo: 'bar'
365
+ foo: 'bar',
383
366
  });
384
367
  const ctx = app.mockContext();
385
368
  console.log(ctx.session.foo);
@@ -388,8 +371,8 @@ console.log(ctx.session.foo);
388
371
  ### app.mockService(service, methodName, fn)
389
372
 
390
373
  ```js
391
- it('should mock user name', async function() {
392
- app.mockService('user', 'getName', async function(ctx, methodName, args) {
374
+ it('should mock user name', async function () {
375
+ app.mockService('user', 'getName', async function (ctx, methodName, args) {
393
376
  return 'popomore';
394
377
  });
395
378
  const ctx = app.mockContext();
@@ -412,9 +395,7 @@ app.mockServiceError('user', 'home', new Error('mock error'));
412
395
  ```js
413
396
  app.mockCsrf();
414
397
 
415
- return app.httpRequest()
416
- .post('/login')
417
- .expect(302);
398
+ return app.httpRequest().post('/login').expect(302);
418
399
  ```
419
400
 
420
401
  ### app.mockHttpclient(url, method, data)
@@ -434,9 +415,7 @@ app.mockHttpclient('https://eggjs.org', {
434
415
  data: 'mock egg',
435
416
  });
436
417
 
437
- return app.httpRequest()
438
- .post('/')
439
- .expect('mock egg');
418
+ return app.httpRequest().post('/').expect('mock egg');
440
419
  ```
441
420
 
442
421
  ## Bootstrap
@@ -450,7 +429,8 @@ describe('test app', () => {
450
429
  it('should request success', () => {
451
430
  // mock data will be restored each case
452
431
  mock.data(app, 'method', { foo: 'bar' });
453
- return app.httpRequest()
432
+ return app
433
+ .httpRequest()
454
434
  .get('/foo')
455
435
  .expect(res => {
456
436
  assert(!res.headers.foo);
@@ -460,7 +440,7 @@ describe('test app', () => {
460
440
  });
461
441
 
462
442
  describe('test ctx', () => {
463
- it('can use ctx', async function() {
443
+ it('can use ctx', async function () {
464
444
  const res = await this.ctx.service.foo();
465
445
  assert(res === 'foo');
466
446
  });
@@ -509,6 +489,6 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
509
489
 
510
490
  ## Contributors
511
491
 
512
- [![Contributors](https://contrib.rocks/image?repo=eggjs/mock)](https://github.com/eggjs/mock/graphs/contributors)
492
+ [![Contributors](https://contrib.rocks/image?repo=eggjs/egg)](https://github.com/eggjs/egg/graphs/contributors)
513
493
 
514
494
  Made with [contributors-img](https://contrib.rocks).
@@ -0,0 +1,7 @@
1
+ import { createRequire } from "node:module";
2
+
3
+ //#region rolldown:runtime
4
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
5
+
6
+ //#endregion
7
+ export { __require };
@@ -0,0 +1,39 @@
1
+ import { MockHttpClientMethod, MockResultFunction, MockResultOptions } from "../../lib/mock_httpclient.js";
2
+ import { EggCore } from "@eggjs/core";
3
+ import { mock, restore } from "mm";
4
+ import * as urllib0 from "urllib";
5
+
6
+ //#region src/app/extend/agent.d.ts
7
+ declare abstract class AgentUnittest extends EggCore {
8
+ [key: string]: any;
9
+ _mockHttpClient: MockHttpClientMethod;
10
+ /**
11
+ * mock httpclient
12
+ * @alias mockHttpClient
13
+ * @function App#mockHttpclient
14
+ */
15
+ mockHttpclient(mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string): void;
16
+ /**
17
+ * mock httpclient
18
+ * @function App#mockHttpClient
19
+ */
20
+ mockHttpClient(mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string): void;
21
+ /**
22
+ * get mock httpclient agent
23
+ * @function Agent#mockHttpclientAgent
24
+ */
25
+ mockAgent(): urllib0.MockAgent<urllib0.MockAgent.Options>;
26
+ mockAgentRestore(): Promise<void>;
27
+ /**
28
+ * @see mm#restore
29
+ * @function Agent#mockRestore
30
+ */
31
+ mockRestore: typeof restore;
32
+ /**
33
+ * @see mm
34
+ * @function Agent#mm
35
+ */
36
+ mm: typeof mock;
37
+ }
38
+ //#endregion
39
+ export { AgentUnittest as default };
@@ -0,0 +1,48 @@
1
+ import { getMockAgent, restoreMockAgent } from "../../lib/mock_agent.js";
2
+ import { createMockHttpClient } from "../../lib/mock_httpclient.js";
3
+ import { EggCore } from "@eggjs/core";
4
+ import { mock, restore } from "mm";
5
+
6
+ //#region src/app/extend/agent.ts
7
+ var AgentUnittest = class extends EggCore {
8
+ _mockHttpClient;
9
+ /**
10
+ * mock httpclient
11
+ * @alias mockHttpClient
12
+ * @function App#mockHttpclient
13
+ */
14
+ mockHttpclient(mockUrl, mockMethod, mockResult) {
15
+ return this.mockHttpClient(mockUrl, mockMethod, mockResult);
16
+ }
17
+ /**
18
+ * mock httpclient
19
+ * @function App#mockHttpClient
20
+ */
21
+ mockHttpClient(mockUrl, mockMethod, mockResult) {
22
+ if (!this._mockHttpClient) this._mockHttpClient = createMockHttpClient(this);
23
+ return this._mockHttpClient(mockUrl, mockMethod, mockResult);
24
+ }
25
+ /**
26
+ * get mock httpclient agent
27
+ * @function Agent#mockHttpclientAgent
28
+ */
29
+ mockAgent() {
30
+ return getMockAgent(this);
31
+ }
32
+ async mockAgentRestore() {
33
+ await restoreMockAgent();
34
+ }
35
+ /**
36
+ * @see mm#restore
37
+ * @function Agent#mockRestore
38
+ */
39
+ mockRestore = restore;
40
+ /**
41
+ * @see mm
42
+ * @function Agent#mm
43
+ */
44
+ mm = mock;
45
+ };
46
+
47
+ //#endregion
48
+ export { AgentUnittest as default };