Open jpike88 opened 3 years ago
my current ugly workaround:
const mockRedis = RedisMockClient.createClient();
// promisify mock methods
redis = {
get: promisify(mockRedis.get).bind(mockRedis),
delete: promisify(mockRedis.del).bind(mockRedis),
flushAll: promisify(mockRedis.flushall).bind(mockRedis),
setEx: promisify(mockRedis.setex).bind(mockRedis),
expire: promisify(mockRedis.expire).bind(mockRedis),
} as unknown as RedisClientType<RedisModules, RedisLuaScripts>;
mockRedis.on('connect', () => {
queue.start();
});
my current ugly workaround:
const mockRedis = RedisMockClient.createClient(); // promisify mock methods redis = { get: promisify(mockRedis.get).bind(mockRedis), delete: promisify(mockRedis.del).bind(mockRedis), flushAll: promisify(mockRedis.flushall).bind(mockRedis), setEx: promisify(mockRedis.setex).bind(mockRedis), expire: promisify(mockRedis.expire).bind(mockRedis), } as unknown as RedisClientType<RedisModules, RedisLuaScripts>; mockRedis.on('connect', () => { queue.start(); });
Can you show a broader exemple on how that solution works?
Thanks to @jpike88 I managed to create a fairly clean, but hopefully temporary, workaround:
// ** redis-mock-v4.ts ** //
// redis-mock has not been updated for node-redis v4 yet, but the main changes
// in the API are camelCase names and promises instead of callback, so we can work around it.
// https://github.com/yeahoffline/redis-mock/issues/195
import redis from "redis-mock";
// @ts-expect-error Work-around redis-mock types reporting incorrectly as v4 redis.
import { RedisClient } from "@types/redis";
import { promisify } from "util";
const client = redis.createClient() as unknown as RedisClient;
const setEx = promisify(client.setex).bind(client);
const v4Client = {
connect: () => undefined,
get: promisify(client.get).bind(client),
del: promisify(client.del).bind(client),
flushAll: promisify(client.flushall).bind(client),
setEx: promisify(client.setex).bind(client),
expire: promisify(client.expire).bind(client),
mGet: promisify(client.mget).bind(client),
pSetEx: (key: string, ms: number, value: string) =>
setEx(key, ms / 1000, value),
// Add additional functions as needed...
};
export default { ...redis, createClient: () => v4Client };
// ** my-unit-tests.ts ** //
import redis from "./redis-mock-v4"; // used to be from "redis-mock"
jest.mock("redis", () => redis);
@wardds thanks for this and @jpike88 I'm trying as above but keep getting:
ReferenceError: Cannot access 'redis_mock_v4_1' before initialization
10 | import redis from '../testutils/redis-mock-v4'
11 |
> 12 | jest.mock('redis', () => redis)`
redis
is there and functions are available but it doesn't seem to like this 🤔
New Redis is around the corner, and this mock lib is breaking in two clear cases: