I'm Using Redis cache for my nestjs Application and i'm maintaining redisCache.module.ts for Redis configuration.But my unit test has fail because,it's trying to connect into Redis server.How can i fix this issue?
redisCache.module.ts
import { Module, CacheModule } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import * as redisStore from 'cache-manager-redis-store';
import { RedisCacheService } from './redisCache.service';
@Module({
imports: [
CacheModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
store:redisStore,
host: configService.get('REDIS_HOST'),
port: configService.get('REDIS_PORT'),
ttl: configService.get('REDIS_CACHE_TTL'),
}),
}),
],
providers: [RedisCacheService],
exports: [RedisCacheService]
})
export class RedisCacheModule {}
redisCache.module.spec.ts
import { CacheModule, CACHE_MANAGER } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { RedisCacheService } from './redisCache.service';
import { RedisCacheModule } from './redisCache.module';
import { Cache } from 'cache-manager';
describe('RedisCacheModule', () => {
let redisCacheService: RedisCacheService;
let redisCacheModule: RedisCacheModule;
let cache: Cache;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [RedisCacheService],
imports: [CacheModule.register({}),RedisCacheModule],
}).compile();
redisCacheService = module.get<RedisCacheService>(RedisCacheService);
redisCacheModule = module.get<RedisCacheModule>(RedisCacheModule);
cache = module.get(CACHE_MANAGER);
});
it('RedisCacheService should be defined', () => {
expect(redisCacheService).toBeDefined();
});
it('RedisCacheModule should be defined', () => {
expect(redisCacheModule).toBeDefined();
});
});
Error
