Testing
Testing a @Controller
import { Bootstrap, get, Module } from '@rhtml/di';
import { FastifyModule } from '@rhtml/fastify';
import fastify from 'fastify';
import { HealthCheckController } from './healthcheck.controller';
import { HealthCheckService } from './healthcheck.service';
import { HealthCheckStatus } from './types';
describe('healthcheck controller', () => {
let healthCheckController: HealthCheckController;
let healthCheckService: HealthCheckService;
beforeEach(async () => {
@Module({
imports: [
FastifyModule.forRoot(fastify, {
logger: true,
}),
],
providers: [HealthCheckService],
bootstrap: [HealthCheckController],
})
class AppModule {}
await Bootstrap(AppModule);
healthCheckController = get(HealthCheckController);
healthCheckService = get(HealthCheckService);
});
afterEach(() => {
jest.restoreAllMocks();
});
it('should be defined', () => {
expect(healthCheckController).toBeDefined();
});
it('should call the healthcheck service', async () => {
const response = {
server: { status: HealthCheckStatus.WORKING },
database: { status: 'ok' },
} as never;
const spy = jest
.spyOn(healthCheckService, 'checkServicesStatus')
.mockResolvedValue(response);
const result = await healthCheckController.healthCheck();
expect(spy).toHaveBeenCalled();
expect(result).toEqual(response);
});
});
The provided TypeScript code is a test suite written using the Jest framework for testing. It tests a controller called HealthCheckController, which utilizes @rhtml/di for dependency injection and @rhtml/fastify for web server functionality.
Imports:
@rhtml/di: Used for dependency injection.@rhtml/fastify: Used for integrating Fastify with the application.fastify: Represents the Fastify instance.jest: Testing framework.
Describing Test Suite:
- The
describe()function is used to group test cases related to the "healthcheck controller".
Test Setup:
healthCheckControllerandhealthCheckServiceare declared outside thebeforeEach()block to make them accessible across test cases.- Inside
beforeEach(), a module (AppModule) is defined using@Moduledecorator from@rhtml/di. It importsFastifyModule.forRoot()from@rhtml/fastifyto configure Fastify. HealthCheckServiceis registered as a provider within the module.Bootstrap(AppModule)is awaited to initialize the module.- Instances of
HealthCheckControllerandHealthCheckServiceare obtained usingget()function from@rhtml/diand assigned tohealthCheckControllerandhealthCheckServicerespectively.
Test Teardown:
jest.restoreAllMocks()is used in theafterEach()block to restore all mocked functions after each test case.
Test Cases:
- The first test case checks if
healthCheckControlleris defined. - The second test case verifies if the
healthCheck()method ofHealthCheckControllercalls thecheckServicesStatus()method ofHealthCheckService. It mocks thecheckServicesStatus()method usingjest.spyOn()and verifies if it's called. - It also checks if the result returned by
healthCheckController.healthCheck()matches the expected response.
Overall, this code tests the behavior of the HealthCheckController by mocking its dependencies and ensuring that it interacts correctly with the HealthCheckService. It validates that the controller functions as expected when handling health check requests.
Testing a @Service
import { Bootstrap, Module, set } from '@rhtml/di';
import { HealthCheckService } from './healthcheck.service';
import { HealthCheckStatus } from './types';
describe('health check service', () => {
let healthCheckService: HealthCheckService;
beforeEach(async () => {
@Module({
providers: [HealthCheckService],
})
class AppModule {}
await Bootstrap(AppModule);
healthCheckService = set(HealthCheckService);
});
afterEach(() => {
jest.restoreAllMocks();
});
it('should return WORKING status for the server and the correct status for the database', () => {
const response = {
server: {
status: HealthCheckStatus.WORKING,
},
} as never;
const result = healthCheckService.checkServicesStatus();
expect(result).toEqual(response);
});
});
The provided TypeScript code is a test suite written using the Jest framework for testing. It tests a service called HealthCheckService, which utilizes @rhtml/di for dependency injection.
Imports:
- The code imports necessary modules and libraries from
@rhtml/di.
Describing Test Suite:
- The
describe()function is used to group test cases related to the "health check service".
Test Setup:
healthCheckServiceis declared outside thebeforeEach()block to make it accessible across test cases.- Inside
beforeEach(), a module (AppModule) is defined using@Moduledecorator from@rhtml/di. It only provides theHealthCheckServiceas a provider. Bootstrap(AppModule)is awaited to initialize the module.- The
set()function from@rhtml/diis used to obtain an instance ofHealthCheckServiceand assign it tohealthCheckService.
Test Teardown:
jest.restoreAllMocks()is used in theafterEach()block to restore all mocked functions after each test case.
Test Case:
- The test case verifies that the
checkServicesStatus()method ofHealthCheckServicereturns the expected response. - It defines a
responseobject with the expected structure and values. - It calls the
checkServicesStatus()method and assigns the result toresult. - It asserts that the
resultmatches theresponseobject usingexpect().toEqual().
Overall, this code tests the behavior of the HealthCheckService by verifying that its checkServicesStatus() method returns the expected response. It ensures that the service functions correctly when checking the status of services.