import { Test } from '@nestjs/testing'; import IORedis from 'ioredis'; import { LoggerModule } from 'nestjs-pino'; import { RedisModule } from './redis.module'; import { REDIS_CLIENT } from './redis.token'; const ORIGINAL_REDIS_URL = process.env['REDIS_URL']; async function compile() { return Test.createTestingModule({ imports: [LoggerModule.forRoot({ pinoHttp: { level: 'silent' } }), RedisModule], }).compile(); } describe('RedisModule', () => { afterEach(async () => { if (ORIGINAL_REDIS_URL === undefined) { delete process.env['REDIS_URL']; } else { process.env['REDIS_URL'] = ORIGINAL_REDIS_URL; } }); it('provides an ioredis client via the REDIS_CLIENT token', async () => { // A well-formed but unreachable URL — `ioredis` constructs the // client lazily so the test never opens a real socket. process.env['REDIS_URL'] = 'redis://default:test-pass@127.0.0.1:65535/0'; const ref = await compile(); try { const client = ref.get(REDIS_CLIENT); expect(client).toBeInstanceOf(IORedis); } finally { ref.get(REDIS_CLIENT).disconnect(); await ref.close(); } }); it('fails to compile when REDIS_URL is missing', async () => { delete process.env['REDIS_URL']; await expect(compile()).rejects.toThrow(/REDIS_URL is not set/); }); });