@eggjs/mock 6.0.7 → 6.1.0-beta.11
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 +36 -57
- package/README.zh_CN.md +31 -51
- package/dist/_virtual/rolldown_runtime.js +7 -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 +34 -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 +10 -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/register.d.ts +10 -0
- package/dist/register.js +40 -0
- package/dist/typings/index.d.ts +1 -0
- package/package.json +69 -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 -172
- 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 -172
- 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 -512
- 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
package/README.md
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
# @eggjs/mock
|
|
2
2
|
|
|
3
3
|
[![NPM version][npm-image]][npm-url]
|
|
4
|
-
[](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
|
[](https://nodejs.org/en/download/)
|
|
8
6
|
[](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
|
|
|
@@ -20,7 +16,7 @@ Mock library for testing Egg applications, plugins and custom Egg frameworks wit
|
|
|
20
16
|
## Install
|
|
21
17
|
|
|
22
18
|
```bash
|
|
23
|
-
npm i
|
|
19
|
+
npm i @eggjs/mock --save-dev
|
|
24
20
|
```
|
|
25
21
|
|
|
26
22
|
## Usage
|
|
@@ -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(
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
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
|
|
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
|
-
[](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
|
-
[](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
|
[](https://nodejs.org/en/download/)
|
|
8
6
|
[](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
|
|
|
@@ -20,7 +16,7 @@
|
|
|
20
16
|
## Install
|
|
21
17
|
|
|
22
18
|
```bash
|
|
23
|
-
npm i
|
|
19
|
+
npm i @eggjs/mock --save-dev
|
|
24
20
|
```
|
|
25
21
|
|
|
26
22
|
## Usage
|
|
@@ -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(
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
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
|
|
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
|
-
[](https://github.com/eggjs/egg/graphs/contributors)
|
|
513
493
|
|
|
514
494
|
Made with [contributors-img](https://contrib.rocks).
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { MockHttpClientMethod, MockResultFunction, MockResultOptions } from "../../lib/mock_httpclient.js";
|
|
2
|
+
import { Agent } from "egg";
|
|
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 Agent {
|
|
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 { Agent } from "egg";
|
|
4
|
+
import { mock, restore } from "mm";
|
|
5
|
+
|
|
6
|
+
//#region src/app/extend/agent.ts
|
|
7
|
+
var AgentUnittest = class extends Agent {
|
|
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 };
|