mirror of
https://github.com/informaticker/discord-jellyfin-bot.git
synced 2024-12-03 14:21:56 +01:00
✅ Add tests for health indicators #12
This commit is contained in:
parent
d67bfbbf84
commit
469d1cb69d
@ -1,18 +1,80 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import {
|
||||
HealthCheckResult,
|
||||
HealthCheckService,
|
||||
HealthIndicatorResult,
|
||||
} from '@nestjs/terminus';
|
||||
import { HealthCheckExecutor } from '@nestjs/terminus/dist/health-check/health-check-executor.service';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { useDefaultMockerToken } from '../utils/tests';
|
||||
import { HealthController } from './health.controller';
|
||||
import { DiscordHealthIndicator } from './indicators/discord.indicator';
|
||||
import { JellyfinHealthIndicator } from './indicators/jellyfin.indicator';
|
||||
|
||||
describe('HealthController', () => {
|
||||
let controller: HealthController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
controllers: [HealthController],
|
||||
}).compile();
|
||||
})
|
||||
.useMocker((token) => {
|
||||
if (token === JellyfinHealthIndicator) {
|
||||
return {
|
||||
isHealthy: jest.fn().mockResolvedValue({
|
||||
jellyfin: {
|
||||
status: 'up',
|
||||
},
|
||||
} as HealthIndicatorResult),
|
||||
};
|
||||
}
|
||||
|
||||
controller = module.get<HealthController>(HealthController);
|
||||
if (token === DiscordHealthIndicator) {
|
||||
return {
|
||||
isHealthy: jest.fn().mockResolvedValue({
|
||||
discord: {
|
||||
status: 'up',
|
||||
},
|
||||
} as HealthIndicatorResult),
|
||||
};
|
||||
}
|
||||
|
||||
if (token === HealthCheckService) {
|
||||
return new HealthCheckService(new HealthCheckExecutor(), null);
|
||||
}
|
||||
|
||||
return useDefaultMockerToken(token);
|
||||
})
|
||||
.compile();
|
||||
|
||||
controller = moduleRef.get<HealthController>(HealthController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return health status', async () => {
|
||||
const result = await controller.healthCheck();
|
||||
|
||||
expect(result).toStrictEqual({
|
||||
details: {
|
||||
discord: {
|
||||
status: 'up',
|
||||
},
|
||||
jellyfin: {
|
||||
status: 'up',
|
||||
},
|
||||
},
|
||||
error: {},
|
||||
info: {
|
||||
discord: {
|
||||
status: 'up',
|
||||
},
|
||||
jellyfin: {
|
||||
status: 'up',
|
||||
},
|
||||
},
|
||||
status: 'ok',
|
||||
} as HealthCheckResult);
|
||||
});
|
||||
});
|
||||
|
@ -14,7 +14,7 @@ export class HealthController {
|
||||
|
||||
@Get()
|
||||
@HealthCheck()
|
||||
healthCheck() {
|
||||
async healthCheck() {
|
||||
return this.health.check([
|
||||
() => this.discordIndicator.isHealthy('discord'),
|
||||
() => this.jellyfinHealthIndicator.isHealthy('jellyfin'),
|
||||
|
48
src/health/indicators/jeyllfin.indicator.spec.ts
Normal file
48
src/health/indicators/jeyllfin.indicator.spec.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { HealthIndicatorResult } from '@nestjs/terminus';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { JellyfinService } from '../../clients/jellyfin/jellyfin.service';
|
||||
import { useDefaultMockerToken } from '../../utils/tests';
|
||||
import { JellyfinHealthIndicator } from './jellyfin.indicator';
|
||||
|
||||
describe('JellyfinHealthIndicator', () => {
|
||||
let service: JellyfinHealthIndicator;
|
||||
let jellyfinService: JellyfinService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
providers: [JellyfinHealthIndicator],
|
||||
})
|
||||
.useMocker((token) => {
|
||||
if (token === JellyfinService) {
|
||||
return { isConnected: jest.fn() };
|
||||
}
|
||||
return useDefaultMockerToken(token);
|
||||
})
|
||||
.compile();
|
||||
|
||||
service = moduleRef.get<JellyfinHealthIndicator>(JellyfinHealthIndicator);
|
||||
jellyfinService = moduleRef.get<JellyfinService>(JellyfinService);
|
||||
});
|
||||
|
||||
it('isHealthyWhenJellyfinIsConnected', async () => {
|
||||
jest.spyOn(jellyfinService, 'isConnected').mockImplementation(() => true);
|
||||
const result = await service.isHealthy('jellyfin');
|
||||
|
||||
expect(result).toStrictEqual({
|
||||
jellyfin: {
|
||||
status: 'up',
|
||||
},
|
||||
} as HealthIndicatorResult);
|
||||
});
|
||||
|
||||
it('isUnhealthyWhenJellyfinIsNotConnected', async () => {
|
||||
jest.spyOn(jellyfinService, 'isConnected').mockImplementation(() => false);
|
||||
const result = await service.isHealthy('jellyfin');
|
||||
|
||||
expect(result).toStrictEqual({
|
||||
jellyfin: {
|
||||
status: 'down',
|
||||
},
|
||||
} as HealthIndicatorResult);
|
||||
});
|
||||
});
|
16
src/utils/tests.ts
Normal file
16
src/utils/tests.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { InjectionToken } from '@nestjs/common';
|
||||
import { MockFunctionMetadata, ModuleMocker } from 'jest-mock';
|
||||
|
||||
const moduleMocker = new ModuleMocker(global);
|
||||
|
||||
export const useDefaultMockerToken = (token: InjectionToken) => {
|
||||
if (typeof token === 'function') {
|
||||
const mockMetadata = moduleMocker.getMetadata(
|
||||
token,
|
||||
) as MockFunctionMetadata<any, any>;
|
||||
const Mock = moduleMocker.generateFromMetadata(mockMetadata);
|
||||
return new Mock();
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
Loading…
Reference in New Issue
Block a user