1- import { Controller , Get , Module } from '@nestjs/common' ;
2- import {
3- HealthCheck ,
4- HealthCheckResult ,
5- HealthCheckService ,
6- TerminusModule ,
7- } from '@nestjs/terminus' ;
1+ import { HealthIndicatorService } from '@nestjs/terminus' ;
82import { Test , TestingModule } from '@nestjs/testing' ;
9- import { InjectRedis , Redis , RedisModule } from '@nestjs-redis/client' ;
103import { createClient } from 'redis' ;
11- import request from 'supertest' ;
124import { RedisHealthIndicator } from './health.indicator' ;
5+ import { Redis } from './interfaces' ;
136
147// These tests require a running Redis instance
158const REDIS_URL = process . env . REDIS_URL || 'redis://localhost:6379' ;
@@ -20,8 +13,22 @@ describe('RedisHealthIndicator Integration Tests', () => {
2013
2114 beforeAll ( async ( ) => {
2215 const module : TestingModule = await Test . createTestingModule ( {
23- imports : [ TerminusModule ] ,
24- providers : [ RedisHealthIndicator ] ,
16+ providers : [
17+ RedisHealthIndicator ,
18+ {
19+ provide : HealthIndicatorService ,
20+ useValue : {
21+ check : jest . fn ( ) . mockReturnValue ( {
22+ up : jest . fn ( ) . mockImplementation ( ( data ) => ( {
23+ redis : { status : 'up' , ...data } ,
24+ } ) ) ,
25+ down : jest . fn ( ) . mockImplementation ( ( data ) => ( {
26+ redis : { status : 'down' , ...data } ,
27+ } ) ) ,
28+ } ) ,
29+ } ,
30+ } ,
31+ ] ,
2532 } ) . compile ( ) ;
2633
2734 healthIndicator = module . get < RedisHealthIndicator > ( RedisHealthIndicator ) ;
@@ -78,66 +85,4 @@ describe('RedisHealthIndicator Integration Tests', () => {
7885 }
7986 } , 15000 ) ;
8087 } ) ;
81-
82- describe ( 'Full RedisModule Integration' , ( ) => {
83- it ( 'should integrate with the full RedisModule' , async ( ) => {
84- @Controller ( 'health' )
85- class HealthController {
86- constructor (
87- private health : HealthCheckService ,
88- private redis : RedisHealthIndicator ,
89- @InjectRedis ( ) private readonly redisClient : Redis ,
90- ) { }
91-
92- @Get ( )
93- @HealthCheck ( )
94- check ( ) : Promise < HealthCheckResult > {
95- return this . health . check ( [
96- ( ) => this . redis . isHealthy ( 'redis' , { client : this . redisClient } ) ,
97- ] ) ;
98- }
99- }
100-
101- @Module ( {
102- imports : [
103- RedisModule . forRoot ( {
104- type : 'client' ,
105- options : { url : process . env . REDIS_URL } ,
106- } ) ,
107- TerminusModule ,
108- ] ,
109- controllers : [ HealthController ] ,
110- providers : [ RedisHealthIndicator ] ,
111- } )
112- class HealthModule { }
113-
114- @Module ( {
115- imports : [ HealthModule ] ,
116- controllers : [ ] ,
117- providers : [ ] ,
118- } )
119- class AppModule { }
120-
121- const module : TestingModule = await Test . createTestingModule ( {
122- imports : [ AppModule ] ,
123- } ) . compile ( ) ;
124-
125- const app = module . createNestApplication ( ) ;
126- await app . init ( ) ;
127-
128- request ( app . getHttpServer ( ) )
129- . get ( '/health' )
130- . expect ( 200 )
131- . expect ( {
132- data : {
133- status : 'ok' ,
134- info : { redis : { status : 'up' } } ,
135- error : { } ,
136- details : { redis : { status : 'up' } } ,
137- } ,
138- } ) ;
139-
140- await app . close ( ) ;
141- } ) ;
142- } ) ;
14388} ) ;
0 commit comments