@eggjs/skills 0.0.0 → 4.1.2-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/LICENSE +21 -0
- package/egg/SKILL.md +304 -0
- package/egg-controller/SKILL.md +105 -0
- package/egg-controller/references/ajv-validate.md +140 -0
- package/egg-controller/references/http-controller.md +464 -0
- package/egg-controller/references/mcp-controller.md +293 -0
- package/egg-controller/references/middleware.md +133 -0
- package/egg-controller/references/schedule.md +110 -0
- package/egg-core/SKILL.md +248 -0
- package/egg-core/references/aop.md +223 -0
- package/egg-core/references/background-task.md +121 -0
- package/egg-core/references/dynamic-inject.md +90 -0
- package/egg-core/references/eventbus.md +138 -0
- package/egg-core/references/inject.md +258 -0
- package/egg-core/references/module.md +202 -0
- package/egg-core/references/proto.md +181 -0
- package/egg-unittest/SKILL.md +149 -0
- package/egg-unittest/references/background-task-test.md +53 -0
- package/egg-unittest/references/eventbus-test.md +47 -0
- package/egg-unittest/references/http-test.md +145 -0
- package/egg-unittest/references/mock.md +108 -0
- package/egg-unittest/references/service-test.md +75 -0
- package/package.json +19 -2
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Service / DI 对象测试
|
|
2
|
+
|
|
3
|
+
## 常见错误
|
|
4
|
+
|
|
5
|
+
| 错误写法 | 正确写法 | 说明 |
|
|
6
|
+
| ------------------------ | ----------------------------------------- | ------------------- |
|
|
7
|
+
| `ctx.service.user.get()` | `ctx.getEggObject(UserService)` | 旧写法,新项目用 DI |
|
|
8
|
+
| 不 await `getEggObject` | `const svc = await ctx.getEggObject(Svc)` | 返回 Promise |
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Singleton 测试
|
|
13
|
+
|
|
14
|
+
`@SingletonProto` 对象直接通过 `app.getEggObject()` 获取:
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import assert from 'node:assert';
|
|
18
|
+
import { app } from '@eggjs/mock/bootstrap';
|
|
19
|
+
import { ConfigService } from '../app/modules/foo/ConfigService.ts';
|
|
20
|
+
|
|
21
|
+
describe('ConfigService', () => {
|
|
22
|
+
it('should get config', async () => {
|
|
23
|
+
const configService = await app.getEggObject(ConfigService);
|
|
24
|
+
const value = configService.get('key');
|
|
25
|
+
assert.equal(value, 'expected');
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## ContextProto 测试
|
|
33
|
+
|
|
34
|
+
`@ContextProto` 对象可以直接通过 `app.getEggObject()` 获取,也可以在 `app.mockModuleContextScope` 中通过 `ctx.getEggObject()` 获取。后者会创建带 DI 生命周期的 ctx,退出时自动销毁:
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import assert from 'node:assert';
|
|
38
|
+
import { app } from '@eggjs/mock/bootstrap';
|
|
39
|
+
import { UserService } from '../app/modules/user/UserService.ts';
|
|
40
|
+
|
|
41
|
+
describe('UserService', () => {
|
|
42
|
+
it('should get user in context scope', async () => {
|
|
43
|
+
await app.mockModuleContextScope(async (ctx) => {
|
|
44
|
+
const userService = await ctx.getEggObject(UserService);
|
|
45
|
+
const user = await userService.getById('1');
|
|
46
|
+
assert(user);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Mock 被注入的依赖
|
|
55
|
+
|
|
56
|
+
当 Service A 依赖 Service B 时,mock B 的原型方法:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import assert from 'node:assert';
|
|
60
|
+
import { app, mm } from '@eggjs/mock/bootstrap';
|
|
61
|
+
import { OrderService } from '../app/modules/order/OrderService.ts';
|
|
62
|
+
import { PaymentService } from '../app/modules/payment/PaymentService.ts';
|
|
63
|
+
|
|
64
|
+
describe('OrderService', () => {
|
|
65
|
+
it('should create order with mocked payment', async () => {
|
|
66
|
+
mm(PaymentService.prototype, 'charge', async () => {
|
|
67
|
+
return { transactionId: 'mock-tx-001' };
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const orderService = await app.getEggObject(OrderService);
|
|
71
|
+
const order = await orderService.create({ productId: '1', amount: 100 });
|
|
72
|
+
assert.equal(order.transactionId, 'mock-tx-001');
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/skills",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.1.2-beta.10",
|
|
4
4
|
"description": "agent skills for egg",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"egg",
|
|
7
|
+
"skill"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/eggjs/egg/tree/next/packages/skills",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/eggjs/egg/issues"
|
|
12
|
+
},
|
|
5
13
|
"license": "MIT",
|
|
6
14
|
"author": "eggjs",
|
|
7
15
|
"repository": {
|
|
@@ -9,7 +17,16 @@
|
|
|
9
17
|
"url": "git+https://github.com/eggjs/egg.git",
|
|
10
18
|
"directory": "packages/skills"
|
|
11
19
|
},
|
|
20
|
+
"files": [
|
|
21
|
+
"egg-controller/**/*.md",
|
|
22
|
+
"egg-core/**/*.md",
|
|
23
|
+
"egg-unittest/**/*.md",
|
|
24
|
+
"egg/**/*.md"
|
|
25
|
+
],
|
|
12
26
|
"publishConfig": {
|
|
13
27
|
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=22.18.0"
|
|
14
31
|
}
|
|
15
|
-
}
|
|
32
|
+
}
|