-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove App controller and service
- Loading branch information
Showing
6 changed files
with
109 additions
and
46 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
HealthCheckService, | ||
HealthIndicatorFunction, | ||
HealthIndicatorResult, | ||
MemoryHealthIndicator, | ||
TypeOrmHealthIndicator, | ||
} from '@nestjs/terminus'; | ||
import { type TestingModule, Test } from '@nestjs/testing'; | ||
import { createMock } from 'ts-auto-mock'; | ||
|
||
import { HealthController } from '~app/health.controller'; | ||
|
||
function getStatus(key: string): HealthIndicatorResult { | ||
return { [key]: { status: 'up' } }; | ||
} | ||
|
||
describe('HealthController', () => { | ||
let controller: HealthController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [HealthController], | ||
}) | ||
.useMocker((token) => { | ||
if (token === HealthCheckService) { | ||
return createMock<HealthCheckService>({ | ||
check: jest | ||
.fn() | ||
.mockImplementation((indicators: HealthIndicatorFunction[]) => | ||
Promise.all(indicators.map((indicator) => indicator())), | ||
), | ||
}); | ||
} | ||
|
||
if (token === TypeOrmHealthIndicator) { | ||
return createMock<TypeOrmHealthIndicator>({ | ||
pingCheck: jest.fn().mockImplementation(getStatus), | ||
}); | ||
} | ||
|
||
if (token === MemoryHealthIndicator) { | ||
return createMock<MemoryHealthIndicator>({ | ||
checkHeap: jest.fn().mockImplementation(getStatus), | ||
checkRSS: jest.fn().mockImplementation(getStatus), | ||
}); | ||
} | ||
|
||
return; | ||
}) | ||
.compile(); | ||
|
||
controller = module.get<HealthController>(HealthController); | ||
}); | ||
|
||
it('should be an instance of HealthController', () => { | ||
expect(controller).toBeInstanceOf(HealthController); | ||
}); | ||
|
||
it('should check the health', async () => { | ||
await expect(controller.healthCheck()).resolves.toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"database": { | ||
"status": "up", | ||
}, | ||
}, | ||
{ | ||
"mem_rss": { | ||
"status": "up", | ||
}, | ||
}, | ||
{ | ||
"mem_heap": { | ||
"status": "up", | ||
}, | ||
}, | ||
] | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { | ||
type HealthCheckResult, | ||
HealthCheck, | ||
HealthCheckService, | ||
MemoryHealthIndicator, | ||
TypeOrmHealthIndicator, | ||
} from '@nestjs/terminus'; | ||
|
||
@Controller('health') | ||
export class HealthController { | ||
constructor( | ||
private readonly health: HealthCheckService, | ||
private readonly mem: MemoryHealthIndicator, | ||
private readonly orm: TypeOrmHealthIndicator, | ||
) {} | ||
|
||
@Get() | ||
@HealthCheck() | ||
healthCheck(): Promise<HealthCheckResult> { | ||
return this.health.check([ | ||
() => this.orm.pingCheck('database'), | ||
() => this.mem.checkRSS('mem_rss', 768 * 2 ** 20 /* 768 MB */), | ||
() => this.mem.checkHeap('mem_heap', 512 * 2 ** 20 /* 512 MB */), | ||
]); | ||
} | ||
} |