@eggjs/mock 6.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +547 -0
- package/README.zh_CN.md +512 -0
- package/dist/commonjs/app/extend/agent.d.ts +33 -0
- package/dist/commonjs/app/extend/agent.js +49 -0
- package/dist/commonjs/app/extend/application.d.ts +175 -0
- package/dist/commonjs/app/extend/application.js +448 -0
- package/dist/commonjs/app/middleware/cluster_app_mock.d.ts +3 -0
- package/dist/commonjs/app/middleware/cluster_app_mock.js +100 -0
- package/dist/commonjs/app.d.ts +6 -0
- package/dist/commonjs/app.js +20 -0
- package/dist/commonjs/bootstrap.d.ts +4 -0
- package/dist/commonjs/bootstrap.js +58 -0
- package/dist/commonjs/index.d.ts +76 -0
- package/dist/commonjs/index.js +99 -0
- package/dist/commonjs/index.test-d.d.ts +1 -0
- package/dist/commonjs/index.test-d.js +43 -0
- package/dist/commonjs/lib/agent_handler.d.ts +3 -0
- package/dist/commonjs/lib/agent_handler.js +28 -0
- package/dist/commonjs/lib/app.d.ts +28 -0
- package/dist/commonjs/lib/app.js +303 -0
- package/dist/commonjs/lib/app_handler.d.ts +5 -0
- package/dist/commonjs/lib/app_handler.js +67 -0
- package/dist/commonjs/lib/cluster.d.ts +114 -0
- package/dist/commonjs/lib/cluster.js +337 -0
- package/dist/commonjs/lib/context.d.ts +1 -0
- package/dist/commonjs/lib/context.js +16 -0
- package/dist/commonjs/lib/format_options.d.ts +5 -0
- package/dist/commonjs/lib/format_options.js +100 -0
- package/dist/commonjs/lib/inject_context.d.ts +6 -0
- package/dist/commonjs/lib/inject_context.js +132 -0
- package/dist/commonjs/lib/mock_agent.d.ts +5 -0
- package/dist/commonjs/lib/mock_agent.js +49 -0
- package/dist/commonjs/lib/mock_custom_loader.d.ts +1 -0
- package/dist/commonjs/lib/mock_custom_loader.js +37 -0
- package/dist/commonjs/lib/mock_http_server.d.ts +2 -0
- package/dist/commonjs/lib/mock_http_server.js +24 -0
- package/dist/commonjs/lib/mock_httpclient.d.ts +35 -0
- package/dist/commonjs/lib/mock_httpclient.js +147 -0
- package/dist/commonjs/lib/parallel/agent.d.ts +20 -0
- package/dist/commonjs/lib/parallel/agent.js +125 -0
- package/dist/commonjs/lib/parallel/app.d.ts +20 -0
- package/dist/commonjs/lib/parallel/app.js +115 -0
- package/dist/commonjs/lib/parallel/util.d.ts +3 -0
- package/dist/commonjs/lib/parallel/util.js +77 -0
- package/dist/commonjs/lib/prerequire.d.ts +1 -0
- package/dist/commonjs/lib/prerequire.js +26 -0
- package/dist/commonjs/lib/request_call_function.d.ts +1 -0
- package/dist/commonjs/lib/request_call_function.js +52 -0
- package/dist/commonjs/lib/restore.d.ts +1 -0
- package/dist/commonjs/lib/restore.js +16 -0
- package/dist/commonjs/lib/start-cluster.d.ts +2 -0
- package/dist/commonjs/lib/start-cluster.js +23 -0
- package/dist/commonjs/lib/supertest.d.ts +11 -0
- package/dist/commonjs/lib/supertest.js +48 -0
- package/dist/commonjs/lib/tmp/empty.d.ts +1 -0
- package/dist/commonjs/lib/tmp/empty.js +3 -0
- package/dist/commonjs/lib/types.d.ts +60 -0
- package/dist/commonjs/lib/types.js +3 -0
- package/dist/commonjs/lib/utils.d.ts +9 -0
- package/dist/commonjs/lib/utils.js +80 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/register.d.ts +8 -0
- package/dist/commonjs/register.js +80 -0
- package/dist/esm/app/extend/agent.d.ts +33 -0
- package/dist/esm/app/extend/agent.js +46 -0
- package/dist/esm/app/extend/application.d.ts +175 -0
- package/dist/esm/app/extend/application.js +442 -0
- package/dist/esm/app/middleware/cluster_app_mock.d.ts +3 -0
- package/dist/esm/app/middleware/cluster_app_mock.js +98 -0
- package/dist/esm/app.d.ts +6 -0
- package/dist/esm/app.js +17 -0
- package/dist/esm/bootstrap.d.ts +4 -0
- package/dist/esm/bootstrap.js +16 -0
- package/dist/esm/index.d.ts +76 -0
- package/dist/esm/index.js +90 -0
- package/dist/esm/index.test-d.d.ts +1 -0
- package/dist/esm/index.test-d.js +42 -0
- package/dist/esm/lib/agent_handler.d.ts +3 -0
- package/dist/esm/lib/agent_handler.js +24 -0
- package/dist/esm/lib/app.d.ts +28 -0
- package/dist/esm/lib/app.js +295 -0
- package/dist/esm/lib/app_handler.d.ts +5 -0
- package/dist/esm/lib/app_handler.js +61 -0
- package/dist/esm/lib/cluster.d.ts +114 -0
- package/dist/esm/lib/cluster.js +328 -0
- package/dist/esm/lib/context.d.ts +1 -0
- package/dist/esm/lib/context.js +13 -0
- package/dist/esm/lib/format_options.d.ts +5 -0
- package/dist/esm/lib/format_options.js +94 -0
- package/dist/esm/lib/inject_context.d.ts +6 -0
- package/dist/esm/lib/inject_context.js +126 -0
- package/dist/esm/lib/mock_agent.d.ts +5 -0
- package/dist/esm/lib/mock_agent.js +45 -0
- package/dist/esm/lib/mock_custom_loader.d.ts +1 -0
- package/dist/esm/lib/mock_custom_loader.js +34 -0
- package/dist/esm/lib/mock_http_server.d.ts +2 -0
- package/dist/esm/lib/mock_http_server.js +18 -0
- package/dist/esm/lib/mock_httpclient.d.ts +35 -0
- package/dist/esm/lib/mock_httpclient.js +144 -0
- package/dist/esm/lib/parallel/agent.d.ts +20 -0
- package/dist/esm/lib/parallel/agent.js +117 -0
- package/dist/esm/lib/parallel/app.d.ts +20 -0
- package/dist/esm/lib/parallel/app.js +110 -0
- package/dist/esm/lib/parallel/util.d.ts +3 -0
- package/dist/esm/lib/parallel/util.js +73 -0
- package/dist/esm/lib/prerequire.d.ts +1 -0
- package/dist/esm/lib/prerequire.js +25 -0
- package/dist/esm/lib/request_call_function.d.ts +1 -0
- package/dist/esm/lib/request_call_function.js +47 -0
- package/dist/esm/lib/restore.d.ts +1 -0
- package/dist/esm/lib/restore.js +13 -0
- package/dist/esm/lib/start-cluster.d.ts +2 -0
- package/dist/esm/lib/start-cluster.js +18 -0
- package/dist/esm/lib/supertest.d.ts +11 -0
- package/dist/esm/lib/supertest.js +40 -0
- package/dist/esm/lib/tmp/empty.d.ts +1 -0
- package/dist/esm/lib/tmp/empty.js +2 -0
- package/dist/esm/lib/types.d.ts +60 -0
- package/dist/esm/lib/types.js +2 -0
- package/dist/esm/lib/utils.d.ts +9 -0
- package/dist/esm/lib/utils.js +69 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/register.d.ts +8 -0
- package/dist/esm/register.js +75 -0
- package/dist/package.json +4 -0
- package/package.json +131 -0
- package/src/app/extend/agent.ts +56 -0
- package/src/app/extend/application.ts +512 -0
- package/src/app/middleware/cluster_app_mock.ts +101 -0
- package/src/app.ts +18 -0
- package/src/bootstrap.ts +25 -0
- package/src/index.d.ts +193 -0
- package/src/index.test-d.ts +47 -0
- package/src/index.ts +110 -0
- package/src/lib/agent_handler.ts +28 -0
- package/src/lib/app.ts +313 -0
- package/src/lib/app_handler.ts +69 -0
- package/src/lib/cluster.ts +363 -0
- package/src/lib/context.ts +14 -0
- package/src/lib/format_options.ts +103 -0
- package/src/lib/inject_context.ts +134 -0
- package/src/lib/mock_agent.ts +57 -0
- package/src/lib/mock_custom_loader.ts +36 -0
- package/src/lib/mock_http_server.ts +19 -0
- package/src/lib/mock_httpclient.ts +181 -0
- package/src/lib/parallel/agent.ts +128 -0
- package/src/lib/parallel/app.ts +123 -0
- package/src/lib/parallel/util.ts +66 -0
- package/src/lib/prerequire.ts +25 -0
- package/src/lib/request_call_function.ts +49 -0
- package/src/lib/restore.ts +14 -0
- package/src/lib/start-cluster.ts +23 -0
- package/src/lib/supertest.ts +45 -0
- package/src/lib/tmp/.gitkeep +0 -0
- package/src/lib/tmp/empty.ts +0 -0
- package/src/lib/types.ts +72 -0
- package/src/lib/utils.ts +82 -0
- package/src/register.ts +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-present Alibaba Group Holding Limited and other contributors.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
# @eggjs/mock
|
|
2
|
+
|
|
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
|
+
[![npm download][download-image]][download-url]
|
|
7
|
+
[](https://nodejs.org/en/download/)
|
|
8
|
+
|
|
9
|
+
[npm-image]: https://img.shields.io/npm/v/@eggjs/mock.svg?style=flat-square
|
|
10
|
+
[npm-url]: https://npmjs.org/package/@eggjs/mock
|
|
11
|
+
[codecov-image]: https://codecov.io/github/eggjs/mock/coverage.svg?branch=master
|
|
12
|
+
[codecov-url]: https://codecov.io/github/eggjs/mock?branch=master
|
|
13
|
+
[download-image]: https://img.shields.io/npm/dm/@eggjs/mock.svg?style=flat-square
|
|
14
|
+
[download-url]: https://npmjs.org/package/@eggjs/mock
|
|
15
|
+
|
|
16
|
+
Mock library for testing Egg applications, plugins and custom Egg frameworks with ease.
|
|
17
|
+
`egg-mock` inherits all APIs from [node_modules/mm](https://github.com/node-modules/mm), offering more flexibility.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm i egg-mock --save-dev
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### Create testcase
|
|
28
|
+
|
|
29
|
+
Launch a mock server with `mm.app`
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
// test/index.test.js
|
|
33
|
+
const path = require('node:path');
|
|
34
|
+
const mm = require('@eggjs/mock');
|
|
35
|
+
|
|
36
|
+
describe('some test', () => {
|
|
37
|
+
let app;
|
|
38
|
+
before(() => {
|
|
39
|
+
app = mm.app({
|
|
40
|
+
baseDir: 'apps/foo'
|
|
41
|
+
});
|
|
42
|
+
return app.ready();
|
|
43
|
+
})
|
|
44
|
+
after(() => app.close());
|
|
45
|
+
|
|
46
|
+
it('should request /', () => {
|
|
47
|
+
return app.httpRequest()
|
|
48
|
+
.get('/')
|
|
49
|
+
.expect(200);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Retrieve Agent instance through `app.agent` after `mm.app` started.
|
|
55
|
+
|
|
56
|
+
Using `mm.cluster` launch cluster server, you can use the same API as `mm.app`;
|
|
57
|
+
|
|
58
|
+
### Test Application
|
|
59
|
+
|
|
60
|
+
`baseDir` is optional that is `process.cwd()` by default.
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
before(() => {
|
|
64
|
+
app = mm.app();
|
|
65
|
+
return app.ready();
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Test Framework
|
|
70
|
+
|
|
71
|
+
framework is optional, it's `node_modules/egg` by default.
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
before(() => {
|
|
75
|
+
app = mm.app({
|
|
76
|
+
baseDir: 'apps/demo',
|
|
77
|
+
framework: true,
|
|
78
|
+
});
|
|
79
|
+
return app.ready();
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Test Plugin
|
|
84
|
+
|
|
85
|
+
If `eggPlugin.name` is defined in `package.json`, it's a plugin that will be loaded to plugin list automatically.
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
before(() => {
|
|
89
|
+
app = mm.app({
|
|
90
|
+
baseDir: 'apps/demo',
|
|
91
|
+
});
|
|
92
|
+
return app.ready();
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
You can also test the plugin in different framework, e.g. test [aliyun-egg](https://github.com/eggjs/aliyun-egg) and framework-b in one plugin.
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
describe('aliyun-egg', () => {
|
|
100
|
+
let app;
|
|
101
|
+
before(() => {
|
|
102
|
+
app = mm.app({
|
|
103
|
+
baseDir: 'apps/demo',
|
|
104
|
+
framework: path.join(__dirname, 'node_modules/aliyun-egg'),
|
|
105
|
+
});
|
|
106
|
+
return app.ready();
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('framework-b', () => {
|
|
111
|
+
let app;
|
|
112
|
+
before(() => {
|
|
113
|
+
app = mm.app({
|
|
114
|
+
baseDir: 'apps/demo',
|
|
115
|
+
framework: path.join(__dirname, 'node_modules/framework-b'),
|
|
116
|
+
});
|
|
117
|
+
return app.ready();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
If it's detected as an plugin, but you don't want it to be, you can use `plugin = false`.
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
before(() => {
|
|
126
|
+
app = mm.app({
|
|
127
|
+
baseDir: 'apps/demo',
|
|
128
|
+
plugin: false,
|
|
129
|
+
});
|
|
130
|
+
return app.ready();
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## API
|
|
135
|
+
|
|
136
|
+
### mm.app(options)
|
|
137
|
+
|
|
138
|
+
Create a mock application.
|
|
139
|
+
|
|
140
|
+
### mm.cluster(options)
|
|
141
|
+
|
|
142
|
+
Create a mock cluster server, but you can't use API in application, you should test using `supertest`.
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
const mm = require('@eggjs/mock');
|
|
146
|
+
|
|
147
|
+
describe('test/app.js', () => {
|
|
148
|
+
let app, config;
|
|
149
|
+
before(() => {
|
|
150
|
+
app = mm.cluster();
|
|
151
|
+
return app.ready();
|
|
152
|
+
});
|
|
153
|
+
after(() => app.close());
|
|
154
|
+
|
|
155
|
+
it('some test', () => {
|
|
156
|
+
return app.httpRequest()
|
|
157
|
+
.get('/config')
|
|
158
|
+
.expect(200)
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
You can disable coverage, because it's slow.
|
|
164
|
+
|
|
165
|
+
```js
|
|
166
|
+
mm.cluster({
|
|
167
|
+
coverage: false,
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### mm.env(env)
|
|
172
|
+
|
|
173
|
+
Mock env when starting
|
|
174
|
+
|
|
175
|
+
```js
|
|
176
|
+
// production environment
|
|
177
|
+
mm.env('prod');
|
|
178
|
+
mm.app({
|
|
179
|
+
cache: false,
|
|
180
|
+
});
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Environment list <https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L82>
|
|
184
|
+
|
|
185
|
+
### mm.consoleLevel(level)
|
|
186
|
+
|
|
187
|
+
Mock level that print to stdout/stderr
|
|
188
|
+
|
|
189
|
+
```js
|
|
190
|
+
// DON'T log to terminal
|
|
191
|
+
mm.consoleLevel('NONE');
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
level list: `DEBUG`, `INFO`, `WARN`, `ERROR`, `NONE`
|
|
195
|
+
|
|
196
|
+
### mm.home(homePath)
|
|
197
|
+
|
|
198
|
+
mock home directory
|
|
199
|
+
|
|
200
|
+
### mm.restore()
|
|
201
|
+
|
|
202
|
+
restore all mock data, e.g. `afterEach(mm.restore)`
|
|
203
|
+
|
|
204
|
+
### options
|
|
205
|
+
|
|
206
|
+
Options for `mm.app` and `mm.cluster`
|
|
207
|
+
|
|
208
|
+
#### baseDir {String}
|
|
209
|
+
|
|
210
|
+
The directory of application, default is `process.cwd()`.
|
|
211
|
+
|
|
212
|
+
```js
|
|
213
|
+
mm.app({
|
|
214
|
+
baseDir: path.join(__dirname, 'fixtures/apps/demo'),
|
|
215
|
+
})
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
You can use a string based on `$CWD/test/fixtures` for short
|
|
219
|
+
|
|
220
|
+
```js
|
|
221
|
+
mm.app({
|
|
222
|
+
baseDir: 'apps/demo',
|
|
223
|
+
})
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
#### framework {String/Boolean}
|
|
227
|
+
|
|
228
|
+
The directory of framework
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
mm.app({
|
|
232
|
+
baseDir: 'apps/demo',
|
|
233
|
+
framework: path.join(__dirname, 'fixtures/egg'),
|
|
234
|
+
})
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
It can be true when test an framework
|
|
238
|
+
|
|
239
|
+
#### plugin
|
|
240
|
+
|
|
241
|
+
The directory of plugin, it's detected automatically.
|
|
242
|
+
|
|
243
|
+
```js
|
|
244
|
+
mm.app({
|
|
245
|
+
baseDir: 'apps/demo',
|
|
246
|
+
})
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
#### plugins {Object}
|
|
250
|
+
|
|
251
|
+
Define a list of plugins
|
|
252
|
+
|
|
253
|
+
#### cache {Boolean}
|
|
254
|
+
|
|
255
|
+
Determine whether enable cache. it's cached by baseDir.
|
|
256
|
+
|
|
257
|
+
#### clean {Boolean}
|
|
258
|
+
|
|
259
|
+
Clean all logs directory, default is true.
|
|
260
|
+
|
|
261
|
+
If you are using `ava`, disable it.
|
|
262
|
+
|
|
263
|
+
### app.mockLog([logger]) and app.expectLog(str[, logger]), app.notExpectLog(str[, logger])
|
|
264
|
+
|
|
265
|
+
Assert some string value in the logger instance.
|
|
266
|
+
It is recommended to pair `app.mockLog()` with `app.expectLog()` or `app.notExpectLog()`.
|
|
267
|
+
Using `app.expectLog()` or `app.notExpectLog()` alone requires dependency on the write speed of the log. When the server disk is high IO, unstable results will occur.
|
|
268
|
+
|
|
269
|
+
```js
|
|
270
|
+
it('should work', async () => {
|
|
271
|
+
app.mockLog();
|
|
272
|
+
await app.httpRequest()
|
|
273
|
+
.get('/')
|
|
274
|
+
.expect('hello world')
|
|
275
|
+
.expect(200);
|
|
276
|
+
|
|
277
|
+
app.expectLog('foo in logger');
|
|
278
|
+
app.expectLog('foo in coreLogger', 'coreLogger');
|
|
279
|
+
app.expectLog('foo in myCustomLogger', 'myCustomLogger');
|
|
280
|
+
|
|
281
|
+
app.notExpectLog('bar in logger');
|
|
282
|
+
app.notExpectLog('bar in coreLogger', 'coreLogger');
|
|
283
|
+
app.notExpectLog('bar in myCustomLogger', 'myCustomLogger');
|
|
284
|
+
});
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### app.httpRequest()
|
|
288
|
+
|
|
289
|
+
Request current app http server.
|
|
290
|
+
|
|
291
|
+
```js
|
|
292
|
+
it('should work', () => {
|
|
293
|
+
return app.httpRequest()
|
|
294
|
+
.get('/')
|
|
295
|
+
.expect('hello world')
|
|
296
|
+
.expect(200);
|
|
297
|
+
});
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
See [supertest](https://github.com/visionmedia/supertest) to get more APIs.
|
|
301
|
+
|
|
302
|
+
#### .unexpectHeader(name)
|
|
303
|
+
|
|
304
|
+
Assert current response not contains the specified header
|
|
305
|
+
|
|
306
|
+
```js
|
|
307
|
+
it('should work', () => {
|
|
308
|
+
return app.httpRequest()
|
|
309
|
+
.get('/')
|
|
310
|
+
.unexpectHeader('set-cookie')
|
|
311
|
+
.expect(200);
|
|
312
|
+
});
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
#### .expectHeader(name)
|
|
316
|
+
|
|
317
|
+
Assert current response contains the specified header
|
|
318
|
+
|
|
319
|
+
```js
|
|
320
|
+
it('should work', () => {
|
|
321
|
+
return app.httpRequest()
|
|
322
|
+
.get('/')
|
|
323
|
+
.expectHeader('set-cookie')
|
|
324
|
+
.expect(200);
|
|
325
|
+
});
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### app.mockContext(options)
|
|
329
|
+
|
|
330
|
+
```js
|
|
331
|
+
const ctx = app.mockContext({
|
|
332
|
+
user: {
|
|
333
|
+
name: 'Jason'
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
console.log(ctx.user.name); // Jason
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### app.mockContextScope(fn, options)
|
|
340
|
+
|
|
341
|
+
```js
|
|
342
|
+
await app.mockContextScope(async ctx => {
|
|
343
|
+
console.log(ctx.user.name); // Jason
|
|
344
|
+
}, {
|
|
345
|
+
user: {
|
|
346
|
+
name: 'Jason'
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
### app.mockCookies(data)
|
|
352
|
+
|
|
353
|
+
```js
|
|
354
|
+
app.mockCookies({
|
|
355
|
+
foo: 'bar'
|
|
356
|
+
});
|
|
357
|
+
const ctx = app.mockContext();
|
|
358
|
+
console.log(ctx.getCookie('foo'));
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### app.mockHeaders(data)
|
|
362
|
+
|
|
363
|
+
Mock request header
|
|
364
|
+
|
|
365
|
+
### app.mockSession(data)
|
|
366
|
+
|
|
367
|
+
```js
|
|
368
|
+
app.mockSession({
|
|
369
|
+
foo: 'bar'
|
|
370
|
+
});
|
|
371
|
+
const ctx = app.mockContext();
|
|
372
|
+
console.log(ctx.session.foo);
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### app.mockService(service, methodName, fn)
|
|
376
|
+
|
|
377
|
+
```js
|
|
378
|
+
it('should mock user name', async function () {
|
|
379
|
+
app.mockService('user', 'getName', async function (ctx, methodName, args) {
|
|
380
|
+
return 'popomore';
|
|
381
|
+
});
|
|
382
|
+
const ctx = app.mockContext();
|
|
383
|
+
await ctx.service.user.getName();
|
|
384
|
+
});
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### app.mockServiceError(service, methodName, error)
|
|
388
|
+
|
|
389
|
+
You can mock an error for service
|
|
390
|
+
|
|
391
|
+
```js
|
|
392
|
+
app.mockServiceError('user', 'home', new Error('mock error'));
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
### app.mockCsrf()
|
|
396
|
+
|
|
397
|
+
```js
|
|
398
|
+
app.mockCsrf();
|
|
399
|
+
|
|
400
|
+
return app.httpRequest()
|
|
401
|
+
.post('/login')
|
|
402
|
+
.expect(302);
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### app.mockHttpclient(url, method, data)
|
|
406
|
+
|
|
407
|
+
Mock httpclient request, e.g.: `ctx.curl`
|
|
408
|
+
|
|
409
|
+
```js
|
|
410
|
+
app.get('/', async function() {
|
|
411
|
+
const ret = await this.curl('https://eggjs.org');
|
|
412
|
+
this.body = ret.data.toString();
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
app.mockHttpclient('https://eggjs.org', {
|
|
416
|
+
// can be buffer / string / json / function
|
|
417
|
+
// will auto convert to buffer
|
|
418
|
+
// follow options.dataType to convert
|
|
419
|
+
data: 'mock egg',
|
|
420
|
+
});
|
|
421
|
+
// app.mockHttpclient('https://eggjs.org', 'get', mockResponse); // mock get
|
|
422
|
+
// app.mockHttpclient('https://eggjs.org', [ 'get' , 'head' ], mockResponse); // mock get and head
|
|
423
|
+
// app.mockHttpclient('https://eggjs.org', '*', mockResponse); // mock all methods
|
|
424
|
+
// app.mockHttpclient('https://eggjs.org', mockResponse); // mock all methods by default
|
|
425
|
+
// app.mockHttpclient('https://eggjs.org', 'get', function(url, opt) { return 'xxx' }); // support fn
|
|
426
|
+
|
|
427
|
+
return app.httpRequest()
|
|
428
|
+
.post('/')
|
|
429
|
+
.expect('mock egg');
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
You can also use Regular Expression for matching url.
|
|
433
|
+
|
|
434
|
+
```js
|
|
435
|
+
app.mockHttpclient(/\/users\/[a-z]$/i, {
|
|
436
|
+
data: {
|
|
437
|
+
name: 'egg',
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
You can alse mock agent.httpclient
|
|
443
|
+
|
|
444
|
+
```js
|
|
445
|
+
app.agent.mockHttpclient('https://eggjs.org', {
|
|
446
|
+
data: {
|
|
447
|
+
name: 'egg',
|
|
448
|
+
},
|
|
449
|
+
});
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
## Bootstrap
|
|
453
|
+
|
|
454
|
+
We also provide a bootstrap file for applications' unit test to reduce duplicated code:
|
|
455
|
+
|
|
456
|
+
```js
|
|
457
|
+
const { app, mock, assert } = require('@eggjs/mock/bootstrap');
|
|
458
|
+
|
|
459
|
+
describe('test app', () => {
|
|
460
|
+
it('should request success', () => {
|
|
461
|
+
// mock data will be restored each case
|
|
462
|
+
mock.data(app, 'method', { foo: 'bar' });
|
|
463
|
+
return app.httpRequest()
|
|
464
|
+
.get('/foo')
|
|
465
|
+
.expect(res => {
|
|
466
|
+
assert(!res.headers.foo);
|
|
467
|
+
})
|
|
468
|
+
.expect(/bar/);
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
describe('test ctx', () => {
|
|
473
|
+
it('can use ctx', async function() {
|
|
474
|
+
const res = await this.ctx.service.foo();
|
|
475
|
+
assert(res === 'foo');
|
|
476
|
+
});
|
|
477
|
+
});
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
We inject ctx to every test case, so you can use `app.currentContext` in your test case.
|
|
481
|
+
and the first call of `app.mockContext` will reuse `app.currentContext`.
|
|
482
|
+
|
|
483
|
+
```js
|
|
484
|
+
const { app, mock, assert } = require('@eggjs/mock/bootstrap');
|
|
485
|
+
|
|
486
|
+
describe('test ctx', () => {
|
|
487
|
+
it('should can use ctx', () => {
|
|
488
|
+
const ctx = app.currentContext;
|
|
489
|
+
assert(ctx);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
it('should reuse ctx', () => {
|
|
493
|
+
const ctx = app.currentContext;
|
|
494
|
+
// first call will reuse app.currentContext
|
|
495
|
+
const mockCtx = app.mockContext();
|
|
496
|
+
assert(ctx === mockCtx);
|
|
497
|
+
// next call will create a new context
|
|
498
|
+
// multi call app.mockContext will get wrong context with app.currentContext
|
|
499
|
+
// so we recommend to use app.mockContextScope
|
|
500
|
+
const mockCtx2 = app.mockContext();
|
|
501
|
+
assert(ctx !== mockCtx);
|
|
502
|
+
});
|
|
503
|
+
});
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
And if you use mm.app to bootstrap app, you can manually call setGetAppCallback,
|
|
507
|
+
then egg-mock will inject ctx for each test case.
|
|
508
|
+
|
|
509
|
+
```js
|
|
510
|
+
// test/.setup.js
|
|
511
|
+
const mm = require('@eggjs/mock');
|
|
512
|
+
const path = require('path');
|
|
513
|
+
|
|
514
|
+
before(async function() {
|
|
515
|
+
const app = this.app = mm.app();
|
|
516
|
+
mm.setGetAppCallback(() => {
|
|
517
|
+
return app;
|
|
518
|
+
});
|
|
519
|
+
await app.ready();
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
// test/index.test.js
|
|
524
|
+
it('should work', function() {
|
|
525
|
+
// eslint-disable-next-line no-undef
|
|
526
|
+
assert(this.app.currentContext);
|
|
527
|
+
});
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
### env for custom bootstrap
|
|
531
|
+
|
|
532
|
+
EGG_BASE_DIR: the base dir of egg app
|
|
533
|
+
EGG_FRAMEWORK: the framework of egg app
|
|
534
|
+
|
|
535
|
+
## Questions & Suggestions
|
|
536
|
+
|
|
537
|
+
Please open an issue [here](https://github.com/eggjs/egg/issues).
|
|
538
|
+
|
|
539
|
+
## License
|
|
540
|
+
|
|
541
|
+
[MIT](LICENSE)
|
|
542
|
+
|
|
543
|
+
## Contributors
|
|
544
|
+
|
|
545
|
+
[](https://github.com/eggjs/mock/graphs/contributors)
|
|
546
|
+
|
|
547
|
+
Made with [contributors-img](https://contrib.rocks).
|