我们创建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();
dev-dependent@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();
});
});
我的同事似乎让测试单独运行,但使用代码分支,我遇到了上述问题。
关于我哪里出了问题,有什么建议吗?
我建议检查您的Typescript版本
如果是<4,然后将TS跳变为4>应该解决这个问题。