Skip to content

Commit 37cab1b

Browse files
committed
add redis-mock library and fix tests
1 parent a4edc89 commit 37cab1b

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"jest": "^26.2.2",
2929
"mongodb-memory-server": "^6.6.1",
3030
"nodemon": "^2.0.2",
31+
"redis-mock": "^0.58.0",
3132
"ts-jest": "^26.1.4",
3233
"ts-node": "^10.9.1",
3334
"typescript": "^4.7.4"

test/integration/jestEnv.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const NodeEnvironment = require('jest-environment-node');
22
const amqp = require('amqplib');
33
const mongodb = require('mongodb');
4+
const { installRedisMock, uninstallRedisMock } = require('./redisMock');
45

56
/**
67
* Custom test environment for defining global connections
@@ -19,6 +20,12 @@ class CustomEnvironment extends NodeEnvironment {
1920
await mongoClient.db('hawk').dropDatabase();
2021
// await mongoClient.db('codex_accounting').dropDatabase();
2122

23+
/**
24+
* Use redis-mock instead of a real Redis connection.
25+
* This avoids spinning up Redis during integration tests while keeping the API surface.
26+
*/
27+
this.global.redisClient = installRedisMock();
28+
2229
this.rabbitMqConnection = await amqp.connect('amqp://guest:guest@rabbitmq:5672/');
2330
this.global.rabbitChannel = await this.rabbitMqConnection.createChannel();
2431
await this.global.rabbitChannel.purgeQueue('cron-tasks/limiter');
@@ -41,6 +48,8 @@ class CustomEnvironment extends NodeEnvironment {
4148
if (this.rabbitMqConnection) {
4249
await this.rabbitMqConnection.close();
4350
}
51+
52+
uninstallRedisMock();
4453
} catch (error) {
4554
console.error('Error during teardown:', error);
4655
}

test/integration/redisMock.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const path = require('path');
2+
const redisMock = require('redis-mock');
3+
4+
let originalRedisModule = null;
5+
let redisModulePath = null;
6+
7+
/**
8+
* Create Redis mock client compatible with node-redis v4 API portions we use.
9+
*
10+
* @returns {object} mocked redis client
11+
*/
12+
function createMockClient() {
13+
const client = redisMock.createClient();
14+
15+
client.isOpen = true;
16+
client.connect = async () => client;
17+
client.quit = async () => undefined;
18+
client.sendCommand = async () => [];
19+
client.on = () => client;
20+
21+
return client;
22+
}
23+
24+
/**
25+
* Install redis-mock into Node's module cache so that `require('redis')`
26+
* returns the mocked client factory.
27+
*
28+
* @returns {object} mock client instance to be reused in tests
29+
*/
30+
function installRedisMock() {
31+
redisModulePath = require.resolve('redis');
32+
originalRedisModule = require.cache[redisModulePath] || null;
33+
34+
const mockExports = {
35+
createClient: () => createMockClient(),
36+
};
37+
38+
require.cache[redisModulePath] = {
39+
id: redisModulePath,
40+
filename: redisModulePath,
41+
loaded: true,
42+
exports: mockExports,
43+
path: path.dirname(redisModulePath),
44+
children: [],
45+
};
46+
47+
return mockExports.createClient();
48+
}
49+
50+
/**
51+
* Restore original `redis` module if it existed.
52+
*/
53+
function uninstallRedisMock() {
54+
if (!redisModulePath) {
55+
return;
56+
}
57+
58+
if (originalRedisModule) {
59+
require.cache[redisModulePath] = originalRedisModule;
60+
} else {
61+
delete require.cache[redisModulePath];
62+
}
63+
64+
originalRedisModule = null;
65+
redisModulePath = null;
66+
}
67+
68+
module.exports = {
69+
installRedisMock,
70+
uninstallRedisMock,
71+
};
72+

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5762,6 +5762,11 @@ readdirp@~3.6.0:
57625762
dependencies:
57635763
picomatch "^2.2.1"
57645764

5765+
redis-mock@^0.58.0:
5766+
version "0.56.3"
5767+
resolved "https://registry.yarnpkg.com/redis-mock/-/redis-mock-0.56.3.tgz#e96471bcc774ddc514c2fc49cdd03cab2baecd89"
5768+
integrity sha512-ynaJhqk0Qf3Qajnwvy4aOjS4Mdf9IBkELWtjd+NYhpiqu4QCNq6Vf3Q7c++XRPGiKiwRj9HWr0crcwy7EiPjYQ==
5769+
57655770
redis@^4.7.0:
57665771
version "4.7.1"
57675772
resolved "https://registry.yarnpkg.com/redis/-/redis-4.7.1.tgz#08588a30936be0e7ad9c0f3e1ac6a85ccaf73e94"

0 commit comments

Comments
 (0)