redis / node-redis

Redis Node.js client
https://redis.js.org/
MIT License
16.9k stars 1.88k forks source link

Provide an official mocking library for mocking Node-Redis in Jest tests #2546

Open blagae opened 1 year ago

blagae commented 1 year ago

Description

I am struggling to find a way to combine the modern Redis v4 client with the jest test framework.

This is my production code:

import redis from 'redis';
import config from './config';
import logger from './logger';

const redisClient = redis.createClient(config.redisConfig);
redisClient.on('error', (e) => logger.error(e));

(async () => {
  await redisClient.connect();
})();

export type RedisClient = typeof redisClient;
export default async () => {
  const clone = redisClient.duplicate();
  await clone.connect();
  return clone;
};

Some of my services of course use redis, and when I try to test them, the entire test suite usually fails at startup. This is a project that was migrated from plain JS to TS, from node-redis 3 to 4, and from mocha to jest.

Below are a few scenarios that don't work.

No config added:

FAIL lib/services/tests/area.spec.ts
  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'createClient')

      4 |
    > 5 | const redisClient = redis.createClient(config.redisConfig);
        |                           ^

      at Object.createClient (lib/redis-client.ts:5:27)

Using the documented scenario with redis-mock:

in package.json

  "jest": {
    "setupFilesAfterEnv": [
      "./jest.setup.redis-mock.js"
    ]
  },

in jest.setup.redis-mock.js

import {jest} from '@jest/globals';
jest.mock('redis', () => jest.requireActual('redis-mock'));

Gives the error:

FAIL lib/services/tests/area.spec.ts
    TypeError: redisClient.connect is not a function

       8 | (async () => {
    >  9 |   await redisClient.connect();
         |                     ^
      10 | })();

      at connect (lib/redis-client.ts:9:21)
      at asyncGeneratorStep (lib/redis-client.ts:3:31)

Messing around in the jest setup file usually ends up with the entire library being undefined, etc.

berthoogsteyns commented 1 year ago

i have the same issue

uglide commented 1 year ago

Hi @blagae Thanks for reporting this issue. Redis-mock package is outdated and doesn't work properly with Node-Redis v4. We plan to provide an official mocking library for Node-redis v5. I'll post an update here once we have a preview release to test.

Please stay tuned.

blagae commented 1 year ago

Hi @uglide

Thanks for your reply. If you're still accepting and/or starting feature requests such as this for the new version, it looks like the release of v5 is not for the very near future. Is there a way that I can follow the current goals, the progress towards it, and/or some milestones ? I haven't found a comprehensive overview.

Alternatively, do you know why the node-redis library doesn't allow being used in a jest test run ? Is it something with an env variable or another type of global flag ? I wouldn't mind messing around with it a bit, including actually running a Redis server to test my code.

EvanSanderson commented 1 month ago

Any updates on this? Having similar issues with migrating to node redis v4 - a big portion of our existing tests that rely on redis-mock fail. It would be great to get something from within the library itself, or any guidance on how to best mock the client in the new rendition of the library. Thanks!