1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/src/lib/routes/health-check.test.ts

89 lines
2.4 KiB
TypeScript
Raw Normal View History

import supertest from 'supertest';
import { EventEmitter } from 'events';
import { createServices } from '../services';
import { createTestConfig } from '../../test/config/test-config';
2016-11-13 15:31:10 +01:00
import createStores from '../../test/fixtures/store';
import getLogger from '../../test/fixtures/no-logger';
import getApp from '../app';
import { IUnleashStores } from '../types';
2016-11-13 15:31:10 +01:00
const eventBus = new EventEmitter();
function getSetup() {
const stores = createStores();
const config = createTestConfig();
const services = createServices(stores, config);
const app = getApp(config, stores, services, eventBus);
2016-11-13 15:31:10 +01:00
return {
request: supertest(app),
stores,
destroy: () => {
services.versionService.destroy();
services.clientMetricsService.destroy();
services.apiTokenService.destroy();
},
2016-11-13 15:31:10 +01:00
};
}
let request;
let destroy;
let stores;
beforeEach(() => {
const setup = getSetup();
request = setup.request;
destroy = setup.destroy;
stores = setup.stores;
});
2016-11-13 15:31:10 +01:00
afterEach(() => {
destroy();
2021-01-06 13:25:25 +01:00
getLogger.setMuteError(false);
});
test('should give 500 when db is failing', () => {
const config = createTestConfig();
const failingStores: Partial<IUnleashStores> = {
// @ts-ignore
featureTypeStore: {
getAll: () => Promise.reject(new Error('db error')),
},
clientMetricsStore: {
// @ts-ignore
on: () => {},
},
};
// @ts-ignore
const services = createServices(failingStores, config);
// @ts-ignore
const app = getApp(createTestConfig(), failingStores, services, eventBus);
request = supertest(app);
2021-01-06 13:25:25 +01:00
getLogger.setMuteError(true);
expect.assertions(2);
stores.featureToggleStore.getAll = () =>
Promise.reject(new Error('db error'));
2017-11-02 09:23:38 +01:00
return request
.get('/health')
.expect(500)
.expect((res) => {
expect(res.status).toBe(500);
expect(res.body.health).toBe('BAD');
2017-11-02 09:23:38 +01:00
});
2016-11-13 15:31:10 +01:00
});
test('should give 200 when db is not failing', () => {
expect.assertions(0);
return request.get('/health').expect(200);
2016-11-13 15:31:10 +01:00
});
test('should give health=GOOD when db is not failing', () => {
expect.assertions(2);
2017-11-02 09:23:38 +01:00
return request
.get('/health')
.expect(200)
.expect((res) => {
expect(res.status).toBe(200);
expect(res.body.health).toBe('GOOD');
2017-11-02 09:23:38 +01:00
});
2016-11-13 15:31:10 +01:00
});