- Integrated Sentry status check functionality in the health controller. - Updated tests to validate Sentry active/disabled states. - Improved Sentry initialization with enhanced logging and error handling.
91 lines
2.7 KiB
TypeScript
91 lines
2.7 KiB
TypeScript
import { CACHE_MANAGER } from "@nestjs/cache-manager";
|
|
import { Test, TestingModule } from "@nestjs/testing";
|
|
import * as Sentry from "@sentry/nestjs";
|
|
import { DatabaseService } from "./database/database.service";
|
|
import { HealthController } from "./health.controller";
|
|
|
|
jest.mock("@sentry/nestjs", () => ({
|
|
getClient: jest.fn(),
|
|
}));
|
|
|
|
describe("HealthController", () => {
|
|
let controller: HealthController;
|
|
|
|
const mockDb = {
|
|
execute: jest.fn().mockResolvedValue([]),
|
|
};
|
|
|
|
const mockCacheManager = {
|
|
set: jest.fn().mockResolvedValue(undefined),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [HealthController],
|
|
providers: [
|
|
{
|
|
provide: DatabaseService,
|
|
useValue: {
|
|
db: mockDb,
|
|
},
|
|
},
|
|
{
|
|
provide: CACHE_MANAGER,
|
|
useValue: mockCacheManager,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
controller = module.get<HealthController>(HealthController);
|
|
});
|
|
|
|
it("should return ok if database and redis are connected", async () => {
|
|
mockDb.execute.mockResolvedValue([]);
|
|
mockCacheManager.set.mockResolvedValue(undefined);
|
|
(Sentry.getClient as jest.Mock).mockReturnValue({
|
|
getOptions: () => ({ dsn: "http://dsn" }),
|
|
});
|
|
|
|
const result = await controller.check();
|
|
expect(result.status).toBe("ok");
|
|
expect(result.database).toBe("connected");
|
|
expect(result.redis).toBe("connected");
|
|
expect(result.sentry).toBe("active");
|
|
});
|
|
|
|
it("should return error if database is disconnected", async () => {
|
|
mockDb.execute.mockRejectedValue(new Error("DB Error"));
|
|
mockCacheManager.set.mockResolvedValue(undefined);
|
|
const result = await controller.check();
|
|
expect(result.status).toBe("error");
|
|
expect(result.database).toBe("disconnected");
|
|
expect(result.databaseError).toBe("DB Error");
|
|
expect(result.redis).toBe("connected");
|
|
});
|
|
|
|
it("should return error if redis is disconnected", async () => {
|
|
mockDb.execute.mockResolvedValue([]);
|
|
mockCacheManager.set.mockRejectedValue(new Error("Redis Error"));
|
|
const result = await controller.check();
|
|
expect(result.status).toBe("error");
|
|
expect(result.database).toBe("connected");
|
|
expect(result.redis).toBe("disconnected");
|
|
expect(result.redisError).toBe("Redis Error");
|
|
});
|
|
|
|
it("should return sentry disabled if client or dsn is missing", async () => {
|
|
mockDb.execute.mockResolvedValue([]);
|
|
mockCacheManager.set.mockResolvedValue(undefined);
|
|
(Sentry.getClient as jest.Mock).mockReturnValue(undefined);
|
|
|
|
const result = await controller.check();
|
|
expect(result.sentry).toBe("disabled");
|
|
|
|
(Sentry.getClient as jest.Mock).mockReturnValue({
|
|
getOptions: () => ({ dsn: undefined }),
|
|
});
|
|
const result2 = await controller.check();
|
|
expect(result2.sentry).toBe("disabled");
|
|
});
|
|
});
|