Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/health-indicator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
"redis": "^5.0.0"
},
"devDependencies": {
"@nestjs-redis/client": "0.10.1",
"@nestjs/testing": "^11.0.0",
"redis": "^5.0.0"
"redis": "^5.0.0",
"supertest": "^7.1.4"
},
"engines": {
"node": ">=18.0.0",
Expand Down
91 changes: 73 additions & 18 deletions packages/health-indicator/src/lib/health.indicator.int.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { HealthIndicatorService } from '@nestjs/terminus';
import { Controller, Get, Module } from '@nestjs/common';
import {
HealthCheck,
HealthCheckResult,
HealthCheckService,
TerminusModule,
} from '@nestjs/terminus';
import { Test, TestingModule } from '@nestjs/testing';
import { InjectRedis, Redis, RedisModule } from '@nestjs-redis/client';
import { createClient } from 'redis';
import request from 'supertest';
import { RedisHealthIndicator } from './health.indicator';
import { Redis } from './interfaces';

// These tests require a running Redis instance
const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379';
Expand All @@ -13,22 +20,8 @@ describe('RedisHealthIndicator Integration Tests', () => {

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
RedisHealthIndicator,
{
provide: HealthIndicatorService,
useValue: {
check: jest.fn().mockReturnValue({
up: jest.fn().mockImplementation((data) => ({
redis: { status: 'up', ...data },
})),
down: jest.fn().mockImplementation((data) => ({
redis: { status: 'down', ...data },
})),
}),
},
},
],
imports: [TerminusModule],
providers: [RedisHealthIndicator],
}).compile();

healthIndicator = module.get<RedisHealthIndicator>(RedisHealthIndicator);
Expand Down Expand Up @@ -85,4 +78,66 @@ describe('RedisHealthIndicator Integration Tests', () => {
}
}, 15000);
});

describe('Full RedisModule Integration', () => {
it('should integrate with the full RedisModule', async () => {
@Controller('health')
class HealthController {
constructor(
private health: HealthCheckService,
private redis: RedisHealthIndicator,
@InjectRedis() private readonly redisClient: Redis,
) {}

@Get()
@HealthCheck()
check(): Promise<HealthCheckResult> {
return this.health.check([
() => this.redis.isHealthy('redis', { client: this.redisClient }),
]);
}
}

@Module({
imports: [
RedisModule.forRoot({
type: 'client',
options: { url: process.env.REDIS_URL },
}),
TerminusModule,
],
controllers: [HealthController],
providers: [RedisHealthIndicator],
})
class HealthModule {}

@Module({
imports: [HealthModule],
controllers: [],
providers: [],
})
class AppModule {}

const module: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

const app = module.createNestApplication();
await app.init();

request(app.getHttpServer())
.get('/health')
.expect(200)
.expect({
data: {
status: 'ok',
info: { redis: { status: 'up' } },
error: {},
details: { redis: { status: 'up' } },
},
});

await app.close();
});
});
});
14 changes: 11 additions & 3 deletions packages/health-indicator/src/lib/health.indicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ import { Redis } from './interfaces';

@Injectable()
export class RedisHealthIndicator {
constructor(
private readonly healthIndicatorService: HealthIndicatorService,
) {}
/**
* TODO
*
* This is workaround, this should be DI but for some reason
* HealthIndicatorService is not injected after building the package.
*
* Reference (how it should be): https://docs.nestjs.com/recipes/terminus#custom-health-indicator
*
* ToDo: Fix this issue in the future.
*/
private healthIndicatorService = new HealthIndicatorService();

async isHealthy(
key: string,
Expand Down
81 changes: 81 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.