1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/server-impl.test.ts

122 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-09-14 20:17:13 +02:00
import { EventEmitter } from 'events';
import express from 'express';
import { createTestConfig } from '../test/config/test-config';
import { start, create } from './server-impl';
jest.mock(
'./routes',
() =>
class Index {
router() {
return express.Router();
}
},
);
const noop = () => {};
const eventStore = new EventEmitter();
const settingStore = {
get: () => {
Promise.resolve('secret');
},
};
jest.mock('./metrics', () => ({
createMetricsMonitor() {
return {
startMonitoring: noop,
stopMonitoring: noop,
};
},
}));
jest.mock('./db', () => ({
createStores() {
return {
db: { destroy: () => undefined },
clientInstanceStore: { destroy: noop },
clientMetricsStore: { destroy: noop, on: noop },
eventStore,
publicSignupTokenStore: { destroy: noop, on: noop },
settingStore,
};
},
}));
2021-09-10 12:07:04 +02:00
jest.mock('../migrator', () => ({
migrateDb: () => Promise.resolve(),
}));
jest.mock(
'./util/version',
() =>
function () {
return 'unleash-test-version';
},
);
test('should call preHook', async () => {
let called = 0;
const config = createTestConfig({
server: { port: 0 },
preHook: () => {
called++;
},
});
2021-09-14 20:17:13 +02:00
const { stop } = await start(config);
expect(called).toBe(1);
2021-09-14 20:17:13 +02:00
await stop();
});
test('should call preRouterHook', async () => {
let called = 0;
2021-09-14 20:17:13 +02:00
const { stop } = await start(
createTestConfig({
server: { port: 0 },
preRouterHook: () => {
called++;
},
}),
);
expect(called === 1).toBe(true);
2021-09-14 20:17:13 +02:00
await stop();
});
test('should call eventHook', async () => {
let called = 0;
const config = createTestConfig({
server: { port: 0 },
eventHook: () => {
called++;
},
});
2021-09-14 20:17:13 +02:00
const { stop } = await start(config);
eventStore.emit('feature-created', {});
expect(called === 1).toBe(true);
2021-09-14 20:17:13 +02:00
await stop();
});
2020-06-17 08:03:02 +02:00
test('should auto-create server on start()', async () => {
2021-09-14 20:17:13 +02:00
const { server, stop } = await start(
createTestConfig({ server: { port: 0 } }),
);
expect(typeof server === 'undefined').toBe(false);
2021-09-14 20:17:13 +02:00
await stop();
2020-06-17 08:03:02 +02:00
});
test('should not create a server using create()', async () => {
const config = createTestConfig({ server: { port: 0 } });
2021-09-14 20:17:13 +02:00
const { server, stop } = await create(config);
expect(server).toBeUndefined();
2021-09-14 20:17:13 +02:00
await stop();
2020-06-17 08:03:02 +02:00
});
test('should shutdown the server when calling stop()', async () => {
2021-09-14 20:17:13 +02:00
const { server, stop } = await start(
createTestConfig({ server: { port: 0 } }),
);
await stop();
expect(server.address()).toBe(null);
});