将ioredis升级到v5.0.1后出现的问题

问题描述 投票:0回答:2

我们创建 Redis 客户端的原始代码

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

随着我们升级到ioredis 5.0.1版本,就变成了这样:

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

开发依赖项 @types/ioredis 已从我们的 package.json 中删除。

编译项目打字稿时遇到语义错误。

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'.

笑话测试不再运行,标记该行返回新集群,失败并显示以下内容

 TypeError: _ioredis.Cluster is not a constructor

测试:

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

我的同事似乎让测试单独运行,但是使用代码分支,我遇到了上述问题。

对我哪里出错有什么建议吗?

node.js typescript jestjs ioredis
2个回答
0
投票

我建议检查你的 Typescript 版本。
如果是 < 4, then bumping TS to 4 > 应该解决此问题。


0
投票

尝试从

Redis.Cluster([])
-
import Redis from 'ioredis'

创建实例

因为

import { Cluster } from 'ioredis'
是一个接口。它对于类型声明很有用。

© www.soinside.com 2019 - 2024. All rights reserved.