redis-mock icon indicating copy to clipboard operation
redis-mock copied to clipboard

Needs a version to be in line with [email protected]

Open jpike88 opened this issue 4 years ago • 4 comments

New Redis is around the corner, and this mock lib is breaking in two clear cases:

  • methods are not promisified, whereas redis supports that
  • 'setEx' no not defined on the mock client (but setex is)

jpike88 avatar Sep 10 '21 10:09 jpike88

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();
	});

jpike88 avatar Sep 10 '21 10:09 jpike88

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?

rjdmacedo avatar Mar 03 '22 14:03 rjdmacedo

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 avatar Oct 25 '22 06:10 wardds

@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 🤔

psoleckimoj avatar Feb 14 '23 11:02 psoleckimoj