'issues after upgrading ioredis to v5.0.1

Our original code to create redis clients

import Redis, { Redis as RedisClient, Cluster, ClusterOptions } from 'ioredis';
import config from '../../../config';

const {
  port, host, cluster, transitEncryption,
} = config.redis;

const retryStrategy = (times: number): number => Math.min(times * 50, 10000);

function GetRedisClient(): RedisClient {
  return new Redis({
    host,
    port: port as number,
    retryStrategy,
  });
}

function GetRedisClusterClient(): Cluster {
  const clusterOptions: ClusterOptions = {
    clusterRetryStrategy: retryStrategy,
    redisOptions: { db: 0 },
  };
  if (transitEncryption) {
    clusterOptions.dnsLookup = (address, callback) => callback(null, address);
    clusterOptions.redisOptions!.tls = {};
  }

  return new Redis.Cluster([
    {
      host,
      port: port as number,
    },
  ], clusterOptions);
}

function getClient() {
  return cluster ? GetRedisClusterClient() : GetRedisClient();
}

export default getClient();

has become this as we upgrade to version 5.0.1 of ioredis:

import Redis, { Cluster, ClusterOptions } from 'ioredis';
import config from '../../../config';

const {
  port, host, cluster, transitEncryption,
} = config.redis;

const retryStrategy = (times: number): number => Math.min(times * 50, 10000);

function GetRedisClient(): Redis {
  return new Redis({
    host,
    port: port as number,
    retryStrategy,
  });
}

function GetRedisClusterClient(): Cluster {
  const clusterOptions: ClusterOptions = {
    clusterRetryStrategy: retryStrategy,
    redisOptions: { db: 0 },
  };
  if (transitEncryption) {
    clusterOptions.dnsLookup = (address, callback) => callback(undefined, address);
    clusterOptions.redisOptions!.tls = {};
  }

  return new Cluster([
    {
      host,
      port: port as number,
    },
  ], clusterOptions);
}

function getClient() {
  return cluster ? GetRedisClusterClient() : GetRedisClient();
}

export default getClient();

The dev dependency @types/ioredis has been removed from our package.json.

I'm getting a semantic error when I compile the project typescript.

xxx/node_modules/@types/connect-redis/index.d.ts(22,51): error TS2694: Namespace '"xxx/node_modules/ioredis/built/index"' has no exported member 'Redis'.

And the jest tests no longer run, flagging up the line return new Cluster, failing with the following

 TypeError: _ioredis.Cluster is not a constructor

The tests:

import Redis, { Cluster, ClusterOptions } from 'ioredis';
import config from '../../../config';

jest.mock('ioredis');
jest.mock('../../../config', () => ({
  redis: {
    host: 'redis',
    port: 1234,
    cluster: false,
    transitEncryption: false,
  },
  service_name: 'app name',
  version: 'test',
}));

describe('redis client', () => {
  const clusterTest = (clusterOptions: ClusterOptions) => {
    // eslint-disable-next-line global-require
    require('./client');
    expect(Cluster)
      .toHaveBeenCalled();
  };
  const clusterOptions: ClusterOptions = {
    clusterRetryStrategy: expect.any(Function),
    redisOptions: { db: 0 },
  };
  test('if the cluster variable is set, then the module should return a clustered client', () => {
    config.redis.cluster = true;
    clusterTest(clusterOptions);
  });
  test('if the cluster variable is set and transit encryption is set, then the module should return a clustered client with encryption', () => {
    config.redis.cluster = true;
    config.redis.transitEncryption = true;
    clusterOptions.dnsLookup = expect.any(Function);
    clusterOptions.redisOptions = { db: 0, tls: {} };
    clusterTest(clusterOptions);
  });
  test('if the cluster variable is not set, then the module should return a standalone client', () => {
    config.redis.cluster = false;
    // eslint-disable-next-line global-require
    require('./client');
    expect(Redis).toHaveBeenCalled();
  });
});

My colleague seems to get the tests running individually, but using code branch, I get the above problem.

Any suggestions about where I've gone wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source