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

118 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-09-14 20:17:13 +02:00
import express from 'express';
import { createTestConfig } from '../test/config/test-config';
import { start, create } from './server-impl';
2023-04-04 13:28:17 +02:00
import FakeEventStore from '../test/fixtures/fake-event-store';
jest.mock(
'./routes',
() =>
class Index {
router() {
return express.Router();
}
},
);
const noop = () => {};
2023-04-04 13:28:17 +02:00
const eventStore = new FakeEventStore();
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,
removeInstancesOlderThanTwoDays: noop,
},
clientMetricsStore: { destroy: noop, on: noop },
eventStore,
publicSignupTokenStore: { destroy: noop, on: noop },
settingStore,
projectStore: { getAll: () => Promise.resolve([]) },
};
},
}));
2021-09-10 12:07:04 +02:00
jest.mock('../migrator', () => ({
migrateDb: () => Promise.resolve(),
}));
2023-04-04 13:28:17 +02:00
jest.mock('./util/db-lock', () => ({
withDbLock: () => (fn) => fn,
}));
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 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();
2023-04-04 13:28:17 +02:00
expect(server!.address()).toBe(null);
});