Commit f64b818
authored
chore(redis): support ioredis-mock for unit testing (#5817)
## Summary
Add built-in support for using
[ioredis-mock](https://github.com/stipsan/ioredis-mock) in unit tests,
and document how to configure it.
## Problem
When using `config.redis.Redis = RedisMock` (ioredis-mock) for unit
testing, the app hangs on startup because:
1. `ioredis-mock` emits the `ready` event synchronously during
construction
2. `@eggjs/redis` plugin attaches the `ready` listener later in
`registerBeforeStart`
3. The `ready` event is missed, causing the app to wait forever
## Solution
Auto-enable `weakDependent` mode when `config.redis.Redis` is set to a
custom class. This skips the blocking `ready` event check, which is safe
for mock clients that are immediately ready.
## Changes
### `plugins/redis/src/lib/redis.ts`
- Detect custom Redis class and auto-enable weakDependent
### `plugins/redis/src/config/config.default.ts`
- Updated `Redis` config JSDoc with ioredis-mock usage example
### `plugins/redis/README.md`
- Added "Using ioredis-mock for Unit Tests" section with install,
configure, and notes
### Tests
- Added `redisapp-mock` fixture with ioredis-mock config
- Added 3 test cases: HTTP request, setex/get, del
## Usage
```ts
// config/config.unittest.ts
import RedisMock from 'ioredis-mock';
export default function () {
return {
redis: {
Redis: RedisMock,
client: { host: '127.0.0.1', port: 6379, password: '', db: 0 },
},
};
}
```
Then remove the `redis` service from your CI workflow — no real Redis
needed for unit tests.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Documentation**
* Added comprehensive Redis plugin integration guide with configuration
examples for single-client, multi-client, and cluster setups.
* Included testing guidance and examples using ioredis-mock for unit
tests with weakDependent configuration details.
* Added Chinese translations of Redis tutorials and plugin
documentation.
* **Tests**
* Added test suite for ioredis-mock support with setup and validation
tests.
* **Chores**
* Added ioredis-mock as a development dependency.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent ae71b0a commit f64b818
15 files changed
Lines changed: 708 additions & 1 deletion
File tree
- plugins/redis
- src/config
- test
- fixtures/apps/redisapp-mock
- app
- controller
- config
- site/docs
- tutorials
- zh-CN/tutorials
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
277 | 277 | | |
278 | 278 | | |
279 | 279 | | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
280 | 323 | | |
281 | 324 | | |
282 | 325 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
54 | 75 | | |
55 | 76 | | |
56 | 77 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
196 | 197 | | |
197 | 198 | | |
198 | 199 | | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments