Skip to content

Commit

Permalink
fix: Fix crash when setting logger to false
Browse files Browse the repository at this point in the history
The logger injected into the health check service was incorrectly typed,
and thus assumed to have a method that the fake logger doesn't have.
  • Loading branch information
ostkrok committed Feb 14, 2023
1 parent e8b2c02 commit 3d417fd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 3 additions & 4 deletions lib/health-check/health-check.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HealthCheckExecutor } from './health-check-executor.service';
import { ERROR_LOGGER } from './error-logger/error-logger.provider';
import { ErrorLogger } from './error-logger/error-logger.interface';
import { TERMINUS_LOGGER } from './logger/logger.provider';
import { ConsoleLogger } from '@nestjs/common';
import { ConsoleLogger, LoggerService } from '@nestjs/common';

const healthCheckExecutorMock: Partial<HealthCheckExecutor> = {
execute: jest.fn(),
Expand All @@ -14,8 +14,7 @@ const errorLoggerMock: ErrorLogger = {
getErrorMessage: jest.fn(),
};

const loggerMock: Partial<ConsoleLogger> = {
setContext: jest.fn(),
const loggerMock: Partial<LoggerService> = {
log: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
Expand All @@ -25,7 +24,7 @@ const loggerMock: Partial<ConsoleLogger> = {
describe('HealthCheckService', () => {
let healthCheckExecutor: HealthCheckExecutor;
let healthCheckService: HealthCheckService;
let logger: ConsoleLogger;
let logger: LoggerService;
let errorLogger: ErrorLogger;

beforeEach(async () => {
Expand Down
7 changes: 5 additions & 2 deletions lib/health-check/health-check.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ServiceUnavailableException,
Inject,
ConsoleLogger,
LoggerService,
} from '@nestjs/common';
import { HealthIndicatorFunction } from '../health-indicator';
import { ErrorLogger } from './error-logger/error-logger.interface';
Expand All @@ -22,9 +23,11 @@ export class HealthCheckService {
@Inject(ERROR_LOGGER)
private readonly errorLogger: ErrorLogger,
@Inject(TERMINUS_LOGGER)
private readonly logger: ConsoleLogger,
private readonly logger: LoggerService,
) {
this.logger.setContext(HealthCheckService.name);
if (this.logger instanceof ConsoleLogger) {
this.logger.setContext(HealthCheckService.name);
}
}

/**
Expand Down

0 comments on commit 3d417fd

Please sign in to comment.