Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jpike88 opened this issue Sep 10, 2021 · 4 comments
Open

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

jpike88 opened this issue Sep 10, 2021 · 4 comments

Comments

@jpike88
Copy link

jpike88 commented Sep 10, 2021

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
Copy link
Author

jpike88 commented Sep 10, 2021

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

@rjdmacedo
Copy link

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?

@wardds
Copy link

wardds commented Oct 25, 2022

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

@psoleckimoj
Copy link

psoleckimoj commented Feb 14, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants