Skip to content

Commit

Permalink
test: fix broken unit tests for 7.0.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Mar 22, 2020
1 parent 247c123 commit 795ce21
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 60 deletions.
5 changes: 1 addition & 4 deletions e2e/terminus.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { TerminusModule } from '../lib/terminus.module';
import { SIG_NOT_EXIST } from '../lib/terminus-bootstrap.service';
import { TerminusLibProvider } from '../lib/terminus-lib.provider';
import { HttpAdapterHost } from '@nestjs/core';
import {
TerminusOptionsFactory,
TerminusModuleOptions,
} from '../lib/interfaces';
import { TerminusOptionsFactory, TerminusModuleOptions } from '../';
import { TerminusOptions } from '@godaddy/terminus';

describe('Terminus', () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/health-check/health-check-executor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class HealthCheckExecutor {
),
);

const info = (results || [])
const info: HealthCheckResult = (results || [])
.concat(errors || [])
.reduce(
(previous: Object, current: Object) => Object.assign(previous, current),
Expand Down
28 changes: 26 additions & 2 deletions lib/health-check/health-check-result.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
import { HealthIndicatorResult } from '../health-indicator';

/**
* @publicApi
*/
export type HealthCheckStatus = 'error' | 'ok' | 'shutting_down';

/**
* The result of a health check
* @publicApi
*/
export interface HealthCheckResult {
results: any[];
errors: any[];
/**
* The overall status of the Health Check
*/
status: HealthCheckStatus;
/**
* The info object contains information of each health indicator
* which is of status "up"
*/
info?: HealthIndicatorResult;
/**
* The error object contains information of each health indicator
* which is of status "down"
*/
errors?: HealthIndicatorResult;
/**
* The details object contains information of every health indicator.
*/
details: HealthIndicatorResult;
}
2 changes: 1 addition & 1 deletion lib/health-indicator/database/typeorm.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
promiseTimeout,
checkPackages,
} from '../../utils';
import { HealthIndicator, HealthIndicatorResult } from '../health-indicator';
import { HealthIndicator, HealthIndicatorResult } from '../';

export interface TypeOrmPingCheckSettings {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/health-indicator/disk/disk.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Inject } from '@nestjs/common';
import { isNil } from '@nestjs/common/utils/shared.utils';
import * as checkDiskSpace from 'check-disk-space';

import { HealthIndicator, HealthIndicatorResult } from '../health-indicator';
import { HealthIndicator, HealthIndicatorResult } from '../';
import { CHECKDISKSPACE_LIB } from '../../terminus.constants';
import { StorageExceededError } from '../../errors';
import { STORAGE_EXCEEDED } from '../../errors/messages.constant';
Expand Down
2 changes: 1 addition & 1 deletion lib/health-indicator/dns/dns.health.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, HttpService } from '@nestjs/common';
import { AxiosResponse, AxiosRequestConfig, AxiosError } from 'axios';
import { HealthIndicator, HealthIndicatorResult } from '../health-indicator';
import { HealthIndicator, HealthIndicatorResult } from '../';
import { HealthCheckError } from '@godaddy/terminus';

/**
Expand Down
24 changes: 24 additions & 0 deletions lib/health-indicator/health-indicator-result.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @publicApi
*/
export type HealthIndicatorStatus = 'up' | 'down';

/**
* The result object of a health indicator
* @publicApi
*/
export type HealthIndicatorResult = {
/**
* The key of the health indicator which should be uniqe
*/
[key: string]: {
/**
* The status if the given health indicator was successful or not
*/
status: HealthIndicatorStatus;
/**
* Optional settings of the health indicator result
*/
[optionalKeys: string]: any;
};
};
20 changes: 1 addition & 19 deletions lib/health-indicator/health-indicator.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
/**
* The result object of a health indicator
* @publicApi
*/
export type HealthIndicatorResult = {
/**
* The key of the health indicator which should be uniqe
*/
[key: string]: {
/**
* The status if the given health indicator was successful or not
*/
status: string;
/**
* Optional settings of the health indicator result
*/
[optionalKeys: string]: any;
};
};
import { HealthIndicatorResult } from './';

/**
* A health indicator function for a health check
Expand Down
6 changes: 4 additions & 2 deletions lib/health-indicator/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export * from './health-indicator-result.interface';
export * from './health-indicator';

/** Health Indicators */
export * from './dns/dns.health';
export * from './database/mongoose.health';
export * from './database/typeorm.health';
export * from './microservice/microservice.health';
export * from './microservice/grpc.health';
export * from './disk';
export * from './memory';

export * from './health-indicator';
2 changes: 1 addition & 1 deletion lib/health-indicator/memory/memory.health.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';

import { HealthIndicator, HealthIndicatorResult } from '../health-indicator';
import { HealthIndicator, HealthIndicatorResult } from '../';
import { STORAGE_EXCEEDED } from '../../errors/messages.constant';
import { StorageExceededError } from '../../errors';

Expand Down
2 changes: 1 addition & 1 deletion lib/health-indicator/microservice/microservice.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
promiseTimeout,
TimeoutError as PromiseTimeoutError,
} from '../../utils';
import { HealthIndicator, HealthIndicatorResult } from '../health-indicator';
import { HealthIndicator, HealthIndicatorResult } from '../';

type ClientOptions =
| RedisOptions
Expand Down
39 changes: 14 additions & 25 deletions lib/terminus-bootstrap.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {
TerminusBootstrapService,
} from './terminus-bootstrap.service';
import { HttpAdapterHost, ApplicationConfig } from '@nestjs/core';
import { TerminusEndpoint, TerminusModuleOptions } from './interfaces';
import { TerminusEndpoint, TerminusModuleOptions } from './';
import { HealthCheckError, Terminus } from '@godaddy/terminus';
import { Test, TestingModule, TestingModuleBuilder } from '@nestjs/testing';
import { TERMINUS_MODULE_OPTIONS, TERMINUS_LIB } from './terminus.constants';
import { HealthCheckExecutor } from './health-check/health-check-executor.service';

const httpServer = jest.fn();

Expand All @@ -20,6 +21,10 @@ const applicationConfigMock = {
getGlobalPrefix: jest.fn().mockImplementation(() => '/health'),
};

const healthCheckExecutorMock = {
execute: jest.fn().mockImplementation(() => Promise.resolve({})),
};

const terminusMock = jest.fn();

const upHealthIndicator = jest
Expand Down Expand Up @@ -47,8 +52,6 @@ describe('TerminusBootstrapService', () => {
let terminus: Terminus;
let module: TestingModuleBuilder;
let context: TestingModule;
// let applicationConfig: ApplicationConfig;
// let httpAdapterHost: HttpAdapterHost;

beforeEach(async () => {
module = Test.createTestingModule({
Expand All @@ -70,15 +73,17 @@ describe('TerminusBootstrapService', () => {
provide: ApplicationConfig,
useValue: applicationConfigMock,
},
{
provide: HealthCheckExecutor,
useValue: healthCheckExecutorMock,
},
],
});

context = await module.compile();

bootstrapService = context.get(TerminusBootstrapService);
terminus = context.get(TERMINUS_LIB);
// applicationConfig = module.get(ApplicationConfig);
// httpAdapterHost = module.get(HttpAdapterHost);
});
describe('onApplicationBootstrap', () => {
it('should ignore bootstrap if there is no HTTP Server', async () => {
Expand All @@ -101,6 +106,10 @@ describe('TerminusBootstrapService', () => {
provide: ApplicationConfig,
useValue: applicationConfigMock,
},
{
provide: HealthCheckExecutor,
useValue: null,
},
],
});
const context = await module.compile();
Expand Down Expand Up @@ -150,26 +159,6 @@ describe('TerminusBootstrapService', () => {
describe('prepareHealthChecksMap', () => {
it('should prepare a correct map', () => {
const map = bootstrapService.getHealthChecksMap();
expect(map['/up']).not.toBe(undefined);
expect(map['/test']).toBe(undefined);
});

it('should call the given test indicator', () => {
const map = bootstrapService.getHealthChecksMap();
expect(upHealthIndicator).not.toHaveBeenCalled();
map['/up']();
expect(upHealthIndicator).toHaveBeenCalled();
expect(downHealthIndicator).not.toHaveBeenCalled();
});

it('should throw an error with causes summary when a health indicator fails', done => {
const map = bootstrapService.getHealthChecksMap();
map['/down']().catch((err: HealthCheckError) => {
expect(err.causes).toEqual({
down: { status: 'down' },
});
done();
});
});

it('should prepend the global prefix when using useGlobalPrefix on TerminusOptions', async () => {
Expand Down
2 changes: 1 addition & 1 deletion sample/deprecated/000-dogs-app/src/dog/dog.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { DogService } from './dog.service';
import { DogState } from './dog.interface';
import { HealthCheckError } from '@godaddy/terminus';
import { HealthIndicatorResult, HealthIndicator } from '../../../../lib';
import { HealthIndicatorResult, HealthIndicator } from '@nestjs/terminus';

@Injectable()
export class DogHealthIndicator extends HealthIndicator {
Expand Down
2 changes: 1 addition & 1 deletion sample/deprecated/000-dogs-app/src/dog/dog.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class DogService {
{ name: 'Fido', state: DogState.GOOD_BOY },
{ name: 'Jazz', state: DogState.GOOD_BOY },
{ name: 'Sweetheart', state: DogState.GOOD_BOY },
{ name: 'Buttercup II', state: DogState.BAD_BOY },
{ name: 'Buttercup II', state: DogState.GOOD_BOY },
];

public async getDogs(): Promise<Dog[]> {
Expand Down

0 comments on commit 795ce21

Please sign in to comment.