@eggjs/redis 4.0.2-beta.1 → 4.0.2-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/README.md +43 -0
- package/dist/config/config.default.d.ts +22 -1
- package/dist/lib/redis.js +2 -2
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -277,6 +277,49 @@ Stop test redis service
|
|
|
277
277
|
docker compose -f docker-compose.yml down
|
|
278
278
|
```
|
|
279
279
|
|
|
280
|
+
## Using ioredis-mock for Unit Tests
|
|
281
|
+
|
|
282
|
+
You can use [ioredis-mock](https://github.com/stipsan/ioredis-mock) to replace the real Redis client in unit tests. This eliminates the need for a running Redis server during testing, making your CI faster and local development simpler.
|
|
283
|
+
|
|
284
|
+
### Install
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
npm i --save-dev ioredis-mock @types/ioredis-mock
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Configure
|
|
291
|
+
|
|
292
|
+
In your test config (e.g., `config/config.unittest.ts`), override the `Redis` class:
|
|
293
|
+
|
|
294
|
+
```ts
|
|
295
|
+
import RedisMock from 'ioredis-mock';
|
|
296
|
+
import type { EggAppInfo, PartialEggConfig } from 'egg';
|
|
297
|
+
|
|
298
|
+
export default function (_appInfo: EggAppInfo): PartialEggConfig {
|
|
299
|
+
return {
|
|
300
|
+
redis: {
|
|
301
|
+
Redis: RedisMock,
|
|
302
|
+
client: {
|
|
303
|
+
host: '127.0.0.1',
|
|
304
|
+
port: 6379,
|
|
305
|
+
password: '',
|
|
306
|
+
db: 0,
|
|
307
|
+
weakDependent: true,
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
> **Important**: You must set `weakDependent: true` when using `ioredis-mock`. Mock clients emit the `ready` event synchronously during construction, before the plugin's listener is attached. Without `weakDependent: true`, the app will hang on startup waiting for a `ready` event that was already emitted.
|
|
315
|
+
|
|
316
|
+
### Notes
|
|
317
|
+
|
|
318
|
+
- `ioredis-mock` provides an in-memory Redis implementation that supports most common commands (`get`, `set`, `setex`, `del`, `incr`, `zadd`, `zpopmin`, `zcount`, etc.)
|
|
319
|
+
- Each test worker gets an isolated in-memory Redis instance
|
|
320
|
+
- For production deployment testing, you should still use a real Redis server
|
|
321
|
+
- You can remove `redis` service containers from your CI workflow when using `ioredis-mock` for unit tests
|
|
322
|
+
|
|
280
323
|
## Questions & Suggestions
|
|
281
324
|
|
|
282
325
|
Please open an issue [here](https://github.com/eggjs/egg/issues).
|
|
@@ -49,7 +49,28 @@ interface RedisConfig {
|
|
|
49
49
|
*/
|
|
50
50
|
agent: boolean;
|
|
51
51
|
/**
|
|
52
|
-
* Customize
|
|
52
|
+
* Customize Redis client class. Use this to replace ioredis with a compatible
|
|
53
|
+
* alternative, such as iovalkey or ioredis-mock for unit testing.
|
|
54
|
+
*
|
|
55
|
+
* When using ioredis-mock, you must also set `weakDependent: true` in the
|
|
56
|
+
* client config to avoid startup hangs (mock clients emit 'ready' synchronously
|
|
57
|
+
* before the plugin's listener is attached).
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* // config/config.unittest.ts
|
|
62
|
+
* import RedisMock from 'ioredis-mock';
|
|
63
|
+
* import type { EggAppInfo, PartialEggConfig } from 'egg';
|
|
64
|
+
*
|
|
65
|
+
* export default function (_appInfo: EggAppInfo): PartialEggConfig {
|
|
66
|
+
* return {
|
|
67
|
+
* redis: {
|
|
68
|
+
* Redis: RedisMock,
|
|
69
|
+
* client: { host: '127.0.0.1', port: 6379, password: '', db: 0, weakDependent: true },
|
|
70
|
+
* },
|
|
71
|
+
* };
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
53
74
|
*
|
|
54
75
|
* Default to `undefined`, which means using the built-in ioredis
|
|
55
76
|
*/
|
package/dist/lib/redis.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import assert from "node:assert";
|
|
2
2
|
import { once } from "node:events";
|
|
3
|
-
import
|
|
3
|
+
import IoRedis from "ioredis";
|
|
4
4
|
|
|
5
5
|
//#region src/lib/redis.ts
|
|
6
6
|
var RedisBoot = class {
|
|
@@ -16,7 +16,7 @@ var RedisBoot = class {
|
|
|
16
16
|
};
|
|
17
17
|
let count = 0;
|
|
18
18
|
function createClient(options, app) {
|
|
19
|
-
const RedisClass = app.config.redis.Redis ??
|
|
19
|
+
const RedisClass = app.config.redis.Redis ?? IoRedis;
|
|
20
20
|
let client;
|
|
21
21
|
if ("cluster" in options && options.cluster === true) {
|
|
22
22
|
const config = options;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/redis",
|
|
3
|
-
"version": "4.0.2-beta.
|
|
3
|
+
"version": "4.0.2-beta.3",
|
|
4
4
|
"description": "Valkey / Redis plugin for egg",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Valkey",
|
|
@@ -46,13 +46,14 @@
|
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/node": "^24.10.2",
|
|
48
48
|
"detect-port": "^2.1.0",
|
|
49
|
+
"ioredis-mock": "^8.13.1",
|
|
49
50
|
"typescript": "^5.9.3",
|
|
50
|
-
"@eggjs/mock": "7.0.2-beta.
|
|
51
|
-
"
|
|
52
|
-
"
|
|
51
|
+
"@eggjs/mock": "7.0.2-beta.3",
|
|
52
|
+
"egg": "4.1.2-beta.3",
|
|
53
|
+
"@eggjs/tsconfig": "3.1.2-beta.3"
|
|
53
54
|
},
|
|
54
55
|
"peerDependencies": {
|
|
55
|
-
"egg": "4.1.2-beta.
|
|
56
|
+
"egg": "4.1.2-beta.3"
|
|
56
57
|
},
|
|
57
58
|
"engines": {
|
|
58
59
|
"node": ">= 22.18.0"
|